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(&block) ⇒ Response



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

def initialize(&block)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Method Details

#bodyObject



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

def body
  @body ||= parse
end

#called?Boolean



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

def called?
  !!@request
end

#error?Boolean



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

def error?
  !success?
end

#json_parseObject



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

def json_parse
  JSON.parse(raw_body)
rescue
  raw_body
end

#parseObject



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

def parse
  request.parse
rescue
  json_parse
end

#raw_bodyObject



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

def raw_body
  @raw_body ||= request.to_s
end

#requestObject Also known as: call



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

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

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



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

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

#statusObject Also known as: status_code



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

def status
  request.code
end

#success?Boolean



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

def success?
  @success ||= request.code < 300 && request.code >= 200
end

#to_hObject



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

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

#uriObject



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

def uri
  request.uri.to_s
end