Class: Appoxy::Api::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @host = host
  @access_key = access_key
  @secret_key = secret_key
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



19
20
21
# File 'lib/api/client.rb', line 19

def access_key
  @access_key
end

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/api/client.rb', line 19

def host
  @host
end

#secret_keyObject

Returns the value of attribute secret_key.



19
20
21
# File 'lib/api/client.rb', line 19

def secret_key
  @secret_key
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/api/client.rb', line 19

def version
  @version
end

Class Method Details

.loggerObject



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.generate_timestamp(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={}, options={})
  begin
    parse_response RestClient.delete(append_params(url(method), add_params(method, params))), options
  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={}, options={})
  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), options
  rescue RestClient::BadRequest, RestClient::InternalServerError => ex
    process_ex(ex)
  end
end

#headersObject



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, options={})
  unless options[: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={}, options={})
  begin
    parse_response RestClient.post(url(method), add_params(method, params).to_json, headers), options
    #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={}, options={})
  begin
    parse_response RestClient.post(url(method), add_params(method, params).merge!({:file=>file}), :multipart => true), options
  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.message+":"+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, options={})
  begin
    parse_response RestClient.put(url(method), add_params(method, body).to_json, headers), options
    #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