Class: Billy::ProxyRequestStub

Inherits:
Object
  • Object
show all
Defined in:
lib/billy/proxy_request_stub.rb

Instance Method Summary collapse

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 = {})
  @options = {:method => :get}.merge(options)
  @method = @options[:method].to_s.upcase
  @url = url
  @response = {code: 204, headers: {}, text: ""}
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 = 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

#matches?(method, url) ⇒ Boolean

Returns:

  • (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