Class: EML::Response

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/eml/response.rb

Direct Known Subclasses

UK::Response

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, id: nil) ⇒ Response

Returns a new instance of Response.



17
18
19
20
21
22
23
# File 'lib/eml/response.rb', line 17

def initialize(response, id: nil)
  @response = T.let(response, HTTP::Response)
  @body = T.let(nil, T.nilable(String))
  @http_status = T.let(nil, T.nilable(Integer))
  @url = T.let(nil, T.nilable(String))
  raise_error(id: id) unless http_status == 200
end

Class Method Details

.field(field_name) ⇒ Object



9
10
11
12
13
14
# File 'lib/eml/response.rb', line 9

def self.field(field_name)
  define_method(field_name) do
    string_name = field_name.to_s
    T.unsafe(self).body[string_name]
  end
end

Instance Method Details

#bodyObject



28
29
30
31
32
# File 'lib/eml/response.rb', line 28

def body
  @body ||= JSON.parse(@response.body.to_s)
rescue JSON::ParserError
  @body = ""
end

#errorObject



35
36
37
# File 'lib/eml/response.rb', line 35

def error
  body.fetch("message", nil) if body.respond_to?(:fetch)
end

#headersObject



40
41
42
# File 'lib/eml/response.rb', line 40

def headers
  @response.headers
end

#http_statusObject



45
46
47
# File 'lib/eml/response.rb', line 45

def http_status
  @http_status ||= @response.status.code
end

#success?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/eml/response.rb', line 50

def success?
  http_status == 200
end

#urlObject



55
56
57
# File 'lib/eml/response.rb', line 55

def url
  @url ||= T.unsafe(@response).url.to_s
end