Module: Airborne::RestClientRequester
- Defined in:
- lib/spaceborne.rb
Overview
spaceborne enhancements
Instance Method Summary collapse
- #body?(method) ⇒ Boolean
- #calc_body(options, local) ⇒ Object
- #calc_headers(options, local) ⇒ Object
- #handle_proxy(_options, local) ⇒ Object
- #make_request(method, url, options = {}) ⇒ Object
- #pre_request(options) ⇒ Object
- #send_restclient(method, url, body, headers) ⇒ Object
- #split_options(options) ⇒ Object
Instance Method Details
#body?(method) ⇒ Boolean
76 77 78 79 80 81 82 83 |
# File 'lib/spaceborne.rb', line 76 def body?(method) case method when :post, :patch, :put true else false end end |
#calc_body(options, local) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/spaceborne.rb', line 109 def calc_body(, local) return '' unless [:body] if local[:nonjson_data] || !local[:is_hash] [:body] else [:body].to_json end end |
#calc_headers(options, local) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/spaceborne.rb', line 94 def calc_headers(, local) headers = base_headers.merge([:headers] || {}) headers[:no_restclient_headers] = true return headers unless local[:is_hash] headers.delete('Content-Type') if [:nonjson_data] headers end |
#handle_proxy(_options, local) ⇒ Object
103 104 105 106 107 |
# File 'lib/spaceborne.rb', line 103 def handle_proxy(, local) return unless local[:proxy] RestClient.proxy = local[:proxy] end |
#make_request(method, url, options = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/spaceborne.rb', line 136 def make_request(method, url, = {}) hdrs, body = pre_request() send_restclient(method, get_url(url), body, hdrs) rescue RestClient::ServerBrokeConnection => e raise e rescue RestClient::Exception => e if [301, 302].include?(e.response&.code) e.response.&follow_redirection else e.response end end |
#pre_request(options) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/spaceborne.rb', line 127 def pre_request() @json_body = nil = () handle_proxy(, ) hdrs = calc_headers(, ) @request_body = calc_body(, ) [hdrs, @request_body] end |
#send_restclient(method, url, body, headers) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/spaceborne.rb', line 119 def send_restclient(method, url, body, headers) if body?(method) RestClient.send(method, url, body, headers) else RestClient.send(method, url, headers) end end |
#split_options(options) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/spaceborne.rb', line 85 def () local = {} local[:nonjson_data] = .dig(:headers, :nonjson_data) [:headers].delete(:nonjson_data) if local[:nonjson_data] local[:is_hash] = [:body].is_a?(Hash) local[:proxy] = .dig(:headers, :use_proxy) local end |