Class: Tightknit::Client
- Inherits:
-
Object
- Object
- Tightknit::Client
- 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.
Instance Attribute Summary collapse
-
#conn ⇒ Faraday::Connection
readonly
The Faraday connection object used for HTTP requests.
Instance Method Summary collapse
-
#calendar_events ⇒ Tightknit::Resources::CalendarEvents
Access the calendar events resource.
-
#feeds ⇒ Tightknit::Resources::Feeds
Access the feeds resource.
-
#get(path, params = {}) ⇒ Hash
Make a GET request to the API.
-
#handle_error(error) ⇒ Object
private
Handle Faraday errors.
-
#handle_response(response) ⇒ Hash
private
Handle the API response.
-
#initialize(api_key:) ⇒ Client
constructor
Initialize a new Tightknit API client.
Constructor Details
#initialize(api_key:) ⇒ Client
Initialize a new Tightknit API client
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
#conn ⇒ Faraday::Connection (readonly)
Returns 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_events ⇒ Tightknit::Resources::CalendarEvents
Access the calendar events resource
36 37 38 |
# File 'lib/tightknit/client.rb', line 36 def calendar_events @calendar_events ||= Resources::CalendarEvents.new(self) end |
#feeds ⇒ Tightknit::Resources::Feeds
Access the feeds resource
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
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
87 88 89 |
# File 'lib/tightknit/client.rb', line 87 def handle_error(error) raise Error, "Network Error: #{error.}" end |
#handle_response(response) ⇒ Hash (private)
Handle the API response
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 = 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}): #{}" end end |