Class: Versionable::Image

Inherits:
Object
  • Object
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 (private)



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

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/versionable/image.rb', line 6

def height
  @height
end

#widthObject (readonly)

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

#fetch_metadataObject



32
33
34
35
36
# File 'lib/versionable/image.rb', line 32

def 
   = URI.parse(Versionable::Version.new(self, meta: true).url)
  json_data = Net::HTTP.get()
  blank?(json_data) ? nil : JSON.parse(Net::HTTP.get())
end

#height_from_metadata(hash) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/versionable/image.rb', line 38

def (hash)
  @height = hash['thumbor']['source']['height']
rescue
  raise ,
        'Argument is not valid thumbor metadata. " +
        "Use #fetch_metadata to get it.'
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (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

#urlObject



16
17
18
# File 'lib/versionable/image.rb', line 16

def url
  blank?(model.send(column)) ? legacy_url(accessor) : model.send(column)
end

#width_from_metadata(hash) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/versionable/image.rb', line 46

def (hash)
  @width = hash['thumbor']['source']['width']
rescue
  raise ,
        'Argument is not valid thumbor metadata. " +
        "Use #fetch_metadata to get it.'
end