Class: Rerout::Resources::Qr

Inherits:
Object
  • Object
show all
Defined in:
lib/rerout/qr.rb

Overview

QR helpers.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Qr

Returns a new instance of Qr.

Parameters:



11
12
13
# File 'lib/rerout/qr.rb', line 11

def initialize(client)
  @client = client
end

Class Method Details

.build_url(base_url:, code:, options: nil) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rerout/qr.rb', line 44

def self.build_url(base_url:, code:, options: nil)
  raise ArgumentError, 'code is required' if code.nil? || code.to_s.empty?

  qopts = coerce_options(options)
  base = base_url.to_s.sub(%r{/+\z}, '')
  path = "/v1/links/#{ERB::Util.url_encode(code.to_s)}/qr"
  url = "#{base}#{path}"
  query = qopts.to_query_hash
  return url if query.empty?

  "#{url}?#{URI.encode_www_form(query)}"
end

.coerce_options(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
# File 'lib/rerout/qr.rb', line 58

def self.coerce_options(options)
  case options
  when nil then QrOptions.new
  when QrOptions then options
  when Hash then QrOptions.new(**options.transform_keys(&:to_sym))
  else
    raise ArgumentError, 'options must be a Rerout::QrOptions, Hash, or nil'
  end
end

Instance Method Details

#svg(code, options = nil) ⇒ String

Fetch the QR as an SVG string. Attaches the bearer token.

Parameters:

Returns:

  • (String)

    the rendered SVG markup.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rerout/qr.rb', line 30

def svg(code, options = nil)
  raise ArgumentError, 'code is required' if code.nil? || code.to_s.empty?

  qopts = Qr.coerce_options(options)
  path = "/v1/links/#{ERB::Util.url_encode(code.to_s)}/qr"
  @client.request(
    method: :get,
    path: path,
    query: qopts.to_query_hash,
    raw: true
  )
end

#url(code, options = nil) ⇒ String

Build the URL the API serves the QR SVG from. Pure — does not hit the network.

Parameters:

Returns:

  • (String)


21
22
23
# File 'lib/rerout/qr.rb', line 21

def url(code, options = nil)
  Qr.build_url(base_url: @client.base_url, code: code, options: options)
end