versacommerce_api 1.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.
Files changed (45) hide show
  1. data/.gitignore +18 -0
  2. data/.ruby-version +1 -0
  3. data/CHANGELOG +2 -0
  4. data/CONTRIBUTORS +0 -0
  5. data/Gemfile +4 -0
  6. data/Gemfile.lock +59 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +200 -0
  9. data/RELEASING +16 -0
  10. data/Rakefile +1 -0
  11. data/bin/versacommerce +4 -0
  12. data/lib/active_resource/base_ext.rb +22 -0
  13. data/lib/active_resource/connection_ext.rb +31 -0
  14. data/lib/active_resource/detailed_log_subscriber.rb +19 -0
  15. data/lib/active_resource/disable_prefix_check.rb +8 -0
  16. data/lib/active_resource/json_errors.rb +21 -0
  17. data/lib/versacommerce_api.rb +21 -0
  18. data/lib/versacommerce_api/associatable.rb +25 -0
  19. data/lib/versacommerce_api/cli.rb +164 -0
  20. data/lib/versacommerce_api/countable.rb +9 -0
  21. data/lib/versacommerce_api/resources.rb +2 -0
  22. data/lib/versacommerce_api/resources/base.rb +42 -0
  23. data/lib/versacommerce_api/resources/billing_addres.rb +9 -0
  24. data/lib/versacommerce_api/resources/collection.rb +6 -0
  25. data/lib/versacommerce_api/resources/customer.rb +6 -0
  26. data/lib/versacommerce_api/resources/item.rb +9 -0
  27. data/lib/versacommerce_api/resources/link.rb +15 -0
  28. data/lib/versacommerce_api/resources/linklist.rb +11 -0
  29. data/lib/versacommerce_api/resources/order.rb +22 -0
  30. data/lib/versacommerce_api/resources/page.rb +6 -0
  31. data/lib/versacommerce_api/resources/payment.rb +6 -0
  32. data/lib/versacommerce_api/resources/product.rb +55 -0
  33. data/lib/versacommerce_api/resources/product_image.rb +67 -0
  34. data/lib/versacommerce_api/resources/property.rb +6 -0
  35. data/lib/versacommerce_api/resources/shipment.rb +11 -0
  36. data/lib/versacommerce_api/resources/shipping_addres.rb +9 -0
  37. data/lib/versacommerce_api/resources/shop.rb +10 -0
  38. data/lib/versacommerce_api/resources/variant.rb +7 -0
  39. data/lib/versacommerce_api/session.rb +88 -0
  40. data/lib/versacommerce_api/version.rb +3 -0
  41. data/spec/resources/order_spec.rb +71 -0
  42. data/spec/resources/product_spec.rb +73 -0
  43. data/spec/spec_helper.rb +18 -0
  44. data/versacommerce_api.gemspec +37 -0
  45. metadata +242 -0
