Class: Tavily::Object

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Object

Returns a new instance of Object.

Parameters:

  • attributes (Hash) (defaults to: {})

    parsed response body



38
39
40
# File 'lib/tavily/object.rb', line 38

def initialize(attributes = {})
  @attributes = attributes.is_a?(Hash) ? attributes : {}
end

Instance Attribute Details

#attributesHash (readonly)

Returns the raw, parsed response body with string keys.

Returns:

  • (Hash)

    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.

Parameters:

  • name (Symbol)

    method name to define

  • key (String) (defaults to: name.to_s)

    the key in the raw hash (defaults to name)

  • wrap (Class, String, nil) (defaults to: nil)

    wrap the value in this Tavily::Object subclass (may be given as a String to allow forward references)

  • collection (Boolean) (defaults to: false)

    when true, treat the value as an array and wrap each element



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).

Parameters:

  • key (String, Symbol)

Returns:



45
46
47
# File 'lib/tavily/object.rb', line 45

def [](key)
  @attributes[key.to_s]
end

#dig(*keys) ⇒ Object?

Returns:

See Also:

  • Hash#dig


51
52
53
# File 'lib/tavily/object.rb', line 51

def dig(*keys)
  @attributes.dig(*keys.map(&:to_s))
end

#hashObject



72
73
74
# File 'lib/tavily/object.rb', line 72

def hash
  [self.class, @attributes].hash
end

#inspectObject



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.

Parameters:

  • key (String, Symbol)

Returns:

  • (Boolean)

    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_hHash Also known as: to_hash

Returns the raw response body.

Returns:

  • (Hash)

    the raw response body



62
63
64
# File 'lib/tavily/object.rb', line 62

def to_h
  @attributes
end