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, &blk) ⇒ Image
Returns a new instance of Image.
7
8
9
10
11
12
13
|
# File 'lib/versionable/image.rb', line 7
def initialize(model, column, &blk)
@model = model
@column = column
@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
57
58
59
60
61
62
63
|
# File 'lib/versionable/image.rb', line 57
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
27
28
29
|
# File 'lib/versionable/image.rb', line 27
def as_json(_options = nil)
serializable_hash
end
|
31
32
33
34
35
|
# File 'lib/versionable/image.rb', line 31
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
|
37
38
39
40
41
42
43
|
# File 'lib/versionable/image.rb', line 37
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
19
20
21
|
# File 'lib/versionable/image.rb', line 19
def respond_to?(method, include_private = false)
super || @versions.key?(method)
end
|
#to_json(_options = nil) ⇒ Object
23
24
25
|
# File 'lib/versionable/image.rb', line 23
def to_json(_options = nil)
JSON.generate(as_json)
end
|
#url ⇒ Object
15
16
17
|
# File 'lib/versionable/image.rb', line 15
def url
model.send(column)
end
|
45
46
47
48
49
50
51
|
# File 'lib/versionable/image.rb', line 45
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
|