Class: Oss::Client
- Inherits:
-
Object
- Object
- Oss::Client
- Defined in:
- lib/oss/client.rb
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
readonly
Returns the value of attribute access_key_id.
-
#access_key_secret ⇒ Object
readonly
Returns the value of attribute access_key_secret.
-
#debug_mode ⇒ Object
Returns the value of attribute debug_mode.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
-
#delete(options = {}) ⇒ Object
http delete request.
-
#get(options = {}) ⇒ Object
http get request.
-
#head(options = {}) ⇒ Object
http delete request.
-
#initialize(endpoint, access_key_id, access_key_secret, debug_mode = false) ⇒ Client
constructor
A new instance of Client.
-
#options(options = {}) ⇒ Object
http delete request.
-
#post(options = {}) ⇒ Object
http post request.
-
#put(options = {}) ⇒ Object
http put request.
- #request(host, path, headers, as = :xml, &block) ⇒ Object
- #request_prepare(method, options) ⇒ Object
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_id ⇒ Object (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_secret ⇒ Object (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_mode ⇒ Object
Returns the value of attribute debug_mode.
14 15 16 |
# File 'lib/oss/client.rb', line 14 def debug_mode @debug_mode end |
#endpoint ⇒ Object (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( = {}) request_prepare(:delete, ) end |
#get(options = {}) ⇒ Object
http get request
29 30 31 |
# File 'lib/oss/client.rb', line 29 def get( = {}) request_prepare(:get, ) end |
#head(options = {}) ⇒ Object
http delete request
44 45 46 |
# File 'lib/oss/client.rb', line 44 def head( = {}) request_prepare(:head, ) end |
#options(options = {}) ⇒ Object
http delete request
49 50 51 |
# File 'lib/oss/client.rb', line 49 def ( = {}) request_prepare(:options, ) end |
#post(options = {}) ⇒ Object
http post request
34 35 36 |
# File 'lib/oss/client.rb', line 34 def post( = {}) request_prepare(:post, ) end |
#put(options = {}) ⇒ Object
http put request
24 25 26 |
# File 'lib/oss/client.rb', line 24 def put( = {}) request_prepare(:put, ) 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, ) # set header sign [:sign_configs] ||= Hash.new [:sign_configs][:date] = gmt_date [:sign_configs][:host] = [:host] [:sign_configs][:path] = [:path] [:sign_configs][:verb] = method.to_s.upcase # content md5 check if need if [:content_md5_check] # payload make md digest md5 = Digest::MD5.digest([:payload]) # then make base64 and trim \n cm = Base64.encode64(md5).gsub("\n", '') [:sign_configs][:content_md5] = cm # set to header [:headers] ||= Hash.new [:headers]['Content-MD5'] = cm end # set header content length if need if [:content_length_check] [:payload] ||= '' [:headers]['Content-Length'] = [:payload].bytesize end # set get path query string path = Util.set_query_string([:path], [:query_string]) # add query string to sign if need if [:sign_configs][:sign_query_string] [:sign_configs][:path] = path end # create sign header signed_header = set_header_sign([:headers], [:sign_configs]) # send request request([:host], path, signed_header, [:as] || :xml) do |url, header| # use RestClient send request RestClient::Request.execute(url: url, method: method, headers: header, payload: [:payload]) end end |