class NeverBounce::API::Session
A single request-response session (server dialog). @see API::Feature::BasicInitialize
@see API::Feature::Igetset
@see API::Feature::RequireAttr
Attributes
An instance of Request::Base
successor. @return [Object]
Public Instance Methods
HTTParty module. Default is HTTParty
. @!attribute httparty @return [Module]
# File lib/never_bounce/api/session.rb, line 27 def httparty @httparty ||= HTTParty end
Meaningful response object. @!attribute response @return [Response::Message] @return [Response::ErrorMessage]
# File lib/never_bounce/api/session.rb, line 35 def response @response ||= begin klass, attrs = require_attr(:robj_klass_and_attrs) klass.new(attrs) end end
Response
body, opportunistically JSON-parsed based on server_content_type
. @return [Hash] @return [false] If server_content_type
isn't a JSON.
# File lib/never_bounce/api/session.rb, line 72 def robj_hash_preview igetset(:robj_hash_preview) do case require_attr(:server_content_type) when "application/json" begin JSON.parse(server_raw) rescue JSON::ParserError => e raise FormatError, "#{e.class}: #{e.message}" end else false end end end
Render response object class and constructor attributes based on peeking in the data. @!attribute robj_klass_and_attrs
@return [Array<Class,Hash>]
# File lib/never_bounce/api/session.rb, line 45 def robj_klass_and_attrs @robj_klass_and_attrs ||= begin if (h = robj_hash_preview).is_a? Hash # Determine response class based on API convention. begin [ h.fetch("status") == "success" ? request.class.response_klass : Response::ErrorMessage, body_hash: robj_hash_preview, ] rescue KeyError raise FormatError, "Key 'status' not found: #{h.inspect}" end elsif robj_hash_preview == false # Let response class handle it on its own. [ request.class.response_klass, raw: server_raw, ] else raise AttributeError, "Unknown `robj_hash_preview`: #{robj_hash_preview.inspect}" end end end
Server response code. Default is server_obj.code
. @!attribute server_code
@return [Integer]
# File lib/never_bounce/api/session.rb, line 90 def server_code @server_code ||= require_attr(:server_obj).code end
Server response content type. Default is server_obj.content_type
. @!attribute server_content_type
@return [String]
# File lib/never_bounce/api/session.rb, line 97 def server_content_type @server_content_type ||= begin require_attr(:server_obj).content_type end end
Make a request, return server response object from HTTParty. @return [Object] An HTTParty::Response
.
# File lib/never_bounce/api/session.rb, line 105 def server_obj @server_obj ||= begin httparty.send(*require_attr(:request).to_httparty) end end
true
if response is an OK response. @!attribute server_ok
@return [Boolean]
# File lib/never_bounce/api/session.rb, line 114 def server_ok igetset(:server_ok) { require_attr(:server_obj).ok? } end
Raw server response body. Default is server_obj.body
. @!attribute server_raw
@return [String]
# File lib/never_bounce/api/session.rb, line 123 def server_raw @server_raw ||= begin raise RequestError, "Code not OK: #{server_code}" if not server_ok? server_obj.body end end