Class: HookSniff::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#endpointsObject (readonly)

Returns the value of attribute endpoints.



7
8
9
# File 'lib/hooksniff/client.rb', line 7

def endpoints
  @endpoints
end

#webhooksObject (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, parse_error_message(response)
  when 401
    raise AuthenticationError, parse_error_message(response)
  when 404
    raise NotFoundError, parse_error_message(response)
  when 413
    raise PayloadTooLargeError, parse_error_message(response)
  when 429
    raise RateLimitError, parse_error_message(response)
  else
    raise Error, "HTTP #{response.code}: #{parse_error_message(response)}"
  end
end

#statsObject

Get platform statistics



18
19
20
21
# File 'lib/hooksniff/client.rb', line 18

def stats
  resp = request(:get, "/stats")
  Models::Stats.new(resp)
end