Class: Tavily::Object
- Inherits:
-
Object
- Object
- Tavily::Object
- Defined in:
- lib/tavily/object.rb
Overview
Base class for every response object returned by the client. Wraps the parsed JSON hash, exposing typed accessors (declared with Object.attribute) while remaining forward-compatible: any field the API adds is still reachable via #[], #dig, and #to_h.
Direct Known Subclasses
CrawlResponse, CrawlResult, ExtractResponse, ExtractResult, FailedResult, Image, MapResponse, ResearchSource, ResearchTask, SearchResponse, SearchResult, Usage
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The raw, parsed response body with string keys.
Class Method Summary collapse
-
.attribute(name, key: name.to_s, wrap: nil, collection: false) ⇒ void
Declare a typed accessor for a response field.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#[](key) ⇒ Object?
Fetch a raw field by name (String or Symbol).
- #dig(*keys) ⇒ Object?
- #hash ⇒ Object
-
#initialize(attributes = {}) ⇒ Object
constructor
A new instance of Object.
- #inspect ⇒ Object
-
#key?(key) ⇒ Boolean
Whether the raw field is present.
-
#to_h ⇒ Hash
(also: #to_hash)
The raw response body.
Constructor Details
#initialize(attributes = {}) ⇒ Object
Returns a new instance of Object.
38 39 40 |
# File 'lib/tavily/object.rb', line 38 def initialize(attributes = {}) @attributes = attributes.is_a?(Hash) ? attributes : {} end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns the raw, parsed response body with string keys.
10 11 12 |
# File 'lib/tavily/object.rb', line 10 def attributes @attributes end |
Class Method Details
.attribute(name, key: name.to_s, wrap: nil, collection: false) ⇒ void
This method returns an undefined value.
Declare a typed accessor for a response field.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tavily/object.rb', line 21 def self.attribute(name, key: name.to_s, wrap: nil, collection: false) define_method(name) do raw = @attributes[key] return raw if wrap.nil? klass = wrap.is_a?(Class) ? wrap : Tavily.const_get(wrap) if collection Array(raw).map { |item| item.is_a?(Hash) ? klass.new(item) : item } elsif raw.is_a?(Hash) klass.new(raw) else raw end end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
67 68 69 |
# File 'lib/tavily/object.rb', line 67 def ==(other) other.is_a?(self.class) && other.attributes == attributes end |
#[](key) ⇒ Object?
Fetch a raw field by name (String or Symbol).
45 46 47 |
# File 'lib/tavily/object.rb', line 45 def [](key) @attributes[key.to_s] end |
#dig(*keys) ⇒ Object?
51 52 53 |
# File 'lib/tavily/object.rb', line 51 def dig(*keys) @attributes.dig(*keys.map(&:to_s)) end |
#hash ⇒ Object
72 73 74 |
# File 'lib/tavily/object.rb', line 72 def hash [self.class, @attributes].hash end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/tavily/object.rb', line 76 def inspect "#<#{self.class.name} #{@attributes.inspect}>" end |
#key?(key) ⇒ Boolean
Returns whether the raw field is present.
57 58 59 |
# File 'lib/tavily/object.rb', line 57 def key?(key) @attributes.key?(key.to_s) end |
#to_h ⇒ Hash Also known as: to_hash
Returns the raw response body.
62 63 64 |
# File 'lib/tavily/object.rb', line 62 def to_h @attributes end |