Class: Heathen::Client::Response
- Inherits:
-
Object
- Object
- Heathen::Client::Response
- Defined in:
- lib/heathen/client/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #converted(file = nil) ⇒ Object
-
#download(args = { }) ⇒ Object
convenience method.
- #error ⇒ Object
- #error? ⇒ Boolean
-
#get(which = :converted, &block) ⇒ Object
yields data to block.
-
#initialize(client, response) ⇒ Response
constructor
A new instance of Response.
- #original(file = nil) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(client, response) ⇒ Response
Returns a new instance of Response.
7 8 9 10 11 12 13 14 15 |
# File 'lib/heathen/client/response.rb', line 7 def initialize(client, response) @client = client @status = response.code begin @data = Yajl::Parser.parse(response.body) rescue Exception @data = { 'error' => 'Internal Server Error' } end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/heathen/client/response.rb', line 5 def data @data end |
Instance Method Details
#converted(file = nil) ⇒ Object
21 22 23 |
# File 'lib/heathen/client/response.rb', line 21 def converted(file = nil) url('converted', file) end |
#download(args = { }) ⇒ Object
convenience method
45 46 47 48 49 50 51 52 |
# File 'lib/heathen/client/response.rb', line 45 def download(args = { }) File.open(args[:path], "wb") do |dest| get(args.fetch(:which, :converted)) do |data| dest.write(data) end dest.path end end |
#error ⇒ Object
25 26 27 |
# File 'lib/heathen/client/response.rb', line 25 def error data['error'] end |
#error? ⇒ Boolean
33 34 35 |
# File 'lib/heathen/client/response.rb', line 33 def error? !error.nil? end |
#get(which = :converted, &block) ⇒ Object
yields data to block
38 39 40 41 42 |
# File 'lib/heathen/client/response.rb', line 38 def get(which = :converted, &block) unless error? RestClient.get(send(which), &block) end end |
#original(file = nil) ⇒ Object
17 18 19 |
# File 'lib/heathen/client/response.rb', line 17 def original(file = nil) url('original', file) end |
#success? ⇒ Boolean
29 30 31 |
# File 'lib/heathen/client/response.rb', line 29 def success? error.nil? end |