Class: Wrappi::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappi/response.rb

Overview

Wrapper around HTTP::Response check documentation at: github.com/httprb/http/wiki/Response-Handling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_klass, &block) ⇒ Response

Returns a new instance of Response.



8
9
10
11
# File 'lib/wrappi/response.rb', line 8

def initialize(endpoint_klass, &block)
  @endpoint_klass = endpoint_klass
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/wrappi/response.rb', line 69

def method_missing(method_name, *arguments, &block)
  if request.respond_to?(method_name)
    request.send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/wrappi/response.rb', line 7

def block
  @block
end

#endpoint_klassObject (readonly)

Returns the value of attribute endpoint_klass.



7
8
9
# File 'lib/wrappi/response.rb', line 7

def endpoint_klass
  @endpoint_klass
end

Instance Method Details

#bodyObject



23
24
25
# File 'lib/wrappi/response.rb', line 23

def body
  @body ||= parse
end

#called?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/wrappi/response.rb', line 19

def called?
  !!@request
end

#error?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/wrappi/response.rb', line 31

def error?
  !success?
end

#json_parseObject



63
64
65
66
67
# File 'lib/wrappi/response.rb', line 63

def json_parse
  JSON.parse(raw_body)
rescue
  raw_body
end

#parseObject



57
58
59
60
61
# File 'lib/wrappi/response.rb', line 57

def parse
  request.parse
rescue
  json_parse
end

#raw_bodyObject



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

def raw_body
  @raw_body ||= request.to_s
end

#requestObject Also known as: call



13
14
15
16
# File 'lib/wrappi/response.rb', line 13

def request
  @request ||= block.call
  # raise controlled errors
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/wrappi/response.rb', line 77

def respond_to_missing?(method_name, include_private = false)
  request.respond_to?(method_name) || super
end

#statusObject Also known as: status_code



43
44
45
# File 'lib/wrappi/response.rb', line 43

def status
  request.code
end

#success?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/wrappi/response.rb', line 27

def success?
  @success ||= endpoint_klass.success?(request)
end

#to_hObject



48
49
50
51
52
53
54
55
# File 'lib/wrappi/response.rb', line 48

def to_h
  @to_h ||= {
    raw_body: raw_body,
    code: code,
    uri: uri,
    success: success?
  }
end

#uriObject



39
40
41
# File 'lib/wrappi/response.rb', line 39

def uri
  request.uri.to_s
end