@@ -0,0 +1,6 @@
1
+ module VersacommerceAPI
2
+
3
+ class Property < Base
4
+ end
5
+
6
+ end
@@ -0,0 +1,11 @@
1
+ module VersacommerceAPI
2
+
3
+ class Shipment < Base
4
+ include Associatable
5
+
6
+ def order
7
+ associated_resource "order", false
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,9 @@
1
+ module VersacommerceAPI
2
+
3
+ class ShippingAddres < Base
4
+ def fullname
5
+ [firstname, lastname].delete_if(&:blank?).compact * " "
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,10 @@
1
+ module VersacommerceAPI
2
+
3
+ # Shop object. Use Shop.current to receive the shop.
4
+ class Shop < Base
5
+ def self.current
6
+ find(:one, :from => "/api/shop.xml")
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,7 @@
1
+ module VersacommerceAPI
2
+
3
+ class Variant < Base
4
+ self.prefix = "/api/products/:product_id/"
5
+ end
6
+
7
+ end
@@ -0,0 +1,88 @@
1
+ module VersacommerceAPI
2
+
3
+ class Session
4
+ cattr_accessor :api_key
5
+ cattr_accessor :secret
6
+ cattr_accessor :protocol
7
+ self.protocol = 'http'
8
+
9
+ attr_accessor :url, :token, :name
10
+
11
+
12
+ def initialize(url, token = nil, params = nil)
13
+ self.url, self.token = url, token
14
+
15
+ if params && params[:signature]
16
+ unless self.class.validate_signature(params) && params[:timestamp].to_i > 24.hours.ago.utc.to_i
17
+ raise "Invalid Signature: Possible malicious login"
18
+ end
19
+ end
20
+
21
+ self.class.prepare_url(self.url)
22
+ end
23
+
24
+
25
+ def self.setup(params)
26
+ params.each { |k,value| send("#{k}=", value) }
27
+ end
28
+
29
+
30
+ def self.request_token(domain)
31
+ return nil if domain.blank? || api_key.blank?
32
+ begin
33
+ content = open("http://#{domain}/api/auth.xml?api_key=#{api_key}") { |io| data = io.read }
34
+ Hash.from_xml(content)["token"] if content
35
+ rescue
36
+ nil
37
+ end
38
+ end
39
+
40
+
41
+ def shop
42
+ Shop.current
43
+ end
44
+
45
+
46
+ def create_permission_url
47
+ return nil if url.blank? || api_key.blank?
48
+ "http://#{url}/api/auth?api_key=#{api_key}"
49
+ end
50
+
51
+
52
+ # Used by ActiveResource::Base to make all non-authentication API calls
53
+ def site
54
+ "#{protocol}://#{api_key}:#{computed_password}@#{url}/api/"
55
+ end
56
+
57
+
58
+ def valid?
59
+ url.present? && token.present?
60
+ end
61
+
62
+ private
63
+
64
+ # The secret is computed by taking the shared_secret which we got when
65
+ # registring this third party application and concating the request_to it,
66
+ # and then calculating a MD5 hexdigest.
67
+
68
+ # secret = shared_key
69
+ # token was provided by registration
70
+ def computed_password
71
+ Digest::MD5.hexdigest(secret + token.to_s)
72
+ end
73
+
74
+ def self.prepare_url(url)
75
+ return nil if url.blank?
76
+ url.gsub!(/https?:\/\//, '') # remove http:// or https://
77
+ url.concat(".versacommerce.de") unless url.include?('.') # extend url to versacommerce.de if no host is given
78
+ end
79
+
80
+ def self.validate_signature(params)
81
+ return false unless signature = params[:signature]
82
+
83
+ sorted_params = params.except(:signature, :action, :controller).collect{|k,v|"#{k}=#{v}"}.sort.join
84
+ Digest::MD5.hexdigest(secret + sorted_params) == signature
85
+ end
86
+ end
87
+
88
+ end
@@ -0,0 +1,3 @@
1
+ module VersacommerceAPI
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe VersacommerceAPI::Order do
4
+ let!(:klass) { VersacommerceAPI::Order }
5
+
6
+ before(:all) do
7
+ initialize_session
8
+ end
9
+
10
+ describe 'CRUD Operations' do
11
+
12
+ describe 'Create' do
13
+ context 'New Order' do
14
+ context 'when order is valid' do
15
+ it 'should place a new order' do
16
+ order = klass.new(customer_email: 'dummy@email.de', item_total: 20)
17
+ order.save.should be_true
18
+ end
19
+ end
20
+
21
+ context 'when order is invalid' do
22
+ it 'should raise error'
23
+ end
24
+ end
25
+ end
26
+
27
+ describe 'Retrive' do
28
+ context 'when order does not exist' do
29
+ it 'should raise resource not found error' do
30
+ expect { klass.find(23456789) }.to raise_error(ActiveResource::ResourceNotFound)
31
+ end
32
+ end
33
+
34
+ context 'when order exist' do
35
+ it 'should return the order' do
36
+ orders = klass.find(:all, :params => {:limit => 2})
37
+ orders.count.should eql 2
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'Update' do
43
+ context 'for existing order' do
44
+ it 'should update the attributes of the order' do
45
+ order = klass.last
46
+ order.update_attribute(:bank_account_name, 'German Bank')
47
+ klass.find(order.id).bank_account_name.should eql 'German Bank'
48
+ end
49
+ end
50
+ end
51
+
52
+ describe 'delete' do
53
+ before(:all) { @order = VersacommerceAPI::Order.last }
54
+
55
+ context 'when order exist' do
56
+ it 'should delete the order' do
57
+ response = @order.destroy
58
+ response.code.should eql '200'
59
+ end
60
+ end
61
+
62
+ context 'when order does not exist' do
63
+ it 'should raise error' do
64
+ response = @order.destroy
65
+ response.code.should eql '404'
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe VersacommerceAPI::Product do
4
+ let!(:klass) { VersacommerceAPI::Product }
5
+
6
+ before(:all) do
7
+ initialize_session
8
+ end
9
+
10
+ describe 'CRUD Operations' do
11
+
12
+ describe 'Create' do
13
+ context 'new product' do
14
+ context 'when invalid' do
15
+ it 'should contains error messages' do
16
+ new_product = klass.create(:title => 'New-Api-product')
17
+ expect { new_product.errors.messages[:base].present? }.to be_true
18
+ end
19
+ end
20
+
21
+ context 'when valid' do
22
+ it 'should save the product' do
23
+ new_product = klass.new(:title => 'New-Api-product', :code=>"1x Api Product")
24
+ new_product.save.should be_true
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "Retrive" do
31
+ context "when product not found" do
32
+ it 'should raise resource not found error' do
33
+ expect { klass.find(23456789) }.to raise_error(ActiveResource::ResourceNotFound)
34
+ end
35
+ end
36
+
37
+ context "when fetching products with limit value" do
38
+ it 'should return specified number of products' do
39
+ products = klass.find(:all, :params => {:limit => 2})
40
+ products.count.should eql 2
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'Update' do
46
+ context 'for an existing product' do
47
+ it 'should update the given product attributes' do
48
+ product = klass.where(code: "1x Api Product").first
49
+ product.code = '2x Api Product'
50
+ product.save!
51
+ expect { klass.find(product.id).code }.should eql '2x Api Product'
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'Delete' do
57
+ before(:all) { @product = VersacommerceAPI::Product.last }
58
+
59
+ context 'when product exist' do
60
+ it 'should delete the specified product' do
61
+ response = @product.destroy
62
+ response.code.should eql '200'
63
+ end
64
+ end
65
+
66
+ context 'when product does not exist' do
67
+ it 'should raise resource not found errors' do
68
+ expect { klass.find(23456789) }.to raise_error(ActiveResource::ResourceNotFound)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+ require 'pry'
5
+
6
+ require './lib/versacommerce_api/'
7
+
8
+ RSpec.configure do |c|
9
+ c.mock_with :rspec
10
+ end
11
+
12
+
13
+ def initialize_session
14
+ VersacommerceAPI::Session.setup(api_key: "4e6d0a2be244cff731f88caaff45e6ea3d2f9221", secret: "afe883e32194fc464c029e73fe9f3674b6e4996e")
15
+ token = "c26a0c74" #VersacommerceAPI::Session.request_token("test-produkte.versacommerce.de")
16
+ session = VersacommerceAPI::Session.new("test-produkte.versacommerce.de", token)
17
+ VersacommerceAPI::Base.activate_session(session)
18
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'versacommerce_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "versacommerce_api"
8
+ spec.version = VersacommerceAPI::VERSION
9
+ spec.author = ["VersaCommerce"]
10
+
11
+ spec.summary = %q{The VersaCommerce API gem is a lightweight gem for accessing the VersaCommerce admin REST web services}
12
+ spec.description = %q{The VersaCommerce API gem allows Ruby developers to programmatically access the admin section of VersaCommerce shops. The API is implemented as JSON or XML over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has its own URL and is manipulated in isolation.}
13
+ spec.homepage = "http://www.versacommerce.de"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-nav"
26
+
27
+ spec.add_dependency("activeresource", "~> 3.2.14")
28
+ spec.add_dependency("thor", [">= 0.14.4"])
29
+
30
+ if spec.respond_to?(:add_development_dependency)
31
+ spec.add_development_dependency("mocha", ">= 0.9.8")
32
+ spec.add_development_dependency("fakeweb")
33
+ else
34
+ spec.add_dependency("mocha", ">= 0.9.8")
35
+ spec.add_dependency("fakeweb")
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,242 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: versacommerce_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - VersaCommerce
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry-nav
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: activeresource
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 3.2.14
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 3.2.14
110
+ - !ruby/object:Gem::Dependency
111
+ name: thor
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.14.4
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.14.4
126
+ - !ruby/object:Gem::Dependency
127
+ name: mocha
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 0.9.8
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 0.9.8
142
+ - !ruby/object:Gem::Dependency
143
+ name: fakeweb
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: The VersaCommerce API gem allows Ruby developers to programmatically
159
+ access the admin section of VersaCommerce shops. The API is implemented as JSON
160
+ or XML over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like
161
+ Order, Product, or Collection, has its own URL and is manipulated in isolation.
162
+ email:
163
+ executables:
164
+ - versacommerce
165
+ extensions: []
166
+ extra_rdoc_files: []
167
+ files:
168
+ - .gitignore
169
+ - .ruby-version
170
+ - CHANGELOG
171
+ - CONTRIBUTORS
172
+ - Gemfile
173
+ - Gemfile.lock
174
+ - LICENSE.txt
175
+ - README.md
176
+ - RELEASING
177
+ - Rakefile
178
+ - bin/versacommerce
179
+ - lib/active_resource/base_ext.rb
180
+ - lib/active_resource/connection_ext.rb
181
+ - lib/active_resource/detailed_log_subscriber.rb
182
+ - lib/active_resource/disable_prefix_check.rb
183
+ - lib/active_resource/json_errors.rb
184
+ - lib/versacommerce_api.rb
185
+ - lib/versacommerce_api/associatable.rb
186
+ - lib/versacommerce_api/cli.rb
187
+ - lib/versacommerce_api/countable.rb
188
+ - lib/versacommerce_api/resources.rb
189
+ - lib/versacommerce_api/resources/base.rb
190
+ - lib/versacommerce_api/resources/billing_addres.rb
191
+ - lib/versacommerce_api/resources/collection.rb
192
+ - lib/versacommerce_api/resources/customer.rb
193
+ - lib/versacommerce_api/resources/item.rb
194
+ - lib/versacommerce_api/resources/link.rb
195
+ - lib/versacommerce_api/resources/linklist.rb
196
+ - lib/versacommerce_api/resources/order.rb
197
+ - lib/versacommerce_api/resources/page.rb
198
+ - lib/versacommerce_api/resources/payment.rb
199
+ - lib/versacommerce_api/resources/product.rb
200
+ - lib/versacommerce_api/resources/product_image.rb
201
+ - lib/versacommerce_api/resources/property.rb
202
+ - lib/versacommerce_api/resources/shipment.rb
203
+ - lib/versacommerce_api/resources/shipping_addres.rb
204
+ - lib/versacommerce_api/resources/shop.rb
205
+ - lib/versacommerce_api/resources/variant.rb
206
+ - lib/versacommerce_api/session.rb
207
+ - lib/versacommerce_api/version.rb
208
+ - spec/resources/order_spec.rb
209
+ - spec/resources/product_spec.rb
210
+ - spec/spec_helper.rb
211
+ - versacommerce_api.gemspec
212
+ homepage: http://www.versacommerce.de
213
+ licenses:
214
+ - MIT
215
+ post_install_message:
216
+ rdoc_options: []
217
+ require_paths:
218
+ - lib
219
+ required_ruby_version: !ruby/object:Gem::Requirement
220
+ none: false
221
+ requirements:
222
+ - - ! '>='
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ required_rubygems_version: !ruby/object:Gem::Requirement
226
+ none: false
227
+ requirements:
228
+ - - ! '>='
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ requirements: []
232
+ rubyforge_project:
233
+ rubygems_version: 1.8.23
234
+ signing_key:
235
+ specification_version: 3
236
+ summary: The VersaCommerce API gem is a lightweight gem for accessing the VersaCommerce
237
+ admin REST web services
238
+ test_files:
239
+ - spec/resources/order_spec.rb
240
+ - spec/resources/product_spec.rb
241
+ - spec/spec_helper.rb
242
+ has_rdoc: