Class: NeverBounce::API::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/never_bounce/api/session.rb

Overview

A single request-response session (server dialog).

Instance Attribute Summary collapse

Instance Attribute Details

#httpartyModule

HTTParty module. Default is HTTParty.

Returns:

  • (Module)


27
28
29
# File 'lib/never_bounce/api/session.rb', line 27

def httparty
  @httparty ||= HTTParty
end

#requestObject

An instance of Request::Base successor.

Returns:

  • (Object)


22
23
24
# File 'lib/never_bounce/api/session.rb', line 22

def request
  @request
end

#responseResponse::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_previewHash, false

Response body, opportunistically JSON-parsed based on server_content_type.

Returns:

  • (Hash)
  • (false)

    If server_content_type isn’t a JSON.



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.message}"
      end
    else
      false
    end
  end
end

#robj_klass_and_attrsArray<Class,Hash>

Render response object class and constructor attributes based on peeking in the data.

Returns:

  • (Array<Class,Hash>)


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_codeInteger

Server response code. Default is server_obj.code.

Returns:

  • (Integer)


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_typeString

Server response content type. Default is server_obj.content_type.

Returns:

  • (String)


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_objObject

Make a request, return server response object from HTTParty.

Returns:

  • (Object)

    An HTTParty::Response.



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_okBoolean Also known as: server_ok?

true if response is an OK response.

Returns:

  • (Boolean)


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_rawString

Raw server response body. Default is server_obj.body.

Returns:

  • (String)


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