Class: AssetHost::Asset
- Inherits:
-
Object
- Object
- AssetHost::Asset
- Defined in:
- lib/asset_host/asset.rb
Direct Known Subclasses
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
-
#json ⇒ Object
Returns the value of attribute json.
Class Method Summary collapse
- .config ⇒ Object
- .connection ⇒ Object
-
.create(attributes = {}) ⇒ Object
—————–.
-
.find(id) ⇒ Object
asset = Asset.find(id) Given an asset ID, returns an asset object.
-
.outputs ⇒ Object
——————-.
Instance Method Summary collapse
-
#_size(output) ⇒ Object
———-.
-
#as_json(options = {}) ⇒ Object
———-.
-
#initialize(json) ⇒ Asset
constructor
A new instance of Asset.
- #is_rich? ⇒ Boolean
- #method_missing(method, *args) ⇒ Object
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
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
121 122 123 |
# File 'lib/asset_host/asset.rb', line 121 def json @json end |
Class Method Details
.config ⇒ Object
19 20 21 |
# File 'lib/asset_host/asset.rb', line 19 def config @config ||= Rails.application.config.assethost end |
.connection ⇒ Object
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 |
.outputs ⇒ Object
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
139 140 141 |
# File 'lib/asset_host/asset.rb', line 139 def _size(output) @_sizes[ output['code'] ] ||= AssetSize.new(self, output) end |
#as_json(options = {}) ⇒ Object
145 146 147 |
# File 'lib/asset_host/asset.rb', line 145 def as_json(={}) @json end |
#is_rich? ⇒ Boolean
133 134 135 |
# File 'lib/asset_host/asset.rb', line 133 def is_rich? self.native.present? end |