Class: Vault8

Inherits:
Object
  • Object
show all
Defined in:
lib/vault8.rb,
lib/vault8/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key, secret_key, service_url) ⇒ Vault8

Returns a new instance of Vault8.



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

def initialize(public_key, secret_key, service_url)
  @public_key = public_key
  @secret_key = secret_key
  @service_url = service_url
end

Instance Attribute Details

#public_keyObject (readonly)

Returns the value of attribute public_key.



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

def public_key
  @public_key
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

#service_urlObject (readonly)

Returns the value of attribute service_url.



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

def service_url
  @service_url
end

Class Method Details

.create!(public_key:, secret_key:, service_url:) ⇒ Object



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

def self.create!(public_key: , secret_key: , service_url:)
  self.new(public_key, secret_key, service_url)
end

Instance Method Details

#encode_token(path:, current_time: nil, until_time: nil) ⇒ Object



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

def encode_token(path:, current_time: nil, until_time: nil)
  Digest::SHA1.hexdigest([public_key, secret_key, path, current_time, until_time].compact.join('|')).reverse
end

#generate_url_for(path:, current_time: nil, until_time: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/vault8.rb', line 44

def generate_url_for(path:, current_time: nil, until_time: nil)
  uri = URI.join(service_url, path)
  uri.query = { p: public_key,
                s: encode_token(path: path, current_time: current_time, until_time: until_time),
                time: current_time,
                until: until_time
              }.reduce([]) {|acc, x| acc << "#{x.first}=#{x.last}" unless x.last.nil?; acc}.join('&')
  uri.to_s
end

#image_path(uid, filters = [], image_name = 'image.jpg') ⇒ Object



31
32
33
# File 'lib/vault8.rb', line 31

def image_path(uid, filters=[], image_name='image.jpg')
  '/' + [uid, merged_filters(filters), image_name].compact.join('/')
end

#image_url(uid:, filters: [], image_name: 'image.jpg', current_time: nil, until_time: nil) ⇒ Object



17
18
19
20
21
# File 'lib/vault8.rb', line 17

def image_url(uid:, filters: [], image_name: 'image.jpg', current_time: nil, until_time: nil)
  generate_url_for( path: image_path(uid, filters, image_name),
                    current_time: (current_time && current_time.to_i),
                    until_time: (until_time && until_time.to_i) )
end

#merged_filters(filters = []) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/vault8.rb', line 35

def merged_filters(filters=[])
  return nil if filters.empty?
  filters.flat_map do |filter|
    filter.map do |k, v|
      [k, v.to_s.empty? ? nil : v].compact.join('-')
    end
  end.join(',')
end

#upload_url(path: '/upload', current_time: Time.current, until_time: Time.current + 86400) ⇒ Object



23
24
25
# File 'lib/vault8.rb', line 23

def upload_url(path: '/upload', current_time: Time.current, until_time: Time.current + 86400)
  generate_url_for(path: path, current_time: current_time, until_time: until_time)
end