Class: AssetHost::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_host/asset.rb

Direct Known Subclasses

Fallback

Defined Under Namespace

Classes: Fallback

Constant Summary collapse

BAD_STATUS =
[400, 404, 500, 502]
GOOD_STATUS =
[200]
ATTRIBUTES =

[
  :caption,
  :title,
  :id,
  :size,
  :taken_at,
  :owner,
  :url,
  :api_url,
  :native,
  :image_file_size
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Asset

Returns a new instance of Asset.



124
125
126
127
128
129
130
131
# File 'lib/asset_host/asset.rb', line 124

def initialize(json)
  @json = json
  @_sizes = {}

  ATTRIBUTES.map(&:to_s).each do |attribute|
    send "#{attribute}=", @json[attribute]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/asset_host/asset.rb', line 145

def method_missing(method, *args)
  if output = Asset.outputs.find { |output| output['code'] == method.to_s }
    self._size(output)
  else
    super
  end
end

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



121
122
123
# File 'lib/asset_host/asset.rb', line 121

def json
  @json
end

Class Method Details

.configObject



19
20
21
# File 'lib/asset_host/asset.rb', line 19

def config
  @config ||= Rails.application.config.assethost
end

.connectionObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/asset_host/asset.rb', line 92

def connection
  @connection ||= begin
    Faraday.new(
      :url    => "http://#{config.server}", 
      :params => { auth_token: config.token }
    ) do |conn|
      conn.request :json
      conn.response :json
      conn.adapter Faraday.default_adapter
    end
  end
end

.create(attributes = {}) ⇒ Object




73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/asset_host/asset.rb', line 73

def create(attributes={})
  response = connection.post do |request|
    request.url "#{config.prefix}/assets"
    request.body = attributes
  end

  json = response.body

  if response.success? && json
    asset = new(json)
    Rails.cache.write("asset/asset-#{asset.id}", json)
  else
    return false
  end

  asset
end

.find(id) ⇒ Object

asset = Asset.find(id) Given an asset ID, returns an asset object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/asset_host/asset.rb', line 50

def find(id)
  key = "asset/asset-#{id}"
  
  if cached = Rails.cache.read(key)
    return new(cached)
  end
  
  response = connection.get "#{config.prefix}/assets/#{id}"
  json = response.body

  if !GOOD_STATUS.include?(response.status.to_i) || !json
    asset = Fallback.new
  else
    asset = new(json)
    Rails.cache.write(key, json)
  end

  asset
end

.outputsObject




25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asset_host/asset.rb', line 25

def outputs
  @outputs ||= begin
    key = "assets/outputs"

    if cached = Rails.cache.read(key)
      return cached
    end
    
    response = connection.get "#{config.prefix}/outputs"
    
    if !GOOD_STATUS.include? response.status
      outputs = JSON.parse(File.read(File.join(AssetHost.fallback_root, "outputs.json")))
    else
      outputs = response.body
      Rails.cache.write(key, outputs)
    end
    
    outputs
  end
end

Instance Method Details

#_size(output) ⇒ Object




135
136
137
# File 'lib/asset_host/asset.rb', line 135

def _size(output)
  @_sizes[ output['code'] ] ||= AssetSize.new(self, output)
end

#as_json(options = {}) ⇒ Object




141
142
143
# File 'lib/asset_host/asset.rb', line 141

def as_json(options={})
  @json
end