Class: TinyZap::UrlSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyzap/url_signature.rb

Constant Summary collapse

DIGEST =

Use for ‘OpenSSL::HMAC`

"SHA256".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key:, public_key:) ⇒ UrlSignature

Returns a new instance of UrlSignature.



11
12
13
14
15
# File 'lib/tinyzap/url_signature.rb', line 11

def initialize(secret_key:, public_key:)
  @secret_key = secret_key
  @public_key = public_key
  @path_prefix = File.join("/", "v1", public_key)
end

Instance Attribute Details

#path_prefixObject (readonly)

Returns the value of attribute path_prefix.



9
10
11
# File 'lib/tinyzap/url_signature.rb', line 9

def path_prefix
  @path_prefix
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



9
10
11
# File 'lib/tinyzap/url_signature.rb', line 9

def public_key
  @public_key
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



9
10
11
# File 'lib/tinyzap/url_signature.rb', line 9

def secret_key
  @secret_key
end

Instance Method Details

#signed_url(path) ⇒ Object



17
18
19
# File 'lib/tinyzap/url_signature.rb', line 17

def signed_url(path)
  URI.join("https://tinyzap.com/", signed_path(path))
end

#url_for(path, **query) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tinyzap/url_signature.rb', line 27

def url_for(path, **query)
  path = URI(path).tap do |url|
    url.path = path
    url.query = query.to_query
  end
  signed_url path
end

#valid?(url) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/tinyzap/url_signature.rb', line 21

def valid?(url)
  path = URI(url).request_uri
  version, , signature, path = Regexp.new("/(v1)/(.+?)/(.+?)(/.+)").match(path).captures
  signature == sign(path)
end