Skip to content

Commit ebf45f6

Browse files
committed
Refactor FeedbackFace client to improve error handling and request structure; update local configuration for API base URL
1 parent 0fb5df1 commit ebf45f6

3 files changed

Lines changed: 51 additions & 29 deletions

File tree

lib/feedbackface.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module FeedbackFace
88
class Error < StandardError; end
99
class AuthenticationError < Error; end
1010
class ValidationError < Error; end
11+
class NotFound < Error; end
12+
class RateLimit < Error; end
1113

1214
class << self
1315
def configure

lib/feedbackface/client.rb

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Client
55
def initialize(api_key: nil, account_id: nil)
66
@api_key = api_key || FeedbackFace.config.api_key
77
@account_id = account_id || FeedbackFace.config.account_id
8-
raise AuthenticationError, "API key is required" unless @api_key
8+
raise FeedbackFace::AuthenticationError, "API key is required" unless @api_key
99
end
1010

1111
# Get current user information
@@ -17,46 +17,65 @@ def me
1717
def create_customer(attributes)
1818
raise ArgumentError, "Account ID is required" unless @account_id
1919

20-
response = post("/accounts/#{@account_id}/customers", customer: attributes)
20+
response = post("/accounts/#{@account_id}/customers", body: { customer: attributes })
2121
Customer.new(response["customer"].merge("account_id" => @account_id))
2222
end
2323

2424
private
2525

26-
def get(path)
27-
request = Net::HTTP::Get.new(path)
28-
make_request(request)
26+
def get(path, **options)
27+
make_request(klass: Net::HTTP::Get, path: path, **options)
2928
end
3029

31-
def post(path, data)
32-
request = Net::HTTP::Post.new(path)
33-
make_request(request, data)
30+
def post(path, **options)
31+
make_request(klass: Net::HTTP::Post, path: path, **options)
3432
end
3533

36-
def make_request(request, data = nil)
37-
request["Authorization"] = "Bearer #{@api_key}"
38-
request["Content-Type"] = "application/json"
39-
request.body = data.to_json if data
34+
def make_request(klass:, path:, headers: {}, body: nil, query: nil)
35+
uri = path.start_with?("http") ? URI(path) : URI("#{FeedbackFace.config.api_base_url}#{path}")
4036

41-
uri = URI(FeedbackFace.config.api_base_url + request.path)
42-
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
43-
http.request(request)
37+
http = Net::HTTP.new(uri.host, uri.port)
38+
http.use_ssl = uri.scheme == "https"
39+
40+
all_headers = default_headers.merge(headers)
41+
all_headers.delete("Content-Type") if klass == Net::HTTP::Get
42+
43+
request = klass.new(uri.request_uri, all_headers)
44+
45+
if body
46+
request.body = body.to_json
4447
end
4548

46-
handle_response(response)
49+
handle_response(http.request(request))
50+
end
51+
52+
def default_headers
53+
{
54+
"Accept" => "application/json",
55+
"Content-Type" => "application/json",
56+
"Authorization" => "Bearer #{api_key}"
57+
}
4758
end
4859

4960
def handle_response(response)
50-
case response
51-
when Net::HTTPSuccess
52-
JSON.parse(response.body)
53-
when Net::HTTPUnauthorized
54-
raise AuthenticationError, "Invalid API key"
55-
when Net::HTTPUnprocessableEntity
56-
error = JSON.parse(response.body)
57-
raise ValidationError, error["error"] || error["errors"].to_s
61+
case response.code
62+
when "200", "201", "202", "203", "204"
63+
response.body.empty? ? {} : JSON.parse(response.body)
64+
when "401"
65+
raise FeedbackFace::AuthenticationError, "Invalid API key"
66+
when "404"
67+
raise FeedbackFace::NotFound, "Resource not found"
68+
when "422"
69+
begin
70+
error = JSON.parse(response.body)
71+
raise FeedbackFace::ValidationError, error["error"] || error["errors"].to_s
72+
rescue JSON::ParserError
73+
raise FeedbackFace::ValidationError, response.body
74+
end
75+
when "429"
76+
raise FeedbackFace::RateLimit, "Rate limit exceeded. Please try again later."
5877
else
59-
raise Error, "Unexpected error: #{response.code} - #{response.body}"
78+
raise FeedbackFace::Error, "Unexpected error: #{response.code} - #{response.body}"
6079
end
6180
end
6281
end

test_local.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
FeedbackFace.configure do |config|
88
config.api_key = ENV['FEEDBACKFACE_API_KEY'] || 'your_api_key_here'
99
config.account_id = ENV['FEEDBACKFACE_ACCOUNT_ID'] || 'acct_123'
10-
config.api_base_url = 'http://localhost:3000'
10+
config.api_base_url = 'http://feedbackface.test:3000/api/v1'
1111
end
1212

1313
def test_me_endpoint
@@ -81,9 +81,10 @@ def test_create_customer
8181
end
8282

8383
puts
84-
puts "Environment variables:"
85-
puts " FEEDBACKFACE_API_KEY: #{ENV['FEEDBACKFACE_API_KEY'] ? '✅ Set' : '❌ Not set'}"
86-
puts " FEEDBACKFACE_ACCOUNT_ID: #{ENV['FEEDBACKFACE_ACCOUNT_ID'] ? '✅ Set' : '❌ Not set (will use default)'}"
84+
puts "Configuration:"
85+
puts " API Key: #{FeedbackFace.config.api_key}"
86+
puts " Account ID: #{FeedbackFace.config.account_id}"
87+
puts " API Base URL: #{FeedbackFace.config.api_base_url}"
8788
puts
8889

8990
success_count = 0

0 commit comments

Comments
 (0)