Class: FakeAPI::Generator
- Inherits:
-
Object
- Object
- FakeAPI::Generator
- Defined in:
- lib/fakeapi/generator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(name) ⇒ Generator
constructor
A new instance of Generator.
- #write_json(body) ⇒ Object
- #write_text(body) ⇒ Object
Constructor Details
#initialize(name) ⇒ Generator
Returns a new instance of Generator.
11 12 13 |
# File 'lib/fakeapi/generator.rb', line 11 def initialize(name) @name = name end |
Class Method Details
.generate(*args) ⇒ Object
7 8 9 |
# File 'lib/fakeapi/generator.rb', line 7 def self.generate(*args) new(*args).generate end |
Instance Method Details
#generate ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fakeapi/generator.rb', line 15 def generate hash = VCRReader.read("vcr/#{@name}.yml") http_method = hash['request']['method'] url = URI(hash['request']['uri']).path status = hash['response']['status']['code'] content_type = hash['response']['headers']['Content-Type'].first body = hash['response']['body']['string'] body = if content_type =~ /json/i write_json(body) else write_text(body) end <<RUBY #{http_method} '#{url}' do status #{status} content_type '#{content_type}' body #{body} end RUBY end |
#write_json(body) ⇒ Object
40 41 42 43 |
# File 'lib/fakeapi/generator.rb', line 40 def write_json(body) require_relative './json_writer' JSONWriter.write(JSON.parse(body, symbolize_names: true).to_hash) end |
#write_text(body) ⇒ Object
45 46 47 |
# File 'lib/fakeapi/generator.rb', line 45 def write_text(body) '"' + body + '"' end |