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
- #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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/api/client.rb', line 78 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.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
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/api/client.rb', line 95 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
63 64 65 66 67 68 69 |
# File 'lib/api/client.rb', line 63 def delete(method, params={}, ={}) begin parse_response RestClient.delete(append_params(url(method), add_params(method, params))), rescue RestClient::BadRequest => ex raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s end end |
#get(method, params = {}, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/api/client.rb', line 28 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 => ex # puts ex.http_body raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s end end |
#headers ⇒ Object
107 108 109 110 |
# File 'lib/api/client.rb', line 107 def headers user_agent = "Appoxy API Ruby Client" headers = {'User-Agent' => user_agent} end |
#parse_response(response, options = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/api/client.rb', line 113 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
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/api/client.rb', line 40 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 => ex # puts ex.http_body raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s end end |
#put(method, body, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/api/client.rb', line 52 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 => ex # puts ex.http_body raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s end end |
#url(command_path) ⇒ Object
72 73 74 75 |
# File 'lib/api/client.rb', line 72 def url(command_path) url = host + command_path url end |