Class: Qrdot::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url: "https://api.qrdot.dev") ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/qrdot/client.rb', line 7

def initialize(api_key, base_url: "https://api.qrdot.dev")
  unless api_key.start_with?("sk_test_", "sk_live_")
    raise ArgumentError, "api_key must start with sk_live_ (legacy sk_test_ accepted)"
  end

  @api_key = api_key
  @base_url = base_url.sub(%r{/\z}, "")
  @qr = QrResource.new(self)
  @assets = AssetsResource.new(self)
  @analytics = AnalyticsResource.new(self)
  @webhooks = WebhooksResource.new(self)
  @domains = DomainsResource.new(self)
end

Instance Attribute Details

#analyticsObject (readonly)

Returns the value of attribute analytics.



5
6
7
# File 'lib/qrdot/client.rb', line 5

def analytics
  @analytics
end

#assetsObject (readonly)

Returns the value of attribute assets.



5
6
7
# File 'lib/qrdot/client.rb', line 5

def assets
  @assets
end

#domainsObject (readonly)

Returns the value of attribute domains.



5
6
7
# File 'lib/qrdot/client.rb', line 5

def domains
  @domains
end

#qrObject (readonly)

Returns the value of attribute qr.



5
6
7
# File 'lib/qrdot/client.rb', line 5

def qr
  @qr
end

#webhooksObject (readonly)

Returns the value of attribute webhooks.



5
6
7
# File 'lib/qrdot/client.rb', line 5

def webhooks
  @webhooks
end

Instance Method Details

#create_qr(payload, idempotency_key: nil) ⇒ Object

Convenience — same as qr.create



22
23
24
# File 'lib/qrdot/client.rb', line 22

def create_qr(payload, idempotency_key: nil)
  qr.create(payload, idempotency_key: idempotency_key)
end

#put_external(url, body, headers) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/qrdot/client.rb', line 39

def put_external(url, body, headers)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  req = Net::HTTP::Put.new(uri)
  headers.each { |k, v| req[k] = v }
  req.body = body
  res = http.request(req)
  res.code.to_i
end

#raw(method, path, body:, headers:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/qrdot/client.rb', line 50

def raw(method, path, body:, headers:)
  uri = URI("#{@base_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = 30
  http.read_timeout = 60

  req = Net::HTTPGenericRequest.new(
    method,
    !body.nil?,
    true,
    uri.request_uri
  )
  req["Authorization"] = "Bearer #{@api_key}"
  req["Accept"] = "application/json"
  headers.each { |k, v| req[k] = v }
  if body
    req["Content-Type"] = "application/json"
    req.body = JSON.generate(body)
  end

  res = http.request(req)
  status = res.code.to_i
  if status < 200 || status >= 300
    code = "internal"
    message = res.message
    begin
      parsed = JSON.parse(res.body.to_s)
      code = parsed.dig("error", "code") || code
      message = parsed.dig("error", "message") || message
    rescue JSON::ParserError
      # ignore
    end
    raise ApiError.new(code, message, status)
  end
  [res.body.to_s, status, res["content-type"]]
end

#request(method, path, body: nil, headers: {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/qrdot/client.rb', line 26

def request(method, path, body: nil, headers: {})
  data, status, = raw(method, path, body: body, headers: headers)
  return nil if status == 204 || data.nil? || data.empty?

  JSON.parse(data)
end

#request_bytes(method, path, body: nil) ⇒ Object



33
34
35
36
37
# File 'lib/qrdot/client.rb', line 33

def request_bytes(method, path, body: nil)
  data, _status, ct = raw(method, path, body: body, headers: {})
  mime = (ct || "application/octet-stream").split(";").first.strip
  [data, mime]
end