Class: Backend::File — Documentation by YARD 0.9.12

# Class: Backend::File

Inherits:
  Object
  - Object
  - Backend::File
  show all

Includes:
  ActiveModel::Model

Defined in:
  file.rb

## Overview

ActiveModel for representing the backend files (special ones and source files too)

## Constant Summary

BUFFER_SIZE =

      40_960

## Instance Attribute Summary collapse

- #name ⇒ Object

  Returns the value of attribute name.

- #response ⇒ Object readonly

  Returns the value of attribute response.

## Class Method Summary collapse

- .query_from_list(params, key_list = nil) ⇒ Object

  TODO: Replace SuseBackend.build_query_from_hash with this function asap.

## Instance Method Summary collapse

- #destroy(query = {}) ⇒ Object

  Tries to destroy the file from the backend.

- #destroy!(query = {}) ⇒ Object

  Tries to destroy the file from the backend, freeze the object and return the response, otherwise will raise an StandardError.

- #file(query = {}) ⇒ Object

  Returns a File object (closed) that have the content of the Backend file.

- #file=(input_stream) ⇒ Object

  Sets the content File object for that model instance, calculates the right response data.

- #file_from_path(path) ⇒ Object

  Sets the content File object from a path.

- #initialize(attributes = {}) ⇒ File constructor

  A new instance of File.

- #reload ⇒ Object

  Reloads from Backend the file content.

- #save(query = {}, content = nil) ⇒ Object

  Tries to save the file to the backend.

- #save!(query = {}, content = nil) ⇒ Object

  Tries to save the file to the backend and return the response, otherwise will raise an StandardError If “content” parameter is provided then is passed directly to the backend, otherwise it creates a temp file and then send it to the backend.

- #to_s(query = {}) ⇒ Object

  Converts file into a String if it's valid.

## Constructor Details

### #initialize(attributes = {}) ⇒ `File`

Returns a new instance of File

    14
    15
    16
    17
    18
    19
    # File 'file.rb', line 14

    def initialize(attributes = {})
      super
      @file ||= nil
      @response = {}
      @last_read_query = {}
    end

## Instance Attribute Details

### #name ⇒ `Object`

Returns the value of attribute name

    8
    9
    10
    # File 'file.rb', line 8

    def name
      @name
    end

### #response ⇒ `Object` (readonly)

Returns the value of attribute response

    9
    10
    11
    # File 'file.rb', line 9

    def response
      @response
    end

## Class Method Details

### .query_from_list(params, key_list = nil) ⇒ `Object`

TODO: Replace SuseBackend.build_query_from_hash with this function asap

    115
    116
    117
    118
    119
    # File 'file.rb', line 115

    def self.query_from_list(params, key_list = nil)
      key_list ||= params.keys
      query = params.slice(*key_list).to_query
      query.present? ? "?#{query}" : query
    end

## Instance Method Details

### #destroy(query = {}) ⇒ `Object`

Tries to destroy the file from the backend. Returns nil if some StandardError is raised

    106
    107
    108
    109
    110
    111
    112
    # File 'file.rb', line 106

    def destroy(query = {})
      destroy!(query)
    rescue => e
      @backend_file_errors = e.message
      valid?
      nil
    end

### #destroy!(query = {}) ⇒ `Object`

Tries to destroy the file from the backend, freeze the object and return the response, otherwise will raise an StandardError

    98
    99
    100
    101
    102
    103
    # File 'file.rb', line 98

    def destroy!(query = {})
      backend_response = Connection.delete(full_path(query))
      @response = { type: backend_response['Content-Type'], status: backend_response.code, size: backend_response.content_length }
      freeze
      @response
    end

### #file(query = {}) ⇒ `Object`

Returns a File object (closed) that have the content of the Backend file

    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    # File 'file.rb', line 38

    def file(query = {})
      if @file.nil? && valid? # Read it from Backend
        Connection.get(full_path(query)) do |backend_response|
          Tempfile.open('backend_file', Dir.tmpdir, encoding: 'ascii-8bit') do |tempfile|
            backend_response.read_body do |buffer|
              tempfile.write(buffer)
            end
            @file = tempfile
            @response = { type: backend_response['Content-Type'], status: backend_response.code, size: @file.size }
          end
        end
        @last_read_query = query
      end
      @file
    rescue => e
      @backend_file_errors = e.message
      valid?
      nil
    end

### #file=(input_stream) ⇒ `Object`

Sets the content File object for that model instance, calculates the right response data

    22
    23
    24
    25
    26
    27
    28
    # File 'file.rb', line 22

    def file=(input_stream)
      Tempfile.open('backend_file', "#{Rails.root}/tmp", encoding: 'ascii-8bit') do |tempfile|
        buffer = ''
        tempfile.write(buffer) while input_stream.read(BUFFER_SIZE, buffer)
        @file = tempfile
      end
    end

### #file_from_path(path) ⇒ `Object`

Sets the content File object from a path

    31
    32
    33
    34
    35
    # File 'file.rb', line 31

    def file_from_path(path)
      @file = ::File.open(path)
      @response = { type: MiniMime.lookup_by_filename(@file.path).try(:content_type), status: 200, size: @file.size }
      @file.close
    end

### #reload ⇒ `Object`

Reloads from Backend the file content

    65
    66
    67
    68
    69
    # File 'file.rb', line 65

    def reload
      @file = nil
      @response = {}
      file(@last_read_query)
    end

### #save(query = {}, content = nil) ⇒ `Object`

Tries to save the file to the backend. Returns nil if some StandardError is raised

    89
    90
    91
    92
    93
    94
    95
    # File 'file.rb', line 89

    def save(query = {}, content = nil)
      save!(query, content)
    rescue => e
      @backend_file_errors = e.message
      valid?
      nil
    end

### #save!(query = {}, content = nil) ⇒ `Object`

Tries to save the file to the backend and return the response, otherwise will raise an StandardError If “content” parameter is provided then is passed directly to the backend, otherwise it creates a temp file and then send it to the backend

    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    # File 'file.rb', line 74

    def save!(query = {}, content = nil)
      backend_response = nil
      if content
        backend_response = Connection.put(full_path(query), content)
        reload
      else
        @file.open
        backend_response = Connection.put(full_path(query), @file)
        @response = { type: backend_response['Content-Type'], status: backend_response.code, size: backend_response.content_length }
        @file.close
      end
      backend_response
    end

### #to_s(query = {}) ⇒ `Object`

Converts file into a String if it's valid

    59
    60
    61
    62
    # File 'file.rb', line 59

    def to_s(query = {})
      file(query)
      !@file.nil? && valid? ? ::File.open(@file.path).read : nil
    end