Class: WhosGotDirt::Result
- Inherits:
-
Object
- Object
- WhosGotDirt::Result
- Defined in:
- lib/whos_got_dirt/result.rb
Overview
A result from a response.
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#add_link! ⇒ Hash
Adds an API URL for the result.
-
#add_source! ⇒ Hash
Adds the requested URL as a source.
-
#finalize! ⇒ Hash
Adds the requested URL as a source, validates the result against its schema, and returns the updated result.
-
#initialize(type, result, response) ⇒ Result
constructor
Sets the result and response.
-
#validate!(opts = {}) ⇒ Hash
Validates the result against its schema.
Constructor Details
#initialize(type, result, response) ⇒ Result
Sets the result and response.
21 22 23 24 25 |
# File 'lib/whos_got_dirt/result.rb', line 21 def initialize(type, result, response) @type = type @result = result @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
10 11 12 |
# File 'lib/whos_got_dirt/result.rb', line 10 def response @response end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
6 7 8 |
# File 'lib/whos_got_dirt/result.rb', line 6 def result @result end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/whos_got_dirt/result.rb', line 14 def type @type end |
Instance Method Details
#add_link! ⇒ Hash
Adds an API URL for the result.
30 31 32 33 34 35 36 |
# File 'lib/whos_got_dirt/result.rb', line 30 def add_link! result['links'] ||= [] result['links'] << { 'url' => response.item_url(result), 'note' => response.class.name.rpartition('::')[2], } end |
#add_source! ⇒ Hash
Adds the requested URL as a source.
41 42 43 44 45 46 47 48 |
# File 'lib/whos_got_dirt/result.rb', line 41 def add_source! result['sources'] ||= [] result['sources'] << { 'url' => response.env.url.to_s, 'note' => response.class.name.rpartition('::')[2], } result end |
#finalize! ⇒ Hash
Adds the requested URL as a source, validates the result against its schema, and returns the updated result.
111 112 113 114 115 116 |
# File 'lib/whos_got_dirt/result.rb', line 111 def finalize! add_link! add_source! validate! result end |
#validate!(opts = {}) ⇒ Hash
Validates the result against its schema. If recognized errors occur, they are corrected, and the result is revalidated; if any error occurs during revalidation, an exception is raised.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/whos_got_dirt/result.rb', line 60 def validate!(opts = {}) # The code assumes that processing errors in reverse avoids re-indexing # issues when deleting items from arrays. If this assumption is invalid, # we can delete items one at a time and re-validate using this skeleton: # # begin # Validator.validate(result, type) # rescue JSON::Schema::ValidationError => e # error = e.to_hash # case error[:failed_attribute] # when 'Properties' # ... # validate! # ... # end # end errors = Validator.validate(result, type) if opts[:strict] && errors.any? raise ValidationError.new(errors * '\n') end errors.reverse.each do |error| pointer = JsonPointer.new(result, error[:fragment][1..-1]) case error.fetch(:failed_attribute) when 'Properties' # The property did not contain a required property. This should be due # to the source having a null value. pointer.delete when 'Type' # The property did not match one or more types. This should be due to # the source having an integer instead of string value. pointer.value = pointer.value.to_s else raise ValidationError.new("#{error.fetch(:message)} (#{pointer.value})") end end if errors.any? validate!(strict: true) end result end |