versacommerce_api 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
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
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
17
+ .DS_Store
18
+ /scratchpads
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p429
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+
2
+ 1.0.1 - First public release
data/CONTRIBUTORS ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in versacommerce_api.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ versacommerce_api (1.0.1)
5
+ activeresource (~> 3.2.14)
6
+ thor (>= 0.14.4)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (3.2.14)
12
+ activesupport (= 3.2.14)
13
+ builder (~> 3.0.0)
14
+ activeresource (3.2.14)
15
+ activemodel (= 3.2.14)
16
+ activesupport (= 3.2.14)
17
+ activesupport (3.2.14)
18
+ i18n (~> 0.6, >= 0.6.4)
19
+ multi_json (~> 1.0)
20
+ builder (3.0.4)
21
+ coderay (1.0.9)
22
+ diff-lcs (1.2.4)
23
+ fakeweb (1.3.0)
24
+ i18n (0.6.5)
25
+ metaclass (0.0.1)
26
+ method_source (0.8.2)
27
+ mocha (0.14.0)
28
+ metaclass (~> 0.0.1)
29
+ multi_json (1.8.0)
30
+ pry (0.9.12.2)
31
+ coderay (~> 1.0.5)
32
+ method_source (~> 0.8)
33
+ slop (~> 3.4)
34
+ pry-nav (0.2.3)
35
+ pry (~> 0.9.10)
36
+ rake (10.1.0)
37
+ rspec (2.14.1)
38
+ rspec-core (~> 2.14.0)
39
+ rspec-expectations (~> 2.14.0)
40
+ rspec-mocks (~> 2.14.0)
41
+ rspec-core (2.14.5)
42
+ rspec-expectations (2.14.3)
43
+ diff-lcs (>= 1.1.3, < 2.0)
44
+ rspec-mocks (2.14.3)
45
+ slop (3.4.6)
46
+ thor (0.18.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ bundler (~> 1.3)
53
+ fakeweb
54
+ mocha (>= 0.9.8)
55
+ pry
56
+ pry-nav
57
+ rake
58
+ rspec
59
+ versacommerce_api!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bjoern B. Dorra
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # VersacommerceAPI
2
+
3
+ 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.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'versacommerce_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install versacommerce_api
18
+
19
+ ## Introduction
20
+
21
+ Every app needs to:
22
+
23
+ 1. Registered with VersaCommerce. You will receive an APP_KEY and a shared secret.
24
+ 2. Registered with a Shop. This will provide a token.
25
+
26
+ You communicate with your shop using your APP_KEY and a generated password. The password is an MD5-Hash of your token and shared secret.
27
+
28
+
29
+ For detailed information see: [Authentication](https://github.com/versacommerce/vc-api/blob/master/sections/authentication.md)
30
+
31
+
32
+ ## Registering your app with a shop.
33
+
34
+ $ open http://YOUR-VERSACOMMERCE-DOMAIN.versacommerce.de/api/auth?api_key=API_KEY
35
+
36
+ ## Interactive console
37
+
38
+ You need you API-KEY and generated password. (http://YOUR-VERSACOMMERCE-DOMAIN.versacommerce.de/admin/settings/apps)
39
+
40
+ $ bin/versacommerce add YOUR-VERSACOMMERCE-DOMAIN (enter APP-Key and password)
41
+ $ bin/versacommerce console
42
+ irb(main):001:0> VersacommerceAPI::Shop.current
43
+
44
+ ## Sample code:
45
+
46
+ require "rubygems"
47
+ require "versacommerce_api"
48
+
49
+ # instantiate a session that is ready to make calls to the given shop.
50
+ VersacommerceAPI::Session.setup(api_key: "API_KEY_FOR_APP", secret: "SHARED_SECRET_FOR_APP")
51
+
52
+ # If your app is resistered with you shop, you can request the "registration-token"
53
+ token = VersacommerceAPI::Session.request_token("api-test.versacommerce.de")
54
+
55
+ # Instantiate a session that is ready to make calls to the given shop.
56
+ session = VersacommerceAPI::Session.new("shopdomain.versacommerce.de", "RECEIVED_TOKEN")
57
+ session.valid? # returns true
58
+
59
+ # Now you can activate the session and you’re set:
60
+ VersacommerceAPI::Base.activate_session(session)
61
+
62
+ # Get data from that shop (returns ActiveResource instances):
63
+ shop = VersacommerceAPI::Shop.current
64
+
65
+ # Get three products:
66
+ products = VersacommerceAPI::Product.find(:all, :params => {:limit => 3})
67
+
68
+ # Get some orders:
69
+ latest_orders = VersacommerceAPI::Order.find(:all)
70
+
71
+ ### Product
72
+
73
+
74
+ Get product count:
75
+
76
+ VersacommerceAPI::Product.count
77
+ => 18
78
+
79
+ Get product count including variants:
80
+
81
+ VersacommerceAPI::Product.count("show_variants" => "true")
82
+ => 21
83
+
84
+ Find all products:
85
+
86
+ VersacommerceAPI::Product.find(:all, params: {limit: 3})
87
+
88
+ Find all products and variants (nested objects):
89
+
90
+ VersacommerceAPI::Product.find(:all, params: {include: :variants})
91
+
92
+ Find all products and variants (not nested):
93
+
94
+ VersacommerceAPI::Product.find(:all, params: {show_variants: true})
95
+
96
+ Fetch a product and the nested variants:
97
+
98
+ VersacommerceAPI::Product.find(167357, params: {include: :variants})
99
+
100
+ Fetch a product and nested properties of that product:
101
+
102
+ VersacommerceAPI::Product.find(167357, params: {include: :variants})
103
+
104
+ Fetch a product and the nested variants including properties of those products and variants:
105
+
106
+ VersacommerceAPI::Product.find(167357, params: {include: [:variants, :properties]})
107
+
108
+ *Reference:*
109
+
110
+ * [Product](https://github.com/versacommerce/vc-api/blob/master/sections/product.md)
111
+ * [ProductImage](https://github.com/versacommerce/vc-api/blob/master/sections/product_image.md)
112
+ * [Variant](https://github.com/versacommerce/vc-api/blob/master/sections/variant.md)
113
+ * [Property](https://github.com/versacommerce/vc-api/blob/master/sections/property.md)
114
+
115
+ #### Receive products in batches
116
+
117
+ You can receive max 250 records per request. The default limit is 150. If you need to handle more records, you should request them in batches. Use the query params "limit" and "offset" to batch through your results.
118
+
119
+ Sample, receive all products and prints them.
120
+
121
+ require "rubygems"
122
+ require "versacommerce_api"
123
+
124
+ products = []
125
+ products_count = VersacommerceAPI::Product.count("show_variants" => "true")
126
+ num_batches = (products_count.to_f / 200).ceil
127
+ num_batches.times do |batch|
128
+ puts "Fetching products: Batch #{batch+1} of #{num_batches}"
129
+ products.concat VersacommerceAPI::Product.find(:all, params: {limit: 200, offset: batch*200, show_variants: true})
130
+ end
131
+
132
+ products.each do |product|
133
+ puts "product code : #{product.code}"
134
+ puts "product title: #{product.title}"
135
+ end
136
+
137
+ ### Order
138
+
139
+ VersacommerceAPI::Order.find(:all, :params => {:limit => 3})
140
+
141
+ *Reference:*
142
+
143
+ * [Order](https://github.com/versacommerce/vc-api/blob/master/sections/order.md)
144
+ * [Item](https://github.com/versacommerce/vc-api/blob/master/sections/item.md)
145
+ * [Customer](https://github.com/versacommerce/bcx-api/blob/master/sections/custimer.md)
146
+ * [BillingAddress](https://github.com/versacommerce/bcx-api/blob/master/sections/billing_address.md)
147
+ * [ShippingAddress](https://github.com/versacommerce/vc-api/blob/master/sections/shipping_address.md)
148
+ * [Shipment](https://github.com/versacommerce/vc-api/blob/master/sections/shipment.md)
149
+ * [Payment](https://github.com/versacommerce/vc-api/blob/master/sections/payment.md)
150
+
151
+ ### Customer
152
+
153
+ VersacommerceAPI::Customer.find(:all, :params => {:limit => 3, :include => :billing_address})
154
+ c = VersacommerceAPI::Customer.find(18451)
155
+ c.option_01 = "test"
156
+ c.save
157
+
158
+ ### Page
159
+
160
+ VersacommerceAPI::Page.find(:all).first
161
+
162
+ => {
163
+ "active"=>true,
164
+ "title"=>"AGB",
165
+ "content"=>"<p>This is some Text</p>",
166
+ "content_meta_description"=>"This is a special page.",
167
+ "content_meta_keywords"=>"kewords, supported",
168
+ "content_title_tag"=>"Search engine optimized title",
169
+ "custom_url"=>nil,
170
+ "custom_url_routing"=>"standard_url_is_canonical",
171
+ "format"=>"tinymce",
172
+ "handle"=>"this-is-a-special-page",
173
+ "id"=>9070,
174
+ "option_01"=>nil,
175
+ "option_02"=>nil,
176
+ "option_03"=>nil,
177
+ "properties_count"=>0,
178
+ "mall_id"=>nil,
179
+ "shop_id"=>1157,
180
+ "created_on"=>Mon, 10 Sep 2013,
181
+ "updated_on"=>Mon, 10 Sep 2013
182
+ }
183
+
184
+ *Reference:*
185
+
186
+ * [Page](https://github.com/versacommerce/vc-api/blob/master/sections/page.md)
187
+
188
+ ## TODO
189
+
190
+ * Add more resources
191
+ * More documentation
192
+ * More Specs
193
+
194
+ ## Contributing
195
+
196
+ 1. Fork it
197
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
198
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
199
+ 4. Push to the branch (`git push origin my-new-feature`)
200
+ 5. Create new Pull Request
data/RELEASING ADDED
@@ -0,0 +1,16 @@
1
+ Releasing VersacommerceAPI
2
+
3
+ 1. Check the Semantic Versioning page for info on how to version the new release: http://semver.org
4
+ 2. Update the version of VersacommerceAPI in lib/versacommerce_api/version.rb
5
+ 3. Add a CHANGELOG entry for the new release with the date
6
+ 4. Commit the changes with a commit message like "Packaging for release X.Y.Z"
7
+ 5. Tag the release with the version (Leave REV blank for HEAD or provide a SHA)
8
+ $ git tag vX.Y.Z REV
9
+ 6. Push out the changes
10
+ $ git push
11
+ 7. Push out the tags
12
+ $ git push --tags
13
+ 8. Build the new .gem from the updated .gemspec
14
+ $ gem build versacommerce_api.gemspec
15
+ 9. Publish the Gem to gemcutter
16
+ $ gem push versacommerce_api-X.Y.Z.gem
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/versacommerce ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../lib/versacommerce_api',__FILE__)
3
+ require File.expand_path('../../lib/versacommerce_api/cli',__FILE__)
4
+ VersacommerceAPI::Cli.start(ARGV)
@@ -0,0 +1,22 @@
1
+ module ActiveResource
2
+ class Base
3
+ # Backported from ActiveResource master branch
4
+ def self.headers
5
+ @headers ||= {}
6
+
7
+ if superclass != Object && superclass.headers
8
+ @headers = superclass.headers.merge(@headers)
9
+ else
10
+ @headers
11
+ end
12
+ end
13
+ # https://github.com/rails/activeresource/commit/dfef85ce8f653f75673631b2950fcdb0781c313c
14
+ def self.delete(id, options = {})
15
+ connection.delete(element_path(id, options), headers)
16
+ end
17
+ def self.build(attributes = {})
18
+ attrs = self.format.decode(connection.get("#{new_element_path}", headers).body).merge(attributes)
19
+ self.new(attrs)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/core_ext/module/aliasing'
2
+
3
+ module ActiveResource
4
+ class Connection
5
+
6
+ attr_reader :response
7
+
8
+ def handle_response_with_response_capture(response)
9
+ @response = handle_response_without_response_capture(response)
10
+ end
11
+
12
+ def request_with_detailed_log_subscriber(method, path, *arguments)
13
+ result = request_without_detailed_log_subscriber(method, path, *arguments)
14
+ detailed_log_subscriber(result, arguments)
15
+ result
16
+ rescue => e
17
+ detailed_log_subscriber(e.response, arguments) if e.respond_to?(:response)
18
+ raise
19
+ end
20
+
21
+ def detailed_log_subscriber(response, arguments)
22
+ ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
23
+ payload[:response] = response
24
+ payload[:data] = arguments
25
+ end
26
+ end
27
+
28
+ alias_method_chain :handle_response, :response_capture
29
+ alias_method_chain :request, :detailed_log_subscriber
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveResource
2
+ class DetailedLogSubscriber < ActiveSupport::LogSubscriber
3
+ def request(event)
4
+ data = event.payload[:data]
5
+ headers = data.extract_options!
6
+ request_body = data.first
7
+
8
+ info "Request:\n#{request_body}" if request_body
9
+ info "Headers: #{headers.inspect}"
10
+ info "Response:\n#{event.payload[:response].body}"
11
+ end
12
+
13
+ def logger
14
+ ActiveResource::Base.logger
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
@@ -0,0 +1,8 @@
1
+ module DisablePrefixCheck
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def check_prefix_options(options)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_resource/base'
2
+
3
+ module ActiveResource
4
+ class Errors < ActiveModel::Errors
5
+ def from_hash(messages, save_cache = false)
6
+ clear unless save_cache
7
+
8
+ messages.each do |key,errors|
9
+ errors.each do |error|
10
+ add(key, error)
11
+ end
12
+ end
13
+ end
14
+
15
+ # Grabs errors from a json response.
16
+ def from_json(json, save_cache = false)
17
+ hash = ActiveSupport::JSON.decode(json)['errors'] || {} rescue {}
18
+ from_hash hash, save_cache
19
+ end
20
+ end
21
+ end