Class: Rester::Utils::StubFile
- Inherits:
-
Object
- Object
- Rester::Utils::StubFile
- Defined in:
- lib/rester/utils/stub_file.rb
Constant Summary collapse
- DEFAULT_TAGS =
{ 'successful' => 'true' }.freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- ._parse_response_code(verb, tags) ⇒ Object
-
._parse_tags(path, verb, context, resp_key) ⇒ Object
Takes a response key (e.g., “response”) and parses out the tags (e.g., => false).
-
._update_context(path, verb, context, spec) ⇒ Object
Given a context hash, updates it for consumption by the rest of the StubAdapter (i.e., removes tags from “response” key and puts them in a “response_tags” key).
-
._update_request(path, verb, context, spec) ⇒ Object
Converts all the values in the request hash to strings, which mimics how the data will be received on the service side.
-
._update_response(path, verb, context, spec) ⇒ Object
Parses response tags (e.g., response).
- ._validate_tags(path, verb, context, tags) ⇒ Object
-
.parse(path) ⇒ Object
Parses the stub file and returns the data as a hash.
-
.parse!(stub_hash) ⇒ Object
Given a raw stub file hash, converts it to the format used internally.
Instance Method Summary collapse
-
#initialize(path) ⇒ StubFile
constructor
A new instance of StubFile.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object (private)
15 16 17 18 19 20 21 |
# File 'lib/rester/utils/stub_file.rb', line 15 def method_missing(meth, *args, &block) if @_stub.respond_to?(meth) @_stub.public_send(meth, *args, &block) else super end end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/rester/utils/stub_file.rb', line 6 def path @path end |
Class Method Details
._parse_response_code(verb, tags) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/rester/utils/stub_file.rb', line 108 def _parse_response_code(verb, ) if ['successful'] == 'true' (verb == 'POST') ? 201 : 200 else 400 end end |
._parse_tags(path, verb, context, resp_key) ⇒ Object
Takes a response key (e.g., “response”) and parses out the tags (e.g., => false)
94 95 96 97 98 |
# File 'lib/rester/utils/stub_file.rb', line 94 def (path, verb, context, resp_key) DEFAULT_TAGS.merge(Hash[resp_key.scan(/(\w+) *= *(\w+)/)]).tap { || (path, verb, context, ) } end |
._update_context(path, verb, context, spec) ⇒ Object
Given a context hash, updates it for consumption by the rest of the StubAdapter (i.e., removes tags from “response” key and puts them in a “response_tags” key).
50 51 52 53 |
# File 'lib/rester/utils/stub_file.rb', line 50 def _update_context(path, verb, context, spec) _update_request(path, verb, context, spec) _update_response(path, verb, context, spec) end |
._update_request(path, verb, context, spec) ⇒ Object
Converts all the values in the request hash to strings, which mimics how the data will be received on the service side.
58 59 60 |
# File 'lib/rester/utils/stub_file.rb', line 58 def _update_request(path, verb, context, spec) spec['request'] = Utils.stringify(spec['request'] || {}) end |
._update_response(path, verb, context, spec) ⇒ Object
Parses response tags (e.g., response).
Currently supported tags:
successful must be 'true' or 'false'
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rester/utils/stub_file.rb', line 67 def _update_response(path, verb, context, spec) responses = spec.select { |k,_| k =~ /\Aresponse(\[(\w+) *= *(\w+)(, *(\w+) *= *(\w+))*\])?\z/ } if responses.count == 0 fail Errors::StubError, "#{verb.upcase} #{path} is missing a " \ "response for the context #{context.inspect}" elsif responses.count > 1 fail Errors::StubError, "#{verb.upcase} #{path} has too many " \ "responses defined for the context #{context.inspect}" end response_key = responses.keys.first = (path, verb, context, response_key) spec.merge!( 'response' => spec.delete(response_key), 'response_tags' => , 'response_code' => _parse_response_code(verb, ) ) end |
._validate_tags(path, verb, context, tags) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/rester/utils/stub_file.rb', line 100 def (path, verb, context, ) unless ['true', 'false'].include?(['successful']) fail Errors::StubError, '"successful" tag should be either "true" '\ 'or "false" in' "#{verb.upcase} #{path} in context " \ "#{context.inspect}" end end |
.parse(path) ⇒ Object
Parses the stub file and returns the data as a hash
28 29 30 |
# File 'lib/rester/utils/stub_file.rb', line 28 def parse(path) parse!(YAML.load_file(path)) end |
.parse!(stub_hash) ⇒ Object
Given a raw stub file hash, converts it to the format used internally.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rester/utils/stub_file.rb', line 34 def parse!(stub_hash) stub_hash.each do |path, verbs| next if ['version', 'consumer', 'producer'].include?(path) verbs.each do |verb, contexts| contexts.each do |context, spec| _update_context(path, verb, context, spec) end end end end |