Class: HookSniff::Client
- Inherits:
-
Object
- Object
- HookSniff::Client
- Defined in:
- lib/hooksniff/client.rb
Instance Attribute Summary collapse
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
-
#webhooks ⇒ Object
readonly
Returns the value of attribute webhooks.
Instance Method Summary collapse
-
#initialize(api_key:, base_url: nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
- #request(method, path, body: nil) ⇒ Object
-
#stats ⇒ Object
Get platform statistics.
Constructor Details
#initialize(api_key:, base_url: nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 |
# File 'lib/hooksniff/client.rb', line 9 def initialize(api_key:, base_url: nil, timeout: nil) @api_key = api_key @base_url = (base_url || DEFAULT_BASE_URL).chomp("/") @timeout = timeout || DEFAULT_TIMEOUT @endpoints = EndpointsResource.new(self) @webhooks = WebhooksResource.new(self) end |
Instance Attribute Details
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
7 8 9 |
# File 'lib/hooksniff/client.rb', line 7 def endpoints @endpoints end |
#webhooks ⇒ Object (readonly)
Returns the value of attribute webhooks.
7 8 9 |
# File 'lib/hooksniff/client.rb', line 7 def webhooks @webhooks end |
Instance Method Details
#request(method, path, body: nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/hooksniff/client.rb', line 24 def request(method, path, body: nil) uri = URI("#{@base_url}#{path}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") http.open_timeout = @timeout http.read_timeout = @timeout case method when :get req = Net::HTTP::Get.new(uri) when :post req = Net::HTTP::Post.new(uri) when :delete req = Net::HTTP::Delete.new(uri) else raise ArgumentError, "Unsupported HTTP method: #{method}" end req["Authorization"] = "Bearer #{@api_key}" req["Content-Type"] = "application/json" req["User-Agent"] = "hooksniff-ruby/#{VERSION}" req.body = JSON.generate(body) if body response = http.request(req) case response.code.to_i when 200..299 content_type = response["content-type"] || "" if content_type.include?("text/csv") response.body else JSON.parse(response.body) rescue response.body end when 400 raise ValidationError, (response) when 401 raise AuthenticationError, (response) when 404 raise NotFoundError, (response) when 413 raise PayloadTooLargeError, (response) when 429 raise RateLimitError, (response) else raise Error, "HTTP #{response.code}: #{(response)}" end end |