Class: Namabar::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Endpoints
Defined in:
lib/namabar/client.rb

Overview

HTTP client for interacting with the Namabar API

This class provides the core HTTP functionality for making requests to the Namabar API. It uses HTTParty for HTTP operations and includes all endpoint methods from the Endpoints module.

The client automatically handles:

  • Base URI configuration

  • Authentication via API key headers

  • Content-Type and Accept headers

  • JSON request/response handling

Examples:

Basic usage

# Configure globally first
Namabar.configure do |config|
  config.api_key = 'your-api-key'
end

# Create and use client
client = Namabar::Client.new
response = client.send_message(
  type: 'sms',
  to: '+1234567890',
  text: 'Hello from Namabar!',
  service_id: 'sms-service'
)
puts response.code # => 200
puts response.body # => parsed JSON response

See Also:

Instance Method Summary collapse

Methods included from Endpoints

#create_verification_code, #get_message, #get_message_status, #get_verification_code_by_id, #send_message, #verify_verification_code

Constructor Details

#initializeClient

Initialize a new Namabar API client

Creates a new client instance using the global Namabar configuration. The client will be configured with the API key and default headers required for authentication and proper JSON communication.

Examples:

Create a client

client = Namabar::Client.new

Raises:

  • (StandardError)

    if Namabar.configuration is nil or api_key is not set

See Also:



56
57
58
59
60
61
62
63
# File 'lib/namabar/client.rb', line 56

def initialize
  @config = Namabar.configuration
  self.class.headers(
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'X-API-Key' => @config.api_key
  )
end

Instance Method Details

#default_optionsHash

Get default HTTP options for requests

Returns a hash containing the default options that should be included with every HTTP request, including headers for authentication and content type.

Examples:

Using default options

opts = client.default_options
opts = opts.merge(query: { limit: 10 })
response = HTTParty.get('/endpoint', opts)

Returns:

  • (Hash)

    hash containing default headers and options



76
77
78
# File 'lib/namabar/client.rb', line 76

def default_options
  { headers: self.class.headers }
end