Class: TLAW::Endpoint

Inherits:
APIPath show all
Defined in:
lib/tlaw/endpoint.rb

Overview

This class does all the hard work: actually calling some HTTP API and processing responses.

Each real API endpoint is this class descendant, defining its own params and response processors. On each call small instance of this class is created, #call-ed and dies as you don't need it anymore.

Typically, you will neither create nor use endpoint descendants or instances directly:

  • endpoint class definition is performed through DSL helpers,
  • and then, containing namespace obtains .<current_endpoint_name>() method, which is (almost) everything you need to know.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIPath

describe, param_set

Constructor Details

#initialize(**parent_params) ⇒ Endpoint

Creates endpoint class (or descendant) instance. Typically, you never use it directly.

Params defined in parent namespace are passed here.



78
79
80
81
82
83
84
85
86
# File 'lib/tlaw/endpoint.rb', line 78

def initialize(**parent_params)
  super

  @client = Faraday.new do |faraday|
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.adapter Faraday.default_adapter
  end
  @url_template = self.class.construct_template
end

Instance Attribute Details

#url_templateObject (readonly)

Returns the value of attribute url_template.



71
72
73
# File 'lib/tlaw/endpoint.rb', line 71

def url_template
  @url_template
end

Class Method Details

.inspectObject

Inspects endpoint class prettily.

Example:

some_api.some_namespace.endpoints[:my_endpoint]
# => <SomeApi::SomeNamespace::MyEndpoint call-sequence: my_endpoint(param1, param2: nil), docs: .describe>


31
32
33
34
# File 'lib/tlaw/endpoint.rb', line 31

def inspect
  "#<#{name || '(unnamed endpoint class)'}:" \
  " call-sequence: #{symbol}(#{param_set.to_code}); docs: .describe>"
end

Instance Method Details

#call(**params) ⇒ Hash, Array

Does the real call to the API, with all params passed to this method and to parent namespace.

Typically, you don't use it directly, that's what called when you do some_namespace.endpoint_name(**params).

Returns:

  • (Hash, Array)

    Parsed, flattened and post-processed response body.



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tlaw/endpoint.rb', line 96

def call(**params)
  url = construct_url(**full_params(params))

  @client.get(url)
         .tap { |response| guard_errors!(response) }
         .derp { |response| self.class.parse(response.body) }
rescue API::Error
  raise # Not catching in the next block
rescue => e
  raise unless url
  raise API::Error, "#{e.class} at #{url}: #{e.message}"
end