Class: Wrappi::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappi/testing.rb,
lib/wrappi/endpoint.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_params = {}, options = {}) ⇒ Endpoint

Returns a new instance of Endpoint.



24
25
26
27
# File 'lib/wrappi/endpoint.rb', line 24

def initialize(input_params = {}, options = {})
  @input_params = input_params
  @options = options
end

Instance Attribute Details

#input_paramsObject (readonly)

Returns the value of attribute input_params.



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

def input_params
  @input_params
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.around_request(&block) ⇒ Object



96
97
98
# File 'lib/wrappi/endpoint.rb', line 96

def self.around_request(&block)
  @around_request = block
end

.async_callback(&block) ⇒ Object



92
93
94
# File 'lib/wrappi/endpoint.rb', line 92

def self.async_callback(&block)
  @async_callback = block
end

.body(*args) ⇒ Object



37
38
39
# File 'lib/wrappi/endpoint.rb', line 37

def self.body(*args)
  new(*args).body
end

.cache_options(&block) ⇒ Object



104
105
106
# File 'lib/wrappi/endpoint.rb', line 104

def self.cache_options(&block)
  @cache_options = block
end

.call(*args) ⇒ Object



29
30
31
# File 'lib/wrappi/endpoint.rb', line 29

def self.call(*args)
  new(*args).call
end

.call!(*args) ⇒ Object



33
34
35
# File 'lib/wrappi/endpoint.rb', line 33

def self.call!(*args)
  new(*args).call!
end

.retry_if(&block) ⇒ Object



100
101
102
# File 'lib/wrappi/endpoint.rb', line 100

def self.retry_if(&block)
  @retry_if = block
end

Instance Method Details

#around_requestObject



117
118
119
# File 'lib/wrappi/endpoint.rb', line 117

def around_request
  self.class.instance_variable_get(:@around_request)
end

#async(async_options = {}) ⇒ Object



71
72
73
# File 'lib/wrappi/endpoint.rb', line 71

def async(async_options = {})
  async_handler.call(self, async_options)
end

#bodyObject



65
# File 'lib/wrappi/endpoint.rb', line 65

def body; response.body end

#cache_keyObject



112
113
114
115
# File 'lib/wrappi/endpoint.rb', line 112

def cache_key
  # TODO: think headers have to be in the key as well
  @cache_key ||= "[#{verb.to_s.upcase}]##{url}#{params_cache_key}"
end

#cache_optionsObject



124
125
126
# File 'lib/wrappi/endpoint.rb', line 124

def cache_options
  self.class.instance_variable_get(:@cache_options)
end

#callObject



51
52
53
54
# File 'lib/wrappi/endpoint.rb', line 51

def call
  return false unless success?
  self
end

#call!Object



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

def call!
  raise UnsuccessfulResponse.new(self) unless success?
  self
end

#consummated_paramsObject

overridable



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

def consummated_params
  params
end

#error?Boolean

Returns:

  • (Boolean)


68
# File 'lib/wrappi/endpoint.rb', line 68

def error?; !success? end

#fixture_contentObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wrappi/testing.rb', line 14

def fixture_content
  return {} unless success?
  {
    request: {
      method: verb.to_s,
      url: url,
      domain: domain,
      headers: headers,
      params: consummated_params,
      path: path_gen.path
    },
    response: {
      status: status,
      body: body
    }
  }
end

#fixture_nameObject



4
5
6
# File 'lib/wrappi/testing.rb', line 4

def fixture_name
  "#{self.class}#{fixture_params_key}.json"
end

#fixture_params_keyObject



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

def fixture_params_key
  return if processed_params.empty?
  d = Digest::MD5.hexdigest processed_params.to_json
  "-#{d}"
end

#flushObject



69
# File 'lib/wrappi/endpoint.rb', line 69

def flush; @response = nil end

#on_error(&block) ⇒ Object



46
47
48
49
# File 'lib/wrappi/endpoint.rb', line 46

def on_error(&block)
  block.call(self) unless success?
  self
end

#on_success(&block) ⇒ Object



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

def on_success(&block)
  block.call(self) if success?
  self  
end

#perform_async_callback(async_options = {}) ⇒ Object



108
109
110
# File 'lib/wrappi/endpoint.rb', line 108

def perform_async_callback(async_options = {})
  instance_exec(async_options, &async_callback)
end

#responseObject



61
62
63
# File 'lib/wrappi/endpoint.rb', line 61

def response
  @response ||= Executer.call(self)
end

#retry_ifObject



121
122
123
# File 'lib/wrappi/endpoint.rb', line 121

def retry_if
  self.class.instance_variable_get(:@retry_if)
end

#statusObject



67
# File 'lib/wrappi/endpoint.rb', line 67

def status; response.status end

#success?Boolean

Returns:

  • (Boolean)


66
# File 'lib/wrappi/endpoint.rb', line 66

def success?; response.success? end

#urlObject



80
81
82
# File 'lib/wrappi/endpoint.rb', line 80

def url
  _url.to_s
end

#url_with_paramsObject



84
85
86
87
88
89
# File 'lib/wrappi/endpoint.rb', line 84

def url_with_params
  return url unless verb == :get
  _url.tap do |u|
    u.query = URI.encode_www_form(consummated_params) if consummated_params.any?
  end.to_s
end