Class: Refile::Attacher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/attacher.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

Presence =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

->(val) { val if val != "" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, record) ⇒ Attacher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Attacher.


9
10
11
12
13
14
# File 'lib/refile/attacher.rb', line 9

def initialize(definition, record)
  @definition = definition
  @record = record
  @errors = []
  @metadata = {}
end

Instance Attribute Details

#definitionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


4
5
6
# File 'lib/refile/attacher.rb', line 4

def definition
  @definition
end

#errorsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


4
5
6
# File 'lib/refile/attacher.rb', line 4

def errors
  @errors
end

#recordObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


4
5
6
# File 'lib/refile/attacher.rb', line 4

def record
  @record
end

#removeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


5
6
7
# File 'lib/refile/attacher.rb', line 5

def remove
  @remove
end

Instance Method Details

#basenameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


48
49
50
51
52
53
54
# File 'lib/refile/attacher.rb', line 48

def basename
  if filename and extension
    ::File.basename(filename, "." << extension)
  else
    filename
  end
end

#cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
# File 'lib/refile/attacher.rb', line 20

def cache
  @definition.cache
end

#cache!(uploadable) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/refile/attacher.rb', line 90

def cache!(uploadable)
  @metadata = {
    size: uploadable.size,
    content_type: Refile.extract_content_type(uploadable),
    filename: Refile.extract_filename(uploadable)
  }
  if valid?
    @metadata[:id] = cache.upload(uploadable).id
    
  elsif @definition.raise_errors?
    raise Refile::Invalid, @errors.join(", ")
  end
end

#cache_idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


44
45
46
# File 'lib/refile/attacher.rb', line 44

def cache_id
  Presence[@metadata[:id]]
end

#content_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


40
41
42
# File 'lib/refile/attacher.rb', line 40

def content_type
  Presence[@metadata[:content_type] || read(:content_type)]
end

#dataObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


152
153
154
# File 'lib/refile/attacher.rb', line 152

def data
  @metadata if valid?
end

#delete!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


138
139
140
141
142
# File 'lib/refile/attacher.rb', line 138

def delete!
  cache.delete(cache_id) if cache_id
  store.delete(id) if id
  @metadata = {}
end

#download(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/refile/attacher.rb', line 104

def download(url)
  unless url.to_s.empty?
    response = RestClient::Request.new(method: :get, url: url, raw_response: true).execute
    @metadata = {
      size: response.file.size,
      filename: URI.parse(url).path.split("/").last,
      content_type: response.headers[:content_type]
    }
    if valid?
      response.file.open if response.file.closed? # https://github.com/refile/refile/pull/210
      @metadata[:id] = cache.upload(response.file).id
      
    elsif @definition.raise_errors?
      raise Refile::Invalid, @errors.join(", ")
    end
  end
rescue RestClient::Exception
  @errors = [:download_failed]
  raise if @definition.raise_errors?
end

#extensionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


56
57
58
59
60
61
62
63
# File 'lib/refile/attacher.rb', line 56

def extension
  if filename
    Presence[::File.extname(filename).sub(/^\./, "")]
  elsif content_type
    type = MIME::Types[content_type][0]
    type.extensions[0] if type
  end
end

#filenameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


36
37
38
# File 'lib/refile/attacher.rb', line 36

def filename
  Presence[@metadata[:filename] || read(:filename)]
end

#getObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


65
66
67
68
69
70
71
# File 'lib/refile/attacher.rb', line 65

def get
  if cache_id
    cache.get(cache_id)
  elsif id
    store.get(id)
  end
end

#idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


28
29
30
# File 'lib/refile/attacher.rb', line 28

def id
  Presence[read(:id, true)]
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


16
17
18
# File 'lib/refile/attacher.rb', line 16

def name
  @definition.name
end

#present?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

148
149
150
# File 'lib/refile/attacher.rb', line 148

def present?
  not @metadata.empty?
end

#remove?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

144
145
146
# File 'lib/refile/attacher.rb', line 144

def remove?
  remove and remove != "" and remove !~ /\A0|false$\z/
end

#retrieve!(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


81
82
83
84
85
86
87
88
# File 'lib/refile/attacher.rb', line 81

def retrieve!(value)
  if value.is_a?(String)
    @metadata = Refile.parse_json(value, symbolize_names: true) || {}
  elsif value.is_a?(Hash)
    @metadata = value
  end
   if cache_id
end

#set(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


73
74
75
76
77
78
79
# File 'lib/refile/attacher.rb', line 73

def set(value)
  if value.is_a?(String) or value.is_a?(Hash)
    retrieve!(value)
  else
    cache!(value)
  end
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


32
33
34
# File 'lib/refile/attacher.rb', line 32

def size
  Presence[@metadata[:size] || read(:size)]
end

#storeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


24
25
26
# File 'lib/refile/attacher.rb', line 24

def store
  @definition.store
end

#store!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/refile/attacher.rb', line 125

def store!
  if remove?
    delete!
    write(:id, nil, true)
  elsif cache_id
    file = store.upload(get)
    delete!
    write(:id, file.id, true)
  end
  
  @metadata = {}
end

#valid?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

156
157
158
159
# File 'lib/refile/attacher.rb', line 156

def valid?
  @errors = @definition.validate(self)
  @errors.empty?
end