Class: Billy::ProxyRequestStub
- Inherits:
-
Object
- Object
- Billy::ProxyRequestStub
- Defined in:
- lib/billy/proxy_request_stub.rb
Instance Method Summary collapse
- #and_return(response) ⇒ Object
- #call(params, headers, body) ⇒ Object
-
#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.
5 6 7 8 9 10 |
# File 'lib/billy/proxy_request_stub.rb', line 5 def initialize(url, = {}) @options = {:method => :get}.merge() @method = @options[:method].to_s.upcase @url = url @response = [204, {}, ""] end |
Instance Method Details
#and_return(response) ⇒ Object
12 13 14 15 |
# File 'lib/billy/proxy_request_stub.rb', line 12 def and_return(response) @response = response self end |
#call(params, headers, body) ⇒ Object
17 18 19 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 |
# File 'lib/billy/proxy_request_stub.rb', line 17 def call(params, headers, body) if @response.respond_to?(:call) res = @response.call(params, headers, body) 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 = Yajl::Encoder.encode(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}(#{Yajl::Encoder::encode(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 |
#matches?(method, url) ⇒ Boolean
55 56 57 58 59 60 61 62 63 |
# File 'lib/billy/proxy_request_stub.rb', line 55 def matches?(method, url) if method == @method if @url.is_a?(Regexp) url.match(@url) else url.split('?')[0] == @url end end end |