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, environment: :production, sandbox: false, 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 https://api.rerout.co.

  • environment (Symbol) (defaults to: :production)

    production or sandbox data plane.

  • sandbox (Boolean) (defaults to: false)

    convenience alias for the sandbox environment.

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rerout/client.rb', line 62

def initialize(api_key:, base_url: nil, environment: :production, sandbox: false,
               connection: nil, timeout: 30, user_agent: nil)
  validate_api_key!(api_key)
  is_sandbox = resolve_sandbox!(api_key, environment, sandbox)

  @api_key = api_key
  @base_url = (base_url || (is_sandbox ? SANDBOX_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)
  @webhooks = Resources::Webhooks.new(self)
  @conversions = Resources::Conversions.new(self)
  @tags = Resources::Tags.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.



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

def base_url
  @base_url
end

#conversionsResources::Conversions (readonly)

Returns conversion tracking namespace.

Returns:



50
51
52
# File 'lib/rerout/client.rb', line 50

def conversions
  @conversions
end

Returns link namespace.

Returns:



42
43
44
# File 'lib/rerout/client.rb', line 42

def links
  @links
end

#projectResources::Project (readonly)

Returns project namespace.

Returns:



44
45
46
# File 'lib/rerout/client.rb', line 44

def project
  @project
end

#qrResources::Qr (readonly)

Returns QR namespace.

Returns:



46
47
48
# File 'lib/rerout/client.rb', line 46

def qr
  @qr
end

#tagsResources::Tags (readonly)

Returns tag management namespace.

Returns:



52
53
54
# File 'lib/rerout/client.rb', line 52

def tags
  @tags
end

#webhooksResources::Webhooks (readonly)

Returns webhook endpoint management namespace.

Returns:



48
49
50
# File 'lib/rerout/client.rb', line 48

def webhooks
  @webhooks
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.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rerout/client.rb', line 91

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