Class: ElectionBuddy::Client

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

Overview

HTTP client for the ElectionBuddy API

Examples:

client = Client.new(api_key: "your-api-key")
client.voter_list.validate(123)

Constant Summary collapse

BASE_URL =
"https://secure.electionbuddy.com/api/v2"

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, adapter: Faraday.default_adapter, stubs: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

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

    The API key for authentication (optional if configured globally)

  • adapter (Object, nil) (defaults to: Faraday.default_adapter)

    The HTTP client adapter to use (optional, defaults to Faraday's default adapter)

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

    Test stubs for the adapter (optional)



16
17
18
19
20
# File 'lib/election_buddy/client.rb', line 16

def initialize(api_key: nil, adapter: Faraday.default_adapter, stubs: nil)
  @api_key = api_key || ElectionBuddy.configuration.api_key
  @adapter = adapter
  @stubs = stubs
end

Instance Method Details

#connectionObject (private)



31
32
33
34
35
36
37
38
# File 'lib/election_buddy/client.rb', line 31

def connection
  @connection ||= Faraday.new(BASE_URL) do |conn|
    conn.headers["authorization"] = @api_key
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.adapter @adapter, @stubs
  end
end

#voter_listVoterListResource

Returns the voter list resource for handling voter-related operations

Returns:



25
26
27
# File 'lib/election_buddy/client.rb', line 25

def voter_list
  VoterListResource.new(connection)
end