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
-
#initialize(http_response, config) ⇒ Response
constructor
A new instance of Response.
- #json_response_body ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
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
56 57 58 |
# File 'lib/api_model/response.rb', line 56 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
31 32 33 34 35 36 37 |
# File 'lib/api_model/response.rb', line 31 def build(builder, hash) if builder.respond_to? :build builder.build hash else builder.new hash end end |
#build_objects ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/api_model/response.rb', line 17 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 return if json_response_body.nil? if response_build_hash.is_a? Array self.objects = response_build_hash.collect{ |hash| build http_response.builder, hash } elsif response_build_hash.is_a? Hash self.objects = self.build http_response.builder, response_build_hash end self end |
#json_response_body ⇒ Object
39 40 41 42 43 44 |
# File 'lib/api_model/response.rb', line 39 def json_response_body @json_response_body ||= JSON.parse http_response.api_call.body rescue JSON::ParserError Log.info "Could not parse JSON response: #{http_response.api_call.body}" return nil end |