chockstone 0.1.0

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/.gitignore ADDED
@@ -0,0 +1 @@
1
+ doc
data/README.rdoc ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
9
+
10
+ # to run a single test:
11
+ # ruby -I"lib:test" test/test_icle.rb -n test_update_user
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{chockstone}
3
+ s.version = %q{0.1.0}
4
+ s.summary = ""
5
+ s.description = "Wrapper for Chockstone API requests"
6
+ s.authors = ["Thomas Mulloy"]
7
+ s.email = %q{twmulloy@gmail.com}
8
+ s.files = [
9
+ "lib/chockstone/connection.rb",
10
+ "lib/chockstone.rb",
11
+ ".gitignore",
12
+ "chockstone.gemspec",
13
+ "Rakefile",
14
+ "README.rdoc"
15
+ ]
16
+ s.homepage = %q{http://thomasmulloy.com}
17
+ s.test_files = [
18
+ "test/test_icle.rb"
19
+ ]
20
+ end
data/lib/chockstone.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Chockstone
2
+
3
+ require 'rubygems'
4
+ require 'active_support/core_ext/hash/conversions'
5
+
6
+ # use 'libxml' if having namespace issues
7
+ # using 'xml' allows us to use XML:: instead of LibXML::XML::
8
+ require 'xml'
9
+ require 'net/https'
10
+
11
+
12
+ $:.unshift(File.dirname(__FILE__))
13
+ require 'chockstone/connection'
14
+
15
+ end
@@ -0,0 +1,190 @@
1
+ module Chockstone
2
+ class Connection
3
+
4
+ def initialize *args
5
+
6
+ return unless args[0].is_a?(Hash)
7
+
8
+ options = args[0]
9
+ @url = options[:url]
10
+
11
+ uri = URI(@url)
12
+ @host = uri.host
13
+ @port = uri.port
14
+ @scheme = uri.scheme
15
+ @path = uri.path
16
+
17
+ @domain = options[:domain]
18
+ @profile = options[:profile]
19
+ @seller_profile = options[:seller_profile]
20
+ @password = options[:password]
21
+
22
+ connection
23
+
24
+ end
25
+
26
+ # api requests
27
+ def auth_user id, password
28
+
29
+ request('authenticate-user',
30
+ :user => {
31
+ :id => id,
32
+ :password => password
33
+ }
34
+ )
35
+
36
+ end
37
+ alias :authenticate_user :auth_user
38
+
39
+ def get_user id
40
+
41
+ request('get-user',
42
+ :user => {
43
+ :id => id
44
+ }
45
+ )
46
+
47
+ end
48
+
49
+ # account argument hash will invoke an account creation
50
+ def create_user user={}, account={}
51
+
52
+ req = {}
53
+ req.merge!({ :user => user }) unless user.empty?
54
+ req.merge!({ :account => account }) unless account.empty?
55
+
56
+ request('create-user', req)
57
+
58
+ end
59
+
60
+ def update_user user={}
61
+ request('update-user',
62
+ :user => user
63
+ )
64
+ end
65
+
66
+ # update user 'id' (email)
67
+ def update_user_id id, new_id
68
+ request('update-user-id',
69
+ :old => {
70
+ :user => {
71
+ :id => id
72
+ }
73
+ },
74
+ :new => {
75
+ :user => {
76
+ :id => new_id
77
+ }
78
+ }
79
+ )
80
+ end
81
+
82
+ def update_user_password id, password
83
+ request('update-user-password',
84
+ :user => {
85
+ :id => id,
86
+ :password => password
87
+ }
88
+ )
89
+ end
90
+
91
+ def add_user_bank_card id, card={}
92
+ request('add-user-bank-card',
93
+ :user => {
94
+ :id => id
95
+ },
96
+ :bank_card => card
97
+ )
98
+ end
99
+
100
+ def delete_user_bank_card id, card_id
101
+ request('delete-user-bank-card',
102
+ :user => {
103
+ :id => id
104
+ },
105
+ :bank_card => {
106
+ :id => card_id
107
+ }
108
+ )
109
+
110
+ end
111
+
112
+
113
+ private
114
+
115
+ def send xml
116
+ http = Net::HTTP.new(@host, @port)
117
+ http.use_ssl = true if @scheme == "https"
118
+ request = Net::HTTP::Post.new(@path)
119
+ request.body = xml.to_s
120
+ resp = http.start{ |http| http.request(request) }
121
+ response(resp.body)
122
+ end
123
+
124
+ def response resp
125
+
126
+ p = XML::Parser.string(resp)
127
+ doc = p.parse
128
+
129
+ #puts "RESPONSE: " + doc.to_s
130
+
131
+ response = doc.find('//response')[0]
132
+ status = response.find('//status')[0]
133
+ method = status.find('//type')[0].content
134
+ data = Hash.from_xml(response.find("//#{method}")[0].to_s)
135
+
136
+ formatted_response = {
137
+ :status => status.find('//code')[0].content,
138
+ :alert => {
139
+ :name => code = status.find('//alert-name')[0].content,
140
+ :type => code = status.find('//alert-type')[0].content
141
+ },
142
+ :method => method,
143
+ :description => status.find('//description')[0].content
144
+ }
145
+
146
+ formatted_response.merge!({ :data => data }) unless data.blank?
147
+
148
+ formatted_response
149
+
150
+ end
151
+
152
+ def request method, params={}
153
+
154
+ # initial xml request
155
+ xml = @hash_connection.merge({
156
+ method => params
157
+ }).to_xml(
158
+ :root => 'request',
159
+ :skip_types => true
160
+ )
161
+
162
+ # parse the inital xml to manipulate nodes
163
+ xml = XML::Parser.string(xml).parse
164
+
165
+ # apply attributes to request node
166
+ req = xml.find('//request')[0]
167
+ req.attributes['version'] = '1'
168
+ req.attributes['revision'] = '1'
169
+
170
+ #puts "REQUEST: " + xml.to_s
171
+
172
+ send(xml)
173
+ end
174
+
175
+ # connection node for request
176
+ def connection
177
+
178
+ @hash_connection = {
179
+ :connection => {
180
+ :domain => @domain,
181
+ :store_id => @profile,
182
+ :service_user => @seller_profile,
183
+ :service_password => @password
184
+ }
185
+ }
186
+
187
+ end
188
+ end
189
+
190
+ end
data/test/test_icle.rb ADDED
@@ -0,0 +1,128 @@
1
+ require 'test/unit'
2
+ require 'chockstone'
3
+
4
+ class ChockstoneTest < Test::Unit::TestCase
5
+
6
+ # YOUR CREDENTIALS
7
+ CREDENTIALS = {
8
+
9
+ }
10
+
11
+ # YOUR TEST USER
12
+ TEST_USER = {
13
+
14
+ }
15
+
16
+ TEST_USER_ACCOUNT = {
17
+ # use one of the following authorization methods
18
+ :pin_authorization => {
19
+ :id => '6277111111111111',
20
+ :pin => '1234'
21
+ }
22
+
23
+ # :credit_card_authorization => {
24
+ # :number => '62771111222233334444',
25
+ # :expiration_date => {
26
+ # :month => '01',
27
+ # :year => '10'
28
+ # },
29
+ # :cvv2 => '010'
30
+ # }
31
+
32
+ # :phone_number_authoirzation => {
33
+ # :number => '5035551212',
34
+ # :pin => '1234'
35
+ # }
36
+
37
+ }
38
+
39
+ def setup
40
+ @cs = Chockstone::Connection.new CREDENTIALS
41
+ @bogus_user = 'loyal-vistor@example.com'
42
+ @bogus_password = 'MyBogusPassword'
43
+ end
44
+
45
+ def test_chockstone_module
46
+ assert_not_nil @cs
47
+ end
48
+
49
+ def test_get_user_fail
50
+ resp = @cs.get_user @bogus_user
51
+
52
+ assert_block "response is a hash and has a status" do
53
+ resp.is_a?(Hash) and resp.include?(:status)
54
+ end
55
+
56
+ assert_equal("fail", resp[:status], "api did not response with fail status: "+resp.to_s)
57
+ assert_equal("get-user", resp[:method], "api did not respond with proper method")
58
+ end
59
+
60
+ def test_get_user_fail
61
+ resp = @cs.auth_user @bogus_user, @bogus_password
62
+
63
+ assert_block "response is a hash and has a status" do
64
+ resp.is_a?(Hash) and resp.include?(:status)
65
+ end
66
+
67
+ assert_equal("fail", resp[:status], "api did not response with fail status: "+resp.to_s)
68
+ assert_equal("authenticate-user", resp[:method], "api did not respond with proper method")
69
+
70
+ end
71
+
72
+ def test_create_user_fail
73
+
74
+ user = TEST_USER
75
+ account = TEST_USER_ACCOUNT || nil
76
+
77
+ resp = @cs.create_user(user, account)
78
+
79
+ assert_block "response is a hash and has a status" do
80
+ resp.is_a?(Hash) and resp.include?(:status)
81
+ end
82
+
83
+ assert_equal("fail", resp[:status], "api did not response with fail status: "+resp.to_s)
84
+ assert_equal("create-user", resp[:method], "api did not respond with proper method")
85
+ end
86
+
87
+ def test_update_user_fail
88
+
89
+ user = TEST_USER
90
+
91
+ # remove password from our generic user
92
+ user.delete :password
93
+
94
+ resp = @cs.update_user(TEST_USER)
95
+
96
+ assert_block "response is a hash and has a status" do
97
+ resp.is_a?(Hash) and resp.include?(:status)
98
+ end
99
+
100
+ assert_equal("fail", resp[:status], "api did not response with fail status: "+resp.to_s)
101
+ assert_equal("update-user", resp[:method], "api did not respond with proper method")
102
+
103
+ end
104
+
105
+ def test_update_user_id_fail
106
+ resp = @cs.update_user_id @bogus_user, @bogus_user
107
+
108
+ assert_block "response is a hash and has a status" do
109
+ resp.is_a?(Hash) and resp.include?(:status)
110
+ end
111
+
112
+ assert_equal("fail", resp[:status], "api did not response with fail status: "+resp.to_s)
113
+ assert_equal("update-user-id", resp[:method], "api did not respond with proper method")
114
+ end
115
+
116
+ def test_update_user_password
117
+ resp = @cs.update_user_password(@bogus_user, @bogus_password)
118
+
119
+ assert_block "response is a hash and has a status" do
120
+ resp.is_a?(Hash) and resp.include?(:status)
121
+ end
122
+
123
+ assert_equal("fail", resp[:status], "api did not response with fail status: "+resp.to_s)
124
+ assert_equal("update-user-password", resp[:method], "api did not respond with proper method")
125
+ end
126
+
127
+
128
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chockstone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Mulloy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Wrapper for Chockstone API requests
15
+ email: twmulloy@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/chockstone/connection.rb
21
+ - lib/chockstone.rb
22
+ - .gitignore
23
+ - chockstone.gemspec
24
+ - Rakefile
25
+ - README.rdoc
26
+ - test/test_icle.rb
27
+ homepage: http://thomasmulloy.com
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.21
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: ''
51
+ test_files:
52
+ - test/test_icle.rb