Class: Rack::APIMock
- Inherits:
-
Object
- Object
- Rack::APIMock
- Defined in:
- lib/rack/apimock.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, apidir: 'api') ⇒ APIMock
constructor
A new instance of APIMock.
Constructor Details
#initialize(app, apidir: 'api') ⇒ APIMock
Returns a new instance of APIMock.
5 6 7 8 |
# File 'lib/rack/apimock.rb', line 5 def initialize(app, apidir: 'api') @app = app @apidir = apidir end |
Instance Method Details
#call(env) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rack/apimock.rb', line 10 def call(env) @status, @headers = 200, {} file_path = get_template_path(env) # Missing Template File return @app.call(env) unless file_path response = ERB.new(::File.open(file_path, &:read), nil, '<>').result(binding) unless env['QUERY_STRING'].empty? query_string = Rack::Utils.parse_nested_query(env['QUERY_STRING']) case env["CONTENT_TYPE"] when /application\/json/i require 'json' data = JSON.parse(response) response = query_string.each_with_object({}) {|(key, _), new_hash| new_hash.merge!(key => data[key]) }.to_json end end [@status, @headers, [response]] end |