Class: ApiModel::Response
- Inherits:
-
Object
- Object
- ApiModel::Response
- Defined in:
- lib/api_model/response.rb
Constant Summary collapse
- FALL_THROUGH_METHODS =
[ :class, :nil?, :empty?, :acts_like?, :as_json, :blank?, :duplicable?, :eval_js, :html_safe?, :in?, :presence, :present?, :psych_to_yaml, :to_json, :to_param, :to_query, :to_yaml, :to_yaml_properties, :with_options, :is_a?, :respond_to?, :kind_of? ]
Instance Attribute Summary collapse
-
#http_response ⇒ Object
Returns the value of attribute http_response.
-
#objects ⇒ Object
Returns the value of attribute objects.
Instance Method Summary collapse
- #build(builder, hash) ⇒ Object
- #build_objects ⇒ Object
-
#fetch_from_body(key_reference) ⇒ Object
Uses a string notation split by colons to fetch nested keys from a hash.
-
#initialize(http_response, config) ⇒ Response
constructor
A new instance of Response.
- #metadata ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Pass though any method which is not defined to the built objects.
- #response_body ⇒ Object
-
#response_build_hash ⇒ Object
If the model config defines a json root, use it on the response_body to dig down in to the hash.
- #response_cookies ⇒ Object
- #successful? ⇒ Boolean
Constructor Details
#initialize(http_response, config) ⇒ Response
Returns a new instance of Response.
12 13 14 15 |
# File 'lib/api_model/response.rb', line 12 def initialize(http_response, config) @http_response = http_response @_config = config || Configuration.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Pass though any method which is not defined to the built objects. This makes the response class quite transparent, and keeps the response acting like the built object, or array of objects.
88 89 90 |
# File 'lib/api_model/response.rb', line 88 def method_missing(method_name, *args, &block) objects.send method_name, *args, &block end |
Instance Attribute Details
#http_response ⇒ Object
Returns the value of attribute http_response.
10 11 12 |
# File 'lib/api_model/response.rb', line 10 def http_response @http_response end |
#objects ⇒ Object
Returns the value of attribute objects.
10 11 12 |
# File 'lib/api_model/response.rb', line 10 def objects @objects end |
Instance Method Details
#build(builder, hash) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/api_model/response.rb', line 36 def build(builder, hash) if builder.respond_to? :build if builder.method(:build).arity == 2 builder.build self, hash else builder.build hash end else builder.new hash end end |
#build_objects ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/api_model/response.rb', line 21 def build_objects raise UnauthenticatedError if @_config.raise_on_unauthenticated && http_response.api_call.response_code == 401 raise NotFoundError if @_config.raise_on_not_found && http_response.api_call.response_code == 404 raise ServerError if @_config.raise_on_server_error && http_response.api_call.response_code == 500 return self if response_body.nil? if response_build_hash.is_a? Array self.objects = response_build_hash.collect{ |hash| build http_response.builder, hash } else self.objects = self.build http_response.builder, response_build_hash end self end |
#fetch_from_body(key_reference) ⇒ Object
Uses a string notation split by colons to fetch nested keys from a hash. For example, if you have a hash which looks like:
{ foo: { bar: { baz: "Hello world" } } }
Then calling ++fetch_from_body(“foo.bar.baz”)++ would return “Hello world”
98 99 100 101 102 103 104 105 106 |
# File 'lib/api_model/response.rb', line 98 def fetch_from_body(key_reference) key_reference.split(".").inject(response_body) do |hash,key| begin hash.fetch(key, nil) rescue NoMethodError Log.error "Could not set #{key_reference} on #{hash}" end end end |
#metadata ⇒ Object
17 18 19 |
# File 'lib/api_model/response.rb', line 17 def @metadata ||= OpenStruct.new end |
#response_body ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/api_model/response.rb', line 48 def response_body return @response_body if @response_body.present? if @_config.parser.method(:parse).arity == 2 @response_body = @_config.parser.parse self, http_response.api_call.body else @response_body = @_config.parser.parse http_response.api_call.body end end |
#response_build_hash ⇒ Object
If the model config defines a json root, use it on the response_body to dig down in to the hash.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/api_model/response.rb', line 110 def response_build_hash if @_config.json_root.present? begin fetch_from_body @_config.json_root rescue raise ResponseBuilderError, "Could not find key #{@_config.json_root} in:\n#{response_body}" end else response_body end end |
#response_cookies ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/api_model/response.rb', line 62 def return @cookies if @cookies.present? jar = HTTP::CookieJar.new = http_response.api_call.headers_hash["Set-Cookie"] = .split(", ") unless .is_a?(Array) .each do || jar.parse , http_response.api_call.request.base_url end @cookies = jar. end |
#successful? ⇒ Boolean
58 59 60 |
# File 'lib/api_model/response.rb', line 58 def successful? http_response.api_call.success? end |