Class: S3::Client::API::RestParameter

Inherits:
Object
  • Object
show all
Defined in:
lib/s3/client/api/rest_parameter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, resource, cano_resource: nil, query_params: {}, parameters: {}, bucket: '', content_type: nil, import: false, raw_data: false, blank_body: false, headers: {}, multipart: false) ⇒ RestParameter

Returns a new instance of RestParameter.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/s3/client/api/rest_parameter.rb', line 5

def initialize(method, resource, cano_resource: nil, query_params: {},
               parameters: {}, bucket: '', content_type: nil, import: false,
               raw_data: false, blank_body: false, headers: {}, multipart: false)

  @method = method
  @resource = resource
  @cano_resource = cano_resource
  @query_params = query_params
  @parameters = parameters
  @bucket = bucket
  @content_type = content_type
  @import = import
  @raw_data = raw_data
  @blank_body = blank_body
  @headers = headers
  @multipart = multipart
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



28
29
30
# File 'lib/s3/client/api/rest_parameter.rb', line 28

def bucket
  @bucket
end

#cano_resourceObject (readonly)

Returns the value of attribute cano_resource.



25
26
27
# File 'lib/s3/client/api/rest_parameter.rb', line 25

def cano_resource
  @cano_resource
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



29
30
31
# File 'lib/s3/client/api/rest_parameter.rb', line 29

def content_type
  @content_type
end

#headersObject (readonly)

Returns the value of attribute headers.



30
31
32
# File 'lib/s3/client/api/rest_parameter.rb', line 30

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



23
24
25
# File 'lib/s3/client/api/rest_parameter.rb', line 23

def method
  @method
end

#parametersObject (readonly)

Returns the value of attribute parameters.



27
28
29
# File 'lib/s3/client/api/rest_parameter.rb', line 27

def parameters
  @parameters
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



26
27
28
# File 'lib/s3/client/api/rest_parameter.rb', line 26

def query_params
  @query_params
end

#resourceObject (readonly)

Returns the value of attribute resource.



24
25
26
# File 'lib/s3/client/api/rest_parameter.rb', line 24

def resource
  @resource
end

Instance Method Details

#authentication(access_key_id, secret_access_key, force_path_style) ⇒ Object



74
75
76
77
# File 'lib/s3/client/api/rest_parameter.rb', line 74

def authentication(access_key_id, secret_access_key, force_path_style)

  "AWS" + " " + access_key_id + ":" + signature(secret_access_key, force_path_style)
end

#blank_body?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/s3/client/api/rest_parameter.rb', line 131

def blank_body?
  @blank_body
end

#calc_dateObject



112
113
114
115
116
117
# File 'lib/s3/client/api/rest_parameter.rb', line 112

def calc_date
  return @date if @date
  @date = Time.now.httpdate

  @date
end

#canonicalized_resource(force_path_style = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/s3/client/api/rest_parameter.rb', line 94

def canonicalized_resource(force_path_style = false)
  result = ''

  if @bucket.present?
    result = '/'
    result += "#{@bucket}/"
  end

  if @bucket.blank? || @resource != '/'
    result = File.join(result, @resource)
  end

  result += '?' if @cano_resource.present?
  result += @cano_resource if @cano_resource

  result
end

#http_verbObject



59
60
61
# File 'lib/s3/client/api/rest_parameter.rb', line 59

def http_verb
  @method.to_s.upcase
end

#import?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/s3/client/api/rest_parameter.rb', line 119

def import?
  @import
end

#multipart?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/s3/client/api/rest_parameter.rb', line 123

def multipart?
  @multipart
end

#raw_data?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/s3/client/api/rest_parameter.rb', line 127

def raw_data?
  @raw_data
end

#signature(secret_access_key, force_path_style = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/s3/client/api/rest_parameter.rb', line 79

def signature(secret_access_key, force_path_style = false)
  http_verb = "#{self.http_verb}\n"
  content_md5 = "\n"
  content_type = signature_content_type
  date = "#{calc_date}\n"

  canonicalized_aws_headers = ""

  string_to_sign = http_verb + content_md5 + content_type + date +
      canonicalized_aws_headers + canonicalized_resource(force_path_style)

  digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret_access_key, string_to_sign)
  Base64.encode64(digest).strip
end

#signature_content_typeObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/s3/client/api/rest_parameter.rb', line 63

def signature_content_type
  result = ""
  if @content_type.present?
    result << @content_type
  end

  result << "\n"

  result
end

#to_sObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/s3/client/api/rest_parameter.rb', line 135

def to_s
  [
    "method:#{@method}",
    "resource: #{@resource}",
    "cano_resource: #{@cano_resource}",
    "query_params: #{@query_params}",
    "bucket: #{@bucket}",
    "parameters: #{@parameters}",
    "headers: #{@headers}"
  ].join(", ")
end

#url(uri, force_path_style = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/s3/client/api/rest_parameter.rb', line 32

def url(uri, force_path_style = false)
  url = uri.host
  url += ":#{uri.port}" unless uri.port == 80 || uri.port == 443

  if @bucket.present?
    if force_path_style
      url += '/' unless url.end_with? "/"
      url += @bucket
    else
      url = [@bucket, url].join('.')
      url += '/' unless url.end_with? "/"
    end
  end

  if @bucket.blank? || @resource != '/'
    url = File.join(url, @resource)
  end

  url += '/' if url.split('/').last == @bucket
  url += '?' if @cano_resource.present? || @query_params.present?
  url += @cano_resource if @cano_resource
  url += '&' if @cano_resource.present? && @query_params.present?
  url += "#{@query_params.to_param}" if @query_params.present?

  uri.scheme + '://' + url
end