Class: Billy::ProxyRequestStub
- Inherits:
-
Object
- Object
- Billy::ProxyRequestStub
- Defined in:
- lib/billy/proxy_request_stub.rb
Instance Attribute Summary collapse
-
#requests ⇒ Object
readonly
Returns the value of attribute requests.
Instance Method Summary collapse
- #and_return(response) ⇒ Object
- #call(method, url, params, headers, body) ⇒ Object
- #has_requests? ⇒ Boolean
-
#initialize(url, options = {}) ⇒ ProxyRequestStub
constructor
A new instance of ProxyRequestStub.
- #matches?(method, url) ⇒ Boolean
Constructor Details
#initialize(url, options = {}) ⇒ ProxyRequestStub
Returns a new instance of ProxyRequestStub.
7 8 9 10 11 12 13 |
# File 'lib/billy/proxy_request_stub.rb', line 7 def initialize(url, = {}) @options = { method: :get }.merge() @method = @options[:method].to_s.upcase @url = url @requests = [] @response = { code: 204, headers: {}, text: '' } end |
Instance Attribute Details
#requests ⇒ Object
Returns the value of attribute requests.
5 6 7 |
# File 'lib/billy/proxy_request_stub.rb', line 5 def requests @requests end |
Instance Method Details
#and_return(response) ⇒ Object
15 16 17 18 |
# File 'lib/billy/proxy_request_stub.rb', line 15 def and_return(response) @response = response self end |
#call(method, url, params, headers, body) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/billy/proxy_request_stub.rb', line 20 def call(method, url, params, headers, body) push_request(method, url, params, headers, body) if @response.respond_to?(:call) res = @response.call(params, headers, body, url, method) else res = @response end code = res[:code] || 200 headers = res[:headers] || {} headers['Content-Type'] = res[:content_type] if res[:content_type] if res[:json] headers = { 'Content-Type' => 'application/json' }.merge(headers) body = MultiJson.dump(res[:json]) elsif res[:jsonp] headers = { 'Content-Type' => 'application/javascript' }.merge(headers) if res[:callback] callback = res[:callback] elsif res[:callback_param] callback = params[res[:callback_param]][0] else callback = params['callback'][0] end body = "#{callback}(#{MultiJson.dump(res[:jsonp])})" elsif res[:text] headers = { 'Content-Type' => 'text/plain' }.merge(headers) body = res[:text] elsif res[:redirect_to] code = 302 headers = { 'Location' => res[:redirect_to] } else body = res[:body] end [code, headers, body] end |
#has_requests? ⇒ Boolean
60 61 62 |
# File 'lib/billy/proxy_request_stub.rb', line 60 def has_requests? @requests.any? end |
#matches?(method, url) ⇒ Boolean
64 65 66 67 68 69 70 71 72 |
# File 'lib/billy/proxy_request_stub.rb', line 64 def matches?(method, url) if @method == 'ALL' || method == @method if @url.is_a?(Regexp) url.match(@url) else Billy.config.strip_query_params ? (url.split('?')[0] == @url) : (url == @url) end end end |