Module: Fog::InternetArchive

Extended by:
Provider
Defined in:
lib/fog/internet_archive.rb,
lib/fog/internet_archive/version.rb,
lib/fog/internet_archive/signaturev4.rb

Defined Under Namespace

Classes: Mock, SignatureV4

Constant Summary collapse

DOMAIN_NAME =
'archive.org'
API_DOMAIN_NAME =
's3.us.' + DOMAIN_NAME
COMPLIANT_BUCKET_NAMES =
/^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\-(?![\.])){1,61}[a-z0-9]$/
VERSION =
"0.0.2".freeze

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ Object



72
73
74
75
76
# File 'lib/fog/internet_archive.rb', line 72

def self.escape(string)
  string.gsub(/([^a-zA-Z0-9_.\-~]+)/) {
    "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase
  }
end

.indexed_filters(filters) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fog/internet_archive.rb', line 59

def self.indexed_filters(filters)
  params = {}
  filters.keys.each_with_index do |key, key_index|
    key_index += 1
    params[format('Filter.%d.Name', key_index)] = key
    [*filters[key]].each_with_index do |value, value_index|
      value_index += 1
      params[format('Filter.%d.Value.%d', key_index, value_index)] = value
    end
  end
  params
end

.indexed_param(key, values) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/internet_archive.rb', line 16

def self.indexed_param(key, values)
  params = {}
  unless key.include?('%d')
    key << '.%d'
  end
  [*values].each_with_index do |value, index|
    if value.respond_to?('keys')
      k = format(key, index + 1)
      value.each do | vkey, vvalue |
        params["#{k}.#{vkey}"] = vvalue
      end
    else
      params[format(key, index + 1)] = value
    end
  end
  params
end

.indexed_request_param(name, values) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/fog/internet_archive.rb', line 51

def self.indexed_request_param(name, values)
  idx = -1
  Array(values).reduce({}) do |params, value|
    params["#{name}.#{idx += 1}"] = value
    params
  end
end

.parse_security_group_options(group_name, options) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/fog/internet_archive.rb', line 268

def self.parse_security_group_options(group_name, options)
  options ||= Hash.new
  if group_name.is_a?(Hash)
    options = group_name
  elsif group_name
    if options.key?('GroupName')
      raise Fog::Compute::InternetArchive::Error, 'Arguments specified both group_name and GroupName in options'
    end
    options = options.clone
    options['GroupName'] = group_name
  end
  name_specified = options.key?('GroupName') && !options['GroupName'].nil?
  group_id_specified = options.key?('GroupId') && !options['GroupId'].nil?
  unless name_specified || group_id_specified
    raise Fog::Compute::InternetArchive::Error, 'Neither GroupName nor GroupId specified'
  end
  if name_specified && group_id_specified
    options.delete('GroupName')
  end
  options
end

.serialize_keys(key, value, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/internet_archive.rb', line 34

def self.serialize_keys(key, value, options = {})
  case value
  when Hash
    value.each do | k, v |
      options.merge!(serialize_keys("#{key}.#{k}", v))
    end
    return options
  when Array
    value.each_with_index do | it, idx |
      options.merge!(serialize_keys("#{key}.member.#{(idx + 1)}", it))
    end
    return options
  else
    return {key => value}
  end
end

.signed_params(params, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fog/internet_archive.rb', line 78

def self.signed_params(params, options = {})
  params.merge!({
                  'AWSAccessKeyId'    => options[:ia_access_key_id],
                  'SignatureMethod'   => 'HmacSHA256',
                  'SignatureVersion'  => '2',
                  'Timestamp'         => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
                  'Version'           => options[:version]
                })

  params.merge!({
                  'SecurityToken'     => options[:ia_session_token]
                }) if options[:ia_session_token]

  body = ''
  for key in params.keys.sort
    unless (value = params[key]).nil?
      body << "#{key}=#{escape(value.to_s)}&"
    end
  end
  string_to_sign = "POST\n#{options[:host]}:#{options[:port]}\n#{options[:path]}\n" << body.chop
  signed_string = options[:hmac].sign(string_to_sign)
  body << "Signature=#{escape(Base64.encode64(signed_string).chomp!)}"

  body
end