Class: Mimic::APIRequest
- Inherits:
-
Object
- Object
- Mimic::APIRequest
- Defined in:
- lib/mimic/api_request.rb
Instance Attribute Summary collapse
-
#request_content_type ⇒ Object
readonly
Returns the value of attribute request_content_type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data, method = nil, request_content_type = '') ⇒ APIRequest
constructor
A new instance of APIRequest.
- #response ⇒ Object
- #setup_stubs_on(host) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data, method = nil, request_content_type = '') ⇒ APIRequest
Returns a new instance of APIRequest.
7 8 9 10 11 12 |
# File 'lib/mimic/api_request.rb', line 7 def initialize(data, method = nil, request_content_type = '') @data = data @method = (method || "GET") @stubs = [] @request_content_type = request_content_type end |
Instance Attribute Details
#request_content_type ⇒ Object (readonly)
Returns the value of attribute request_content_type.
5 6 7 |
# File 'lib/mimic/api_request.rb', line 5 def request_content_type @request_content_type end |
Class Method Details
.from_request(request, method = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mimic/api_request.rb', line 31 def self.from_request(request, method = nil) case request.content_type when /json/ data = JSON.parse(request.body.string) when /plist/ data = Plist.parse_xml(request.body.string) else data = request.body.string && request.body.string.length >= 2 ? JSON.parse(request.body.string) : {} end new(data, method, request.content_type) end |
Instance Method Details
#response ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mimic/api_request.rb', line 18 def response response = {"stubs" => @stubs.map(&:to_hash)} case request_content_type when /json/ response.to_json when /plist/ response.to_plist else response.to_json end end |
#setup_stubs_on(host) ⇒ Object
44 45 46 47 48 |
# File 'lib/mimic/api_request.rb', line 44 def setup_stubs_on(host) (@data["stubs"] || [@data]).each do |stub_data| @stubs << Stub.new(stub_data, stub_data['method'] || @method).on(host) end end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/mimic/api_request.rb', line 14 def to_s @data.inspect end |