Class: Appoxy::Api::Client
- Inherits:
-
Object
- Object
- Appoxy::Api::Client
- Defined in:
- lib/api/client.rb
Overview
Subclass must define:
host: endpoint url for service
Constant Summary collapse
- @@logger =
Logger.new(STDOUT)
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#host ⇒ Object
Returns the value of attribute host.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #add_params(command_path, hash) ⇒ Object
- #append_params(host, params) ⇒ Object
- #delete(method, params = {}, options = {}) ⇒ Object
- #get(method, params = {}, options = {}) ⇒ Object
- #headers ⇒ Object
-
#initialize(host, access_key, secret_key, options = {}) ⇒ Client
constructor
A new instance of Client.
- #parse_response(response, options = {}) ⇒ Object
- #post(method, params = {}, options = {}) ⇒ Object
- #post_file(method, file, params = {}, options = {}) ⇒ Object
- #process_ex(ex) ⇒ Object
- #put(method, body, options = {}) ⇒ Object
- #url(command_path) ⇒ Object
Constructor Details
#initialize(host, access_key, secret_key, options = {}) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 |
# File 'lib/api/client.rb', line 21 def initialize(host, access_key, secret_key, ={}) @host = host @access_key = access_key @secret_key = secret_key end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
19 20 21 |
# File 'lib/api/client.rb', line 19 def access_key @access_key end |
#host ⇒ Object
Returns the value of attribute host.
19 20 21 |
# File 'lib/api/client.rb', line 19 def host @host end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
19 20 21 |
# File 'lib/api/client.rb', line 19 def secret_key @secret_key end |
#version ⇒ Object
Returns the value of attribute version.
19 20 21 |
# File 'lib/api/client.rb', line 19 def version @version end |
Class Method Details
.logger ⇒ Object
15 16 17 |
# File 'lib/api/client.rb', line 15 def self.logger @@logger end |
Instance Method Details
#add_params(command_path, hash) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/api/client.rb', line 83 def add_params(command_path, hash) v = version||"0.1" ts = Appoxy::Api::Signatures.(Time.now.gmtime) # puts 'timestamp = ' + ts sig = case v when "0.3"#only for ssl version Appoxy::Api::Signatures.generate_signature("", ts, secret_key) when "0.2" Appoxy::Api::Signatures.generate_signature(command_path + Appoxy::Api::Signatures.hash_to_s(hash), ts, secret_key) when "0.1" Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key) end extra_params = {'sigv'=>v, 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key} hash.merge!(extra_params) end |
#append_params(host, params) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/api/client.rb', line 100 def append_params(host, params) host += "?" i = 0 params.each_pair do |k, v| host += "&" if i > 0 host += k + "=" + CGI.escape(v) i +=1 end return host end |
#delete(method, params = {}, options = {}) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/api/client.rb', line 70 def delete(method, params={}, ={}) begin parse_response RestClient.delete(append_params(url(method), add_params(method, params))), rescue RestClient::BadRequest, RestClient::InternalServerError => ex process_ex(ex) end end |
#get(method, params = {}, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/api/client.rb', line 34 def get(method, params={}, ={}) begin # ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params) parse_response RestClient.get(append_params(url(method), add_params(method, params)), headers), rescue RestClient::BadRequest, RestClient::InternalServerError => ex process_ex(ex) end end |
#headers ⇒ Object
111 112 113 114 |
# File 'lib/api/client.rb', line 111 def headers user_agent = "Appoxy API Ruby Client" headers = {'User-Agent' => user_agent} end |
#parse_response(response, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/api/client.rb', line 116 def parse_response(response, ={}) unless [:parse] == false begin return ActiveSupport::JSON.decode(response.to_s) rescue => ex puts 'response that caused error = ' + response.to_s raise ex end else response end end |
#post(method, params = {}, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/api/client.rb', line 51 def post(method, params={}, ={}) begin parse_response RestClient.post(url(method), add_params(method, params).to_json, headers), #ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params) rescue RestClient::BadRequest, RestClient::InternalServerError => ex process_ex(ex) end end |
#post_file(method, file, params = {}, options = {}) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/api/client.rb', line 43 def post_file(method, file, params={}, ={}) begin parse_response RestClient.post(url(method), add_params(method, params).merge!({:file=>file}), :multipart => true), rescue RestClient::BadRequest, RestClient::InternalServerError => ex process_ex(ex) end end |
#process_ex(ex) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/api/client.rb', line 27 def process_ex(ex) decoded_ex = ActiveSupport::JSON.decode(ex.http_body) exception = Exception.new(ex.+":"+decoded_ex["msg"]) exception.set_backtrace(decoded_ex["backtrace"].split(",")) if decoded_ex["backtrace"] raise exception end |
#put(method, body, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/api/client.rb', line 61 def put(method, body, ={}) begin parse_response RestClient.put(url(method), add_params(method, body).to_json, headers), #ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil) rescue RestClient::BadRequest, RestClient::InternalServerError => ex process_ex(ex) end end |
#url(command_path) ⇒ Object
78 79 80 81 |
# File 'lib/api/client.rb', line 78 def url(command_path) url = host + command_path url end |