Class: Resto::Response::Base
- Inherits:
-
Object
- Object
- Resto::Response::Base
- Defined in:
- lib/resto/response/base.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #body ⇒ Object
- #code ⇒ Object
- #current_formatter ⇒ Object
- #format(symbol, options = {}) ⇒ Object
- #formatter(formatter) ⇒ Object
- #http_response(response) ⇒ Object
- #klass(klass) ⇒ Object
- #read_body ⇒ Object
- #read_xpath ⇒ Object
- #to_collection ⇒ Object
- #to_object ⇒ Object
- #translator(translator) ⇒ Object
- #valid? ⇒ Boolean
- #xpath(xpath) ⇒ Object
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
61 62 63 |
# File 'lib/resto/response/base.rb', line 61 def response @response end |
Instance Method Details
#body ⇒ Object
49 50 51 |
# File 'lib/resto/response/base.rb', line 49 def body @response ? @response.body : nil end |
#code ⇒ Object
53 54 55 |
# File 'lib/resto/response/base.rb', line 53 def code @response ? @response.code : nil end |
#current_formatter ⇒ Object
37 38 39 |
# File 'lib/resto/response/base.rb', line 37 def current_formatter @formatter ||= Resto::Format.get(@symbol || :default) end |
#format(symbol, options = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/resto/response/base.rb', line 19 def format(symbol, = {}) xpath = [:xpath] xpath(xpath) if xpath formatter(Resto::Format.get(@symbol = symbol)) end |
#formatter(formatter) ⇒ Object
33 34 35 |
# File 'lib/resto/response/base.rb', line 33 def formatter(formatter) tap { @formatter = formatter } end |
#http_response(response) ⇒ Object
41 42 43 |
# File 'lib/resto/response/base.rb', line 41 def http_response(response) tap { @response = response } end |
#klass(klass) ⇒ Object
10 11 12 |
# File 'lib/resto/response/base.rb', line 10 def klass(klass) tap { @klass = klass } end |
#read_body ⇒ Object
45 46 47 |
# File 'lib/resto/response/base.rb', line 45 def read_body body ? current_formatter.decode(body, :xpath => read_xpath) : nil end |
#read_xpath ⇒ Object
29 30 31 |
# File 'lib/resto/response/base.rb', line 29 def read_xpath @xpath || "//#{@klass.to_s.downcase}" end |
#to_collection ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/resto/response/base.rb', line 73 def to_collection return self unless @translator (read_body || []).map do |hash| @translator.call(@klass, hash).tap do |instance| instance.response = self end end end |
#to_object ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/resto/response/base.rb', line 63 def to_object return self unless @translator body = read_body ? read_body.first : nil @translator.call(@klass, body).tap do |instance| instance.response = self end end |
#translator(translator) ⇒ Object
14 15 16 17 |
# File 'lib/resto/response/base.rb', line 14 def translator(translator) @translator = Resto::Translator::ResponseFactory.create(translator) self end |
#valid? ⇒ Boolean
57 58 59 |
# File 'lib/resto/response/base.rb', line 57 def valid? (/\A20\d{1}\z/ =~ code.to_s) == 0 end |
#xpath(xpath) ⇒ Object
25 26 27 |
# File 'lib/resto/response/base.rb', line 25 def xpath(xpath) tap { @xpath = xpath } end |