Class: TwelvedataRuby::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/twelvedata_ruby/request.rb

Constant Summary collapse

DEFAULT_HTTP_VERB =

Default HTTP method for API requests

:get

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **query_params) ⇒ Request

Initialize a new request

Parameters:

  • name (Symbol, String)

    Endpoint name

  • query_params (Hash)

    Query parameters for the request



21
22
23
# File 'lib/twelvedata_ruby/request.rb', line 21

def initialize(name, **query_params)
  @endpoint = Endpoint.new(name, **query_params)
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



15
16
17
# File 'lib/twelvedata_ruby/request.rb', line 15

def endpoint
  @endpoint
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check equality with another request

Parameters:

  • other (Request)

    Request to compare with

Returns:

  • (Boolean)

    True if requests are equivalent



113
114
115
116
117
# File 'lib/twelvedata_ruby/request.rb', line 113

def ==(other)
  return false unless other.is_a?(self.class)

  name == other.name && query_params == other.query_params
end

#fetchResponse, ...

Send the request using the client

Returns:



28
29
30
# File 'lib/twelvedata_ruby/request.rb', line 28

def fetch
  Client.instance.fetch(self)
end

#full_urlString?

Get the complete URL for this request

Returns:

  • (String, nil)

    Full URL or nil if invalid



62
63
64
65
66
# File 'lib/twelvedata_ruby/request.rb', line 62

def full_url
  return nil unless valid?

  "#{Client::BASE_URL}/#{relative_url}"
end

#hashInteger

Generate hash code for request

Returns:

  • (Integer)

    Hash code



123
124
125
# File 'lib/twelvedata_ruby/request.rb', line 123

def hash
  [name, query_params].hash
end

#http_verbSymbol?

Get the HTTP verb for this request

Returns:

  • (Symbol, nil)

    HTTP verb or nil if invalid



35
36
37
38
39
# File 'lib/twelvedata_ruby/request.rb', line 35

def http_verb
  return nil unless valid?

  endpoint.definition[:http_verb] || DEFAULT_HTTP_VERB
end

#inspectString

Detailed inspection of the request

Returns:

  • (String)

    Detailed request information



105
106
107
# File 'lib/twelvedata_ruby/request.rb', line 105

def inspect
  "#<#{self.class.name}:#{object_id} endpoint=#{name} valid=#{valid?} params=#{query_params.keys}>"
end

#paramsHash?

Get request parameters formatted for HTTP client

Returns:

  • (Hash, nil)

    Parameters hash or nil if invalid



44
45
46
47
48
# File 'lib/twelvedata_ruby/request.rb', line 44

def params
  return nil unless valid?

  { params: endpoint.query_params }
end

#relative_urlString?

Get the relative URL path for this request

Returns:

  • (String, nil)

    URL path or nil if invalid



53
54
55
56
57
# File 'lib/twelvedata_ruby/request.rb', line 53

def relative_url
  return nil unless valid?

  name.to_s
end

#to_aArray? Also known as: build

Convert request to array format for HTTPX

Returns:

  • (Array, nil)

    Request as array or nil if invalid



84
85
86
87
88
# File 'lib/twelvedata_ruby/request.rb', line 84

def to_a
  return nil unless valid?

  [http_verb.to_s.upcase, full_url, params]
end

#to_hHash?

Convert request to hash representation

Returns:

  • (Hash, nil)

    Request as hash or nil if invalid



71
72
73
74
75
76
77
78
79
# File 'lib/twelvedata_ruby/request.rb', line 71

def to_h
  return nil unless valid?

  {
    http_verb: http_verb,
    url: full_url,
    params: query_params,
  }
end

#to_sString

String representation of the request

Returns:

  • (String)

    Human-readable request description



94
95
96
97
98
99
100
# File 'lib/twelvedata_ruby/request.rb', line 94

def to_s
  if valid?
    "#{http_verb.to_s.upcase} #{full_url} with params: #{query_params}"
  else
    "Invalid request for endpoint: #{name}"
  end
end