Class: Versionable::Image
- Inherits:
-
Object
- Object
- Versionable::Image
show all
- Defined in:
- lib/versionable/image.rb
Defined Under Namespace
Classes: InvalidMetadata
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, column, accessor, &blk) ⇒ Image
Returns a new instance of Image.
7
8
9
10
11
12
13
14
|
# File 'lib/versionable/image.rb', line 7
def initialize(model, column, accessor, &blk)
@model = model
@column = column
@accessor = accessor
@versions = {}
instance_eval(&blk) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/versionable/image.rb', line 58
def method_missing(name, *args, &blk)
if versions.respond_to?(:key?) && versions.key?(name)
versions[name]
else
super
end
end
|
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
6
7
8
|
# File 'lib/versionable/image.rb', line 6
def height
@height
end
|
#width ⇒ Object
Returns the value of attribute width.
6
7
8
|
# File 'lib/versionable/image.rb', line 6
def width
@width
end
|
Instance Method Details
#as_json(_options = nil) ⇒ Object
28
29
30
|
# File 'lib/versionable/image.rb', line 28
def as_json(_options = nil)
serializable_hash
end
|
32
33
34
35
36
|
# File 'lib/versionable/image.rb', line 32
def fetch_metadata
metadata_url = URI.parse(Versionable::Version.new(self, meta: true).url)
json_data = Net::HTTP.get(metadata_url)
blank?(json_data) ? nil : JSON.parse(Net::HTTP.get(metadata_url))
end
|
38
39
40
41
42
43
44
|
# File 'lib/versionable/image.rb', line 38
def height_from_metadata(hash)
@height = hash['thumbor']['source']['height']
rescue
raise InvalidMetadata,
'Argument is not valid thumbor metadata. " +
"Use #fetch_metadata to get it.'
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
20
21
22
|
# File 'lib/versionable/image.rb', line 20
def respond_to?(method, include_private = false)
super || @versions.key?(method)
end
|
#to_json(_options = nil) ⇒ Object
24
25
26
|
# File 'lib/versionable/image.rb', line 24
def to_json(_options = nil)
JSON.generate(as_json)
end
|
#url ⇒ Object
16
17
18
|
# File 'lib/versionable/image.rb', line 16
def url
blank?(model.send(column)) ? legacy_url(accessor) : model.send(column)
end
|
46
47
48
49
50
51
52
|
# File 'lib/versionable/image.rb', line 46
def width_from_metadata(hash)
@width = hash['thumbor']['source']['width']
rescue
raise InvalidMetadata,
'Argument is not valid thumbor metadata. " +
"Use #fetch_metadata to get it.'
end
|