Class: Tightknit::Client

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

Overview

The Client class is the main entry point for interacting with the Tightknit API. It handles the HTTP connection and provides access to the various API resources.

Examples:

Creating a client

client = Tightknit::Client.new(api_key: "your_api_key")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ Client

Initialize a new Tightknit API client

Parameters:

  • api_key (String)

    The API key to use for authentication

Raises:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tightknit/client.rb', line 21

def initialize(api_key:)
  @api_key = api_key

  raise Error, "API key is required" unless @api_key

  @conn = Faraday.new(url: BASE_URL) do |faraday|
    faraday.headers["Authorization"] = "Bearer #{@api_key}"
    faraday.headers["Content-Type"] = "application/json"
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#connFaraday::Connection (readonly)

Returns The Faraday connection object used for HTTP requests.

Returns:

  • (Faraday::Connection)

    The Faraday connection object used for HTTP requests



15
16
17
# File 'lib/tightknit/client.rb', line 15

def conn
  @conn
end

Instance Method Details

#calendar_eventsTightknit::Resources::CalendarEvents

Access the calendar events resource

Returns:



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

def calendar_events
  @calendar_events ||= Resources::CalendarEvents.new(self)
end

#feedsTightknit::Resources::Feeds

Access the feeds resource

Returns:



43
44
45
# File 'lib/tightknit/client.rb', line 43

def feeds
  @feeds ||= Resources::Feeds.new(self)
end

#get(path, params = {}) ⇒ Hash

Make a GET request to the API

Parameters:

  • path (String)

    The path to request

  • params (Hash) (defaults to: {})

    The query parameters to include

Returns:

  • (Hash)

    The parsed JSON response

Raises:



53
54
55
56
57
58
# File 'lib/tightknit/client.rb', line 53

def get(path, params = {})
  response = @conn.get(path, params)
  handle_response(response)
rescue Faraday::Error => e
  handle_error(e)
end

#handle_error(error) ⇒ Object (private)

Handle Faraday errors

Parameters:

  • error (Faraday::Error)

    The Faraday error

Raises:



87
88
89
# File 'lib/tightknit/client.rb', line 87

def handle_error(error)
  raise Error, "Network Error: #{error.message}"
end

#handle_response(response) ⇒ Hash (private)

Handle the API response

Parameters:

  • response (Faraday::Response)

    The Faraday response object

Returns:

  • (Hash)

    The parsed JSON response

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tightknit/client.rb', line 67

def handle_response(response)
  case response.status
  when 200..299
    JSON.parse(response.body, symbolize_names: true)
  else
    error_message = begin
      error_data = JSON.parse(response.body, symbolize_names: true)
      error_data[:message] || error_data[:error] || "Unknown error"
    rescue JSON::ParserError
      response.body || "Unknown error"
    end

    raise Error, "API Error (#{response.status}): #{error_message}"
  end
end