Class: Rerout::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rerout/client.rb

Overview

Main entry point — construct one of these per project API key and re-use it across requests. Thread-safe so long as the injected Faraday connection is thread-safe (Faraday’s default ‘Net::HTTP` adapter is).

Examples:

rerout = Rerout::Client.new(api_key: ENV.fetch('REROUT_API_KEY'))
link = rerout.links.create(Rerout::CreateLinkInput.new(target_url: 'https://example.com'))
puts link.short_url

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: nil, connection: nil, timeout: 30, user_agent: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    project API key (‘rrk_…`). Required.

  • base_url (String, nil) (defaults to: nil)

    override base URL. Defaults to ‘api.rerout.co`.

  • connection (Faraday::Connection, nil) (defaults to: nil)

    inject a Faraday connection (useful for the test adapter or for sharing connection pools).

  • timeout (Integer) (defaults to: 30)

    per-request timeout in seconds. Default 30.

  • user_agent (String, nil) (defaults to: nil)

    override the default ‘User-Agent` header.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rerout/client.rb', line 46

def initialize(api_key:, base_url: nil, connection: nil, timeout: 30, user_agent: nil)
  if api_key.nil? || !api_key.is_a?(String) || api_key.strip.empty?
    raise Error.new(
      code: 'missing_api_key',
      message: 'A project API key is required to construct Rerout::Client.',
      status: 0
    )
  end

  @api_key = api_key
  @base_url = (base_url || DEFAULT_BASE_URL).to_s.sub(%r{/+\z}, '')
  @timeout = timeout
  @user_agent = user_agent || "rerout-ruby/#{Rerout::VERSION}"
  @connection = connection || default_connection

  @links = Resources::Links.new(self)
  @project = Resources::Project.new(self)
  @qr = Resources::Qr.new(self)
end

Instance Attribute Details

#base_urlString (readonly)

Returns resolved base URL with trailing slashes stripped.

Returns:

  • (String)

    resolved base URL with trailing slashes stripped.



31
32
33
# File 'lib/rerout/client.rb', line 31

def base_url
  @base_url
end

Returns link namespace.

Returns:



34
35
36
# File 'lib/rerout/client.rb', line 34

def links
  @links
end

#projectResources::Project (readonly)

Returns project namespace.

Returns:



36
37
38
# File 'lib/rerout/client.rb', line 36

def project
  @project
end

#qrResources::Qr (readonly)

Returns QR namespace.

Returns:



38
39
40
# File 'lib/rerout/client.rb', line 38

def qr
  @qr
end

Instance Method Details

#request(method:, path:, query: nil, body: nil, raw: false) ⇒ Hash, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform a JSON request against the Rerout API.

Parameters:

  • method (Symbol)

    :get, :post, :patch, :delete

  • path (String)

    starts with ‘/`, includes any path params.

  • query (Hash, nil) (defaults to: nil)

    query string params.

  • body (Object, nil) (defaults to: nil)

    body to be JSON-encoded.

Returns:

  • (Hash, Array, String, nil)

    parsed JSON body, raw text for non-JSON success bodies that the caller opted into via ‘raw: true`, or nil for 204 No Content.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rerout/client.rb', line 76

def request(method:, path:, query: nil, body: nil, raw: false)
  headers = base_headers
  payload = nil
  if body
    payload = JSON.generate(body)
    headers['Content-Type'] = 'application/json'
  end

  response = perform_request(method: method, path: path, query: query,
                             headers: headers, payload: payload)

  handle_response(response, raw: raw)
end