Module: CassetteRack::Request

Defined in:
lib/cassette-rack/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#request_optionsObject (readonly)

Returns the value of attribute request_options.



6
7
8
# File 'lib/cassette-rack/request.rb', line 6

def request_options
  @request_options
end

Instance Method Details

#delete(path, headers = {}) ⇒ Object



24
25
26
# File 'lib/cassette-rack/request.rb', line 24

def delete(path, headers={})
  request(:delete, path, {}, headers)
end

#get(path, params = {}, headers = {}) ⇒ Object



8
9
10
# File 'lib/cassette-rack/request.rb', line 8

def get(path, params={}, headers={})
  request(:get, path, params, headers)
end

#parse_content(body, req) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/cassette-rack/request.rb', line 53

def parse_content(body, req)
  if req.headers['content-type'] == 'application/json' and body.is_a?(Hash)
    body.to_json
  else
    body
  end
end

#patch(path, body = {}, headers = {}) ⇒ Object



16
17
18
# File 'lib/cassette-rack/request.rb', line 16

def patch(path, body={}, headers={})
  request(:patch, path, {}, headers, body)
end

#post(path, body = {}, headers = {}) ⇒ Object



12
13
14
# File 'lib/cassette-rack/request.rb', line 12

def post(path, body={}, headers={})
  request(:post, path, {}, headers, body)
end

#put(path, body = {}, headers = {}) ⇒ Object



20
21
22
# File 'lib/cassette-rack/request.rb', line 20

def put(path, body={}, headers={})
  request(:put, path, {}, headers, body)
end

#request(method, path, params = {}, headers = {}, body = {}, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cassette-rack/request.rb', line 28

def request(method, path, params={}, headers={}, body={}, options={})
  if request_options
    options = request_options
  else
    options = { url: CassetteRack.config.url, headers: headers }
  end

  conn = Faraday.new(options)
  res = conn.send(method) do |req|
    case method
    when :get, :delete
      req.url path, params
    when :post, :patch, :put
      req.path = path
      req.body = parse_content(body, req)
    end
  end

  @response = CassetteRack::Response.new(res)
end

#responseObject



49
50
51
# File 'lib/cassette-rack/request.rb', line 49

def response
  @response
end