Class: FakeHTTP::Responder

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

Instance Method Summary collapse

Constructor Details

#initialize(pattern, code) ⇒ Responder

Returns a new instance of Responder.



75
76
77
# File 'lib/fake_http.rb', line 75

def initialize(pattern, code)
  @pattern, @code = Mustermann.new(pattern), code
end

Instance Method Details

#call(uri, options) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/fake_http.rb', line 83

def call(uri, options)
  params = @pattern.params(uri)
  status 200
  content_type "application/json"
  body = instance_exec(params, options, &@code)
  HTTP::Response.new(status: status, version: "1.1", headers: headers, body: body.to_json)
end

#content_type(mime_type) ⇒ Object



96
97
98
# File 'lib/fake_http.rb', line 96

def content_type(mime_type)
  headers["Content-Type"] = mime_type
end

#headersObject



100
101
102
# File 'lib/fake_http.rb', line 100

def headers
  @headers ||= {}
end

#match(uri) ⇒ Object



79
80
81
# File 'lib/fake_http.rb', line 79

def match(uri)
  @pattern.match(uri)
end

#status(new_status = nil) ⇒ Object



91
92
93
94
# File 'lib/fake_http.rb', line 91

def status(new_status=nil)
  @status = new_status if new_status
  @status
end