Class: Wrappi::Endpoint
- Inherits:
-
Object
- Object
- Wrappi::Endpoint
- Defined in:
- lib/wrappi/testing.rb,
lib/wrappi/endpoint.rb
Instance Attribute Summary collapse
-
#input_params ⇒ Object
readonly
Returns the value of attribute input_params.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .around_request(&block) ⇒ Object
-
.async_callback(&block) ⇒ Object
Configs =================.
- .body(*args) ⇒ Object
- .cache_options(&block) ⇒ Object
-
.call(*args) ⇒ Object
ClassMethods ################ ============ API class metnods ================.
- .call!(*args) ⇒ Object
-
.inherited(subclass) ⇒ Object
Inheritance =================.
- .retry_if(&block) ⇒ Object
- .setup(&block) ⇒ Object
-
.success?(request) ⇒ Boolean
success behaviour =================== overridable.
Instance Method Summary collapse
- #around_request ⇒ Object
- #async(async_options = {}) ⇒ Object
- #body ⇒ Object
- #cache_key ⇒ Object
- #cache_options ⇒ Object
- #call ⇒ Object
- #call! ⇒ Object
-
#consummated_params ⇒ Object
overridable.
- #error? ⇒ Boolean
- #fixture_content ⇒ Object
- #fixture_name ⇒ Object
- #fixture_params_key ⇒ Object
- #flush ⇒ Object
-
#initialize(input_params = {}, options = {}) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #on_error(&block) ⇒ Object
- #on_success(&block) ⇒ Object
- #perform_async_callback(async_options = {}) ⇒ Object
- #response ⇒ Object
- #retry_if ⇒ Object
- #status ⇒ Object
- #success? ⇒ Boolean
- #url ⇒ Object
- #url_with_params ⇒ Object
Constructor Details
#initialize(input_params = {}, options = {}) ⇒ Endpoint
Returns a new instance of Endpoint.
75 76 77 78 |
# File 'lib/wrappi/endpoint.rb', line 75 def initialize(input_params = {}, = {}) @input_params = input_params @options = end |
Instance Attribute Details
#input_params ⇒ Object (readonly)
Returns the value of attribute input_params.
74 75 76 |
# File 'lib/wrappi/endpoint.rb', line 74 def input_params @input_params end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
74 75 76 |
# File 'lib/wrappi/endpoint.rb', line 74 def @options end |
Class Method Details
.around_request(&block) ⇒ Object
46 47 48 |
# File 'lib/wrappi/endpoint.rb', line 46 def self.around_request(&block) @around_request = block end |
.async_callback(&block) ⇒ Object
Configs =================
42 43 44 |
# File 'lib/wrappi/endpoint.rb', line 42 def self.async_callback(&block) @async_callback = block end |
.body(*args) ⇒ Object
33 34 35 |
# File 'lib/wrappi/endpoint.rb', line 33 def self.body(*args) new(*args).body end |
.cache_options(&block) ⇒ Object
54 55 56 |
# File 'lib/wrappi/endpoint.rb', line 54 def self.(&block) @cache_options = block end |
.call(*args) ⇒ Object
ClassMethods ################
API class metnods ================
25 26 27 |
# File 'lib/wrappi/endpoint.rb', line 25 def self.call(*args) new(*args).call end |
.call!(*args) ⇒ Object
29 30 31 |
# File 'lib/wrappi/endpoint.rb', line 29 def self.call!(*args) new(*args).call! end |
.inherited(subclass) ⇒ Object
Inheritance =================
59 60 61 62 63 64 65 |
# File 'lib/wrappi/endpoint.rb', line 59 def self.inherited(subclass) super(subclass) subclass.instance_variable_set(:@async_callback, @async_callback) subclass.instance_variable_set(:@around_request, @around_request) subclass.instance_variable_set(:@retry_if, @retry_if) subclass.instance_variable_set(:@cache_options, @cache_options) end |
.retry_if(&block) ⇒ Object
50 51 52 |
# File 'lib/wrappi/endpoint.rb', line 50 def self.retry_if(&block) @retry_if = block end |
.setup(&block) ⇒ Object
37 38 39 |
# File 'lib/wrappi/endpoint.rb', line 37 def self.setup(&block) instance_exec(&block) end |
.success?(request) ⇒ Boolean
success behaviour ===================
overridable
69 70 71 |
# File 'lib/wrappi/endpoint.rb', line 69 def self.success?(request) request.code >= 200 && request.code < 300 end |
Instance Method Details
#around_request ⇒ Object
141 142 143 |
# File 'lib/wrappi/endpoint.rb', line 141 def around_request self.class.instance_variable_get(:@around_request) end |
#async(async_options = {}) ⇒ Object
111 112 113 |
# File 'lib/wrappi/endpoint.rb', line 111 def async( = {}) async_handler.call(self, ) end |
#body ⇒ Object
105 |
# File 'lib/wrappi/endpoint.rb', line 105 def body; response.body end |
#cache_key ⇒ Object
135 136 137 138 |
# File 'lib/wrappi/endpoint.rb', line 135 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_options ⇒ Object
149 150 151 |
# File 'lib/wrappi/endpoint.rb', line 149 def self.class.instance_variable_get(:@cache_options) end |
#call ⇒ Object
91 92 93 94 |
# File 'lib/wrappi/endpoint.rb', line 91 def call return false unless success? self end |
#call! ⇒ Object
96 97 98 99 |
# File 'lib/wrappi/endpoint.rb', line 96 def call! raise UnsuccessfulResponse.new(self) unless success? self end |
#consummated_params ⇒ Object
overridable
116 117 118 |
# File 'lib/wrappi/endpoint.rb', line 116 def consummated_params params end |
#error? ⇒ Boolean
108 |
# File 'lib/wrappi/endpoint.rb', line 108 def error?; !success? end |
#fixture_content ⇒ Object
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_name ⇒ Object
4 5 6 |
# File 'lib/wrappi/testing.rb', line 4 def fixture_name "#{self.class}#{fixture_params_key}.json" end |
#fixture_params_key ⇒ Object
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 |
#flush ⇒ Object
109 |
# File 'lib/wrappi/endpoint.rb', line 109 def flush; @response = nil end |
#on_error(&block) ⇒ Object
86 87 88 89 |
# File 'lib/wrappi/endpoint.rb', line 86 def on_error(&block) block.call(self) unless success? self end |
#on_success(&block) ⇒ Object
81 82 83 84 |
# File 'lib/wrappi/endpoint.rb', line 81 def on_success(&block) block.call(self) if success? self end |
#perform_async_callback(async_options = {}) ⇒ Object
131 132 133 |
# File 'lib/wrappi/endpoint.rb', line 131 def perform_async_callback( = {}) instance_exec(, &async_callback) end |
#response ⇒ Object
101 102 103 |
# File 'lib/wrappi/endpoint.rb', line 101 def response @response ||= Executer.call(self) end |
#retry_if ⇒ Object
145 146 147 |
# File 'lib/wrappi/endpoint.rb', line 145 def retry_if self.class.instance_variable_get(:@retry_if) end |
#status ⇒ Object
107 |
# File 'lib/wrappi/endpoint.rb', line 107 def status; response.status end |
#success? ⇒ Boolean
106 |
# File 'lib/wrappi/endpoint.rb', line 106 def success?; response.success? end |
#url ⇒ Object
120 121 122 |
# File 'lib/wrappi/endpoint.rb', line 120 def url _url.to_s end |
#url_with_params ⇒ Object
124 125 126 127 128 129 |
# File 'lib/wrappi/endpoint.rb', line 124 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 |