Class: Oss::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, access_key_id, access_key_secret, debug_mode = false) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
# File 'lib/oss/client.rb', line 16

def initialize(endpoint, access_key_id, access_key_secret, debug_mode = false)
  @debug_mode        = debug_mode
  @endpoint          = endpoint
  @access_key_id     = access_key_id
  @access_key_secret = access_key_secret
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



13
14
15
# File 'lib/oss/client.rb', line 13

def access_key_id
  @access_key_id
end

#access_key_secretObject (readonly)

Returns the value of attribute access_key_secret.



13
14
15
# File 'lib/oss/client.rb', line 13

def access_key_secret
  @access_key_secret
end

#debug_modeObject

Returns the value of attribute debug_mode.



14
15
16
# File 'lib/oss/client.rb', line 14

def debug_mode
  @debug_mode
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/oss/client.rb', line 13

def endpoint
  @endpoint
end

Instance Method Details

#delete(options = {}) ⇒ Object

http delete request



39
40
41
# File 'lib/oss/client.rb', line 39

def delete(options = {})
  request_prepare(:delete, options)
end

#get(options = {}) ⇒ Object

http get request



29
30
31
# File 'lib/oss/client.rb', line 29

def get(options = {})
  request_prepare(:get, options)
end

#head(options = {}) ⇒ Object

http delete request



44
45
46
# File 'lib/oss/client.rb', line 44

def head(options = {})
  request_prepare(:head, options)
end

#options(options = {}) ⇒ Object

http delete request



49
50
51
# File 'lib/oss/client.rb', line 49

def options(options = {})
  request_prepare(:options, options)
end

#post(options = {}) ⇒ Object

http post request



34
35
36
# File 'lib/oss/client.rb', line 34

def post(options = {})
  request_prepare(:post, options)
end

#put(options = {}) ⇒ Object

http put request



24
25
26
# File 'lib/oss/client.rb', line 24

def put(options = {})
  request_prepare(:put, options)
end

#request(host, path, headers, as = :xml, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/oss/client.rb', line 101

def request(host, path, headers, as = :xml, &block)
  url = "http://#{host}#{URI::encode(path)}"

  # send request and rescue errors
  begin
    response = yield(url, headers)
  rescue RestClient::ExceptionWithResponse => err
    response = err.response
  end

  p "DEBUG: response body =======>\n#{response.body}\n" if debug_mode

  if response.code/100 != 2
    # error response
    error_handler(response)
  else
    # success response
    case as
      when :xml
        # parse as xml object
        Nokogiri::XML(response.body)
      when :raw
        # return raw response
        response
      else
        raise 'Undefined request as param!'
    end
  end

end

#request_prepare(method, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/oss/client.rb', line 53

def request_prepare(method, options)
  # set header sign
  options[:sign_configs] ||= Hash.new
  options[:sign_configs][:date] = gmt_date
  options[:sign_configs][:host] = options[:host]
  options[:sign_configs][:path] = options[:path]
  options[:sign_configs][:verb] = method.to_s.upcase

  # content md5 check if need
  if options[:content_md5_check]
    # payload make md digest
    md5 = Digest::MD5.digest(options[:payload])

    # then make base64 and trim \n
    cm = Base64.encode64(md5).gsub("\n", '')
    options[:sign_configs][:content_md5] = cm

    # set to header
    options[:headers] ||= Hash.new
    options[:headers]['Content-MD5'] = cm
  end

  # set header content length if need
  if options[:content_length_check]
    options[:payload] ||= ''
    options[:headers]['Content-Length'] = options[:payload].bytesize
  end

  # set get path query string
  path = Util.set_query_string(options[:path], options[:query_string])

  # add query string to sign if need
  if options[:sign_configs][:sign_query_string]
    options[:sign_configs][:path] = path
  end

  # create sign header
  signed_header = set_header_sign(options[:headers], options[:sign_configs])

  # send request
  request(options[:host], path, signed_header, options[:as] || :xml) do |url, header|

    # use RestClient send request
    RestClient::Request.execute(url: url, method: method, headers: header, payload: options[:payload])

  end
end