Class: NeverBounce::API::Session
- Inherits:
-
Object
- Object
- NeverBounce::API::Session
- Defined in:
- lib/never_bounce/api/session.rb
Overview
A single request-response session (server dialog).
Instance Attribute Summary collapse
-
#httparty ⇒ Module
HTTParty module.
-
#request ⇒ Object
An instance of
Request::Base
successor. -
#response ⇒ Response::Message, Response::ErrorMessage
Meaningful response object.
-
#robj_hash_preview ⇒ Hash, false
Response body, opportunistically JSON-parsed based on
server_content_type
. -
#robj_klass_and_attrs ⇒ Array<Class,Hash>
Render response object class and constructor attributes based on peeking in the data.
-
#server_code ⇒ Integer
Server response code.
-
#server_content_type ⇒ String
Server response content type.
-
#server_obj ⇒ Object
Make a request, return server response object from HTTParty.
-
#server_ok ⇒ Boolean
(also: #server_ok?)
true
if response is an OK response. -
#server_raw ⇒ String
Raw server response body.
Instance Attribute Details
#httparty ⇒ Module
HTTParty module. Default is HTTParty
.
27 28 29 |
# File 'lib/never_bounce/api/session.rb', line 27 def httparty @httparty ||= HTTParty end |
#request ⇒ Object
An instance of Request::Base
successor.
22 23 24 |
# File 'lib/never_bounce/api/session.rb', line 22 def request @request end |
#response ⇒ Response::Message, Response::ErrorMessage
Meaningful response object.
35 36 37 38 39 40 |
# 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 |
#robj_hash_preview ⇒ Hash, false
Response body, opportunistically JSON-parsed based on server_content_type
.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# 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.}" end else false end end end |
#robj_klass_and_attrs ⇒ Array<Class,Hash>
Render response object class and constructor attributes based on peeking in the data.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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_code ⇒ Integer
Server response code. Default is server_obj.code
.
90 91 92 |
# File 'lib/never_bounce/api/session.rb', line 90 def server_code @server_code ||= require_attr(:server_obj).code end |
#server_content_type ⇒ String
Server response content type. Default is server_obj.content_type
.
97 98 99 100 101 |
# 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 |
#server_obj ⇒ Object
Make a request, return server response object from HTTParty.
105 106 107 108 109 |
# File 'lib/never_bounce/api/session.rb', line 105 def server_obj @server_obj ||= begin httparty.send(*require_attr(:request).to_httparty) end end |
#server_ok ⇒ Boolean Also known as: server_ok?
true
if response is an OK response.
114 115 116 |
# File 'lib/never_bounce/api/session.rb', line 114 def server_ok igetset(:server_ok) { require_attr(:server_obj).ok? } end |
#server_raw ⇒ String
Raw server response body. Default is server_obj.body
.
123 124 125 126 127 128 |
# 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 |