Class: AuthenticImage::Client

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

Constant Summary collapse

PATH_PATTERN =
%r{
  (?<prefix>[\w_\-/]+)/
  (?<identifier>[\w~%_\-\.]+)/
  (?<region>[\w!,:]+)/
  (?<size>[\w!,:]+)/
  (?<rotation>[\w!]+)/
  (?<quality>[\w]+)\.
  (?<format>[\w]+)
}x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, prefix: "/iiif/2", signing_key:, digest: "sha1") ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
# File 'lib/authentic_image/client.rb', line 20

def initialize(host:, prefix: "/iiif/2", signing_key:, digest: "sha1")
  @host        = host
  @prefix      = prefix
  @signing_key = signing_key
  @digest      = digest
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



27
28
29
# File 'lib/authentic_image/client.rb', line 27

def digest
  @digest
end

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/authentic_image/client.rb', line 27

def host
  @host
end

#prefixObject (readonly)

Returns the value of attribute prefix.



27
28
29
# File 'lib/authentic_image/client.rb', line 27

def prefix
  @prefix
end

#signing_keyObject (readonly)

Returns the value of attribute signing_key.



27
28
29
# File 'lib/authentic_image/client.rb', line 27

def signing_key
  @signing_key
end

Instance Method Details

#parse_url(url) ⇒ Object



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
# File 'lib/authentic_image/client.rb', line 42

def parse_url(url)
  uri = URI(url)

  path_match = PATH_PATTERN.match(uri.path)
  return unless path_match

  bucket, key = CGI.unescape(path_match[:identifier]).split("/", 2)

  params  = CGI.parse(uri.query)
  version = params["ver"].first if params["ver"]

  Request.new(
    host:        "#{uri.scheme}://#{uri.host}",
    prefix:      path_match[:prefix],
    signing_key: signing_key,
    bucket:      bucket,
    key:         key,
    size:        path_match[:size],
    format:      path_match[:format],
    region:      path_match[:region],
    rotation:    path_match[:rotation],
    quality:     path_match[:quality],
    version:     version
  )
end

#request(**kwargs) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/authentic_image/client.rb', line 29

def request(**kwargs)
  Request.new(
    host:        host,
    prefix:      prefix,
    signing_key: signing_key,
    **kwargs
  )
end

#url(**kwargs) ⇒ Object



38
39
40
# File 'lib/authentic_image/client.rb', line 38

def url(**kwargs)
  request(**kwargs).url
end

#valid?(url) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
# File 'lib/authentic_image/client.rb', line 68

def valid?(url)
  request = parse_url(url)
  return false unless request

  uri = URI(url)
  params = CGI.parse(uri.query)
  return false unless params["sig"]

  params["sig"].first == request.signature
end