appoxy_api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +1 -0
- data/lib/api/api_controller.rb +80 -0
- data/lib/api/client.rb +41 -0
- data/lib/api/client_helper.rb +109 -0
- data/lib/api/signatures.rb +22 -0
- data/lib/appoxy_api.rb +5 -0
- metadata +78 -0
data/README.markdown
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Nada.
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Appoxy
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# The api controllers that use this should set:
|
6
|
+
# protect_from_forgery :only => [] # can add methods to here, eg: :create, :update, :destroy
|
7
|
+
|
8
|
+
# rescue_from SigError, :with => :send_error
|
9
|
+
# rescue_from Api::ApiError, :with => :send_error
|
10
|
+
# before_filter :verify_signature(params)
|
11
|
+
|
12
|
+
# Your Controller must define a sig_should method which will return the signature to compare against.
|
13
|
+
|
14
|
+
module ApiController
|
15
|
+
|
16
|
+
def verify_signature(params, operation)
|
17
|
+
|
18
|
+
# puts 'params in base=' + params.inspect
|
19
|
+
|
20
|
+
access_key = params["access_key"]
|
21
|
+
sigv = params["sigv"]
|
22
|
+
timestamp = params["timestamp"]
|
23
|
+
sig = params["sig"]
|
24
|
+
|
25
|
+
raise Api::SigError, "No access_key" if access_key.nil?
|
26
|
+
raise Api::SigError, "No sigv" if sigv.nil?
|
27
|
+
raise Api::SigError, "No timestamp" if timestamp.nil?
|
28
|
+
raise Api::SigError, "No sig" if sig.nil?
|
29
|
+
|
30
|
+
# todo: verify sig! Use SimpleWorker::HttpEnabled signature methods
|
31
|
+
sig2 = Appoxy::Api::Signatures.generate_signature(operation, timestamp, @project.secret_key)
|
32
|
+
|
33
|
+
raise Api::ApiError, "Invalid signature!" unless sig_should == sig2
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def sig_should
|
38
|
+
raise "You didn't define a sig_should method in your controller!"
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def send_ok(msg={})
|
43
|
+
response_as_string = '' # in case we want to add debugging or something
|
44
|
+
respond_to do |format|
|
45
|
+
# format.json { render :json=>msg }
|
46
|
+
response_as_string = render_to_string :json => msg
|
47
|
+
render :json => response_as_string
|
48
|
+
end
|
49
|
+
true
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def send_error(statuscode_or_error, msg)
|
54
|
+
exc = nil
|
55
|
+
if statuscode_or_error.is_a? Exception
|
56
|
+
exc = statuscode_or_error
|
57
|
+
statuscode_or_error = 400
|
58
|
+
msg = exc.message
|
59
|
+
end
|
60
|
+
# deprecate status, should use status_code
|
61
|
+
json_msg = {"status_code"=>statuscode_or_error, "msg"=>msg}
|
62
|
+
render :json=>json_msg, :status=>statuscode_or_error
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
class ApiError < StandardError
|
70
|
+
|
71
|
+
def initialize(msg=nil)
|
72
|
+
super(msg)
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
data/lib/api/client.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Appoxy
|
2
|
+
module Api
|
3
|
+
|
4
|
+
# Subclass must define:
|
5
|
+
# host: endpoint url for service
|
6
|
+
class Client
|
7
|
+
|
8
|
+
attr_accessor :access_key, :secret_key
|
9
|
+
|
10
|
+
def initialize(host, access_key, secret_key, options={})
|
11
|
+
@access_key = access_key
|
12
|
+
@secret_key = secret_key
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(method, params={}, options={})
|
17
|
+
parse_response ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def post(method, params={}, options={})
|
21
|
+
parse_response ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
|
22
|
+
end
|
23
|
+
|
24
|
+
def put(method, body, options={})
|
25
|
+
parse_response ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_response(response)
|
29
|
+
begin
|
30
|
+
return ActiveSupport::JSON.decode(response)
|
31
|
+
rescue => ex
|
32
|
+
puts 'response that caused error = ' + response.to_s
|
33
|
+
raise ex
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module Appoxy
|
2
|
+
module Api
|
3
|
+
module ClientHelper
|
4
|
+
|
5
|
+
# TODO: SWAP OUT ALL THIS CRAP AND USE REST-CLIENT INSTEAD
|
6
|
+
|
7
|
+
# body is a hash
|
8
|
+
def self.run_http(host, access_key, secret_key, http_method, command_path, body=nil, parameters={}, extra_headers=nil)
|
9
|
+
ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
|
10
|
+
# puts 'timestamp = ' + ts
|
11
|
+
sig = Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
|
12
|
+
# puts "My signature = " + sig
|
13
|
+
url = host + command_path
|
14
|
+
# puts url
|
15
|
+
|
16
|
+
user_agent = "Ruby Client"
|
17
|
+
headers = {'User-Agent' => user_agent}
|
18
|
+
|
19
|
+
if !extra_headers.nil?
|
20
|
+
extra_headers.each_pair do |k, v|
|
21
|
+
headers[k] = v
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
|
26
|
+
if http_method == :put
|
27
|
+
body.update(extra_params)
|
28
|
+
else
|
29
|
+
parameters = {} if parameters.nil?
|
30
|
+
parameters.update(extra_params)
|
31
|
+
# puts 'params=' + parameters.inspect
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
uri = URI.parse(url)
|
37
|
+
#puts 'body=' + body.to_s
|
38
|
+
if (http_method == :put)
|
39
|
+
req = Net::HTTP::Put.new(uri.path)
|
40
|
+
body = ActiveSupport::JSON.encode(body)
|
41
|
+
req.body = body unless body.nil?
|
42
|
+
elsif (http_method == :post)
|
43
|
+
req = Net::HTTP::Post.new(uri.path)
|
44
|
+
if !parameters.nil?
|
45
|
+
req.set_form_data(parameters)
|
46
|
+
else
|
47
|
+
req.body = body unless body.nil?
|
48
|
+
end
|
49
|
+
elsif (http_method == :delete)
|
50
|
+
req = Net::HTTP::Delete.new(uri.path)
|
51
|
+
if !parameters.nil?
|
52
|
+
req.set_form_data(parameters)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
req = Net::HTTP::Get.new(uri.path)
|
56
|
+
if !parameters.nil?
|
57
|
+
req.set_form_data(parameters)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
headers.each_pair do |k, v|
|
61
|
+
req[k] = v
|
62
|
+
end
|
63
|
+
# req.each_header do |k, v|
|
64
|
+
# puts 'header ' + k + '=' + v
|
65
|
+
#end
|
66
|
+
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
67
|
+
http.request(req)
|
68
|
+
end
|
69
|
+
|
70
|
+
ret = ''
|
71
|
+
case res
|
72
|
+
when Net::HTTPSuccess
|
73
|
+
# puts 'response body=' + res.body
|
74
|
+
ret = res.body
|
75
|
+
when Net::HTTPClientError
|
76
|
+
raise ClientError.new(res.class.name, ActiveSupport::JSON.decode(res.body))
|
77
|
+
else
|
78
|
+
#res.error
|
79
|
+
raise ServiceError.new(res.class.name, res.body)
|
80
|
+
end
|
81
|
+
return ret
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
class ClientError < StandardError
|
88
|
+
|
89
|
+
attr_reader :response_hash
|
90
|
+
|
91
|
+
def initialize(class_name, response_hash)
|
92
|
+
puts 'response-hash=' + response_hash.inspect
|
93
|
+
super("#{class_name} - #{response_hash["msg"]}")
|
94
|
+
@response_hash = response_hash
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class ServiceError < StandardError
|
99
|
+
attr_reader :body
|
100
|
+
|
101
|
+
def initialize(class_name, body)
|
102
|
+
super("#{class_name}")
|
103
|
+
@body = body
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Appoxy
|
2
|
+
module Api
|
3
|
+
module Signatures
|
4
|
+
|
5
|
+
|
6
|
+
def self.generate_timestamp(gmtime)
|
7
|
+
return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.generate_signature(operation, timestamp, secret_key)
|
11
|
+
# if USE_EMBEDDED_HMAC
|
12
|
+
# my_sha_hmac = HMAC::SHA1.digest(secret_key, operation + timestamp)
|
13
|
+
# else
|
14
|
+
my_sha_hmac = Digest::HMAC.digest(operation + timestamp, secret_key, Digest::SHA1)
|
15
|
+
# end
|
16
|
+
my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
|
17
|
+
return my_b64_hmac_digest
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/appoxy_api.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: appoxy_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Travis Reeder
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-02-21 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
description: Appoxy API Helper gem description...
|
33
|
+
email: travis@appoxy.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README.markdown
|
40
|
+
files:
|
41
|
+
- lib/api/api_controller.rb
|
42
|
+
- lib/api/client.rb
|
43
|
+
- lib/api/client_helper.rb
|
44
|
+
- lib/api/signatures.rb
|
45
|
+
- lib/appoxy_api.rb
|
46
|
+
- README.markdown
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://www.appoxy.com
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --charset=UTF-8
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Appoxy API Helper gem
|
77
|
+
test_files: []
|
78
|
+
|