whiplash_api 1.0.6 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a92bd769283e38a3430455207ddc5481d7c55745
4
- data.tar.gz: 2d6c8ea5e92d6dd5002b1a02571ca482ff7c7da8
3
+ metadata.gz: 1b8e4cde329204d9a5cfbaeeee4f98fca367bf2d
4
+ data.tar.gz: 83e20c82aaa00ece745944f6ddb1c1a853466346
5
5
  SHA512:
6
- metadata.gz: 2839984f50273684227e474a41a5e93d12fe30bb17a016dc91c0f5463c37703e81aadd5ddfe8bc1bd4bd2c3aae840a83b895d0ba496f65fb1d723ed420b28069
7
- data.tar.gz: 768fb3dee5910cb1e37d149c9bfba65cd01d473679b0ac26b1c02e2d6a02c7193d0d4cbab2a345704f3b18aaa00485ec3c50b88060c0c4b2f888996e9d12fa6c
6
+ metadata.gz: f978b08e9b7bbc52a70dcfb09a86f050e8043b3a6c2a2155ea83b40467985ad834c1421c313cace4ac00892c2962a72907b80b13bb2672444f9e696724419203
7
+ data.tar.gz: 1951090bdfa4d3d17a30014721d4573b2aa44a90f2abe3a84b89932951c83e86923c644dfa5caabfa07efd9fb00c892b25a5efd7ce28ff90b8ae60d4837d175d
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  .ruby-gemset
11
11
  .ruby-version
12
+ .rbenv-vars
data/README.md CHANGED
@@ -5,10 +5,6 @@ Whiplash API V1 - Ruby Client
5
5
 
6
6
  This library provides a wrapper around the [Whiplash][whiplash] [Merchandising REST API][api] for use within Ruby apps or via the console.
7
7
 
8
- ### Note
9
-
10
- **If you are using a Rails app, the advised approach is to use ActiveResource. You can get started or gain inspiration from our [example Rails app][app]: **
11
-
12
8
  ### Requirements
13
9
 
14
10
  - Ruby 1.9.3+
@@ -16,9 +12,9 @@ This library provides a wrapper around the [Whiplash][whiplash] [Merchandising R
16
12
  - JSON
17
13
  - ActiveResource
18
14
 
19
- A valid API key is required to authenticate requests. You can find your API key on your customer account page.
15
+ A valid API key (v1) or Oauth token (v2) is required to authenticate requests. You can find your API key on your customer account page.
20
16
 
21
- You can, also, use the test API key `Hc2BHTn3bcrwyPooyYTP` to test this client.
17
+ You can also use the test API key `Hc2BHTn3bcrwyPooyYTP` to test this client.
22
18
 
23
19
  ### Installation
24
20
 
@@ -40,15 +36,29 @@ To use the API client in your Ruby code, provide the required credentials as fol
40
36
  require 'rubygems'
41
37
  require 'whiplash_api'
42
38
 
43
- WhiplashAPI::Base.api_key = 'XXXXXXXXXXXXX'
39
+ WhiplashApi::Base.api_key = 'XXXXXXXXXXXXX'
44
40
  ```
45
-
46
41
  An error will be raised if no API key is provided.
47
42
 
43
+ ### API Versions
44
+
45
+ The default Whiplash API is v1 and uses API keys. The v2 API uses Oauth, and is currently available in private beta.
46
+
47
+ Both API versions are fully supported by this gem. Unless specified, API v1 is used.
48
+
49
+ NOTE: The API Key is your OAuth access token if you are using API v2. If you want to use API v2, call `.api_version` before setting the OAuth token:
50
+
51
+ ```
52
+ WhiplashApi::Base.api_version = 1
53
+ WhiplashApi::Base.api_key = 'XXXXXXXXXXXXX'
54
+ ```
55
+
56
+ ### Sandbox usage
57
+
48
58
  You'll likely want to start by testing in the Sandbox:
49
59
 
50
60
  ```
51
- WhiplashAPI::Base.testing!
61
+ WhiplashApi::Base.testing!
52
62
  ```
53
63
 
54
64
  You can, also, check a very basic implementation/example inside `exe/whiplash_api` file.
@@ -60,17 +70,17 @@ The API currently gives you access to all endpoints supported by [Whiplash][whip
60
70
  ```
61
71
  $ irb
62
72
  >
63
- > WhiplashAPI::Base.api_key = 'XXXXXXXXXXXXX'
73
+ > WhiplashApi::Base.api_key = 'XXXXXXXXXXXXX'
64
74
  >
65
- > items = WhiplashAPI::Item.all
75
+ > items = WhiplashApi::Item.all
66
76
  >
67
- > items = WhiplashAPI::Item.sku('SOME-SKU-111')
77
+ > items = WhiplashApi::Item.sku('SOME-SKU-111')
68
78
  >
69
- > order = WhiplashAPI::Order.last
79
+ > order = WhiplashApi::Order.last
70
80
  >
71
- > orders = WhiplashAPI::Order.all(:params => {:status => 'shipped', :created_at_min => '2008-01-01'})
81
+ > orders = WhiplashApi::Order.all(:params => {:status => 'shipped', :created_at_min => '2008-01-01'})
72
82
  >
73
- > order_item = WhiplashAPI::OrderItem.originator(ID_IN_YOUR_STORE)
83
+ > order_item = WhiplashApi::OrderItem.originator(ID_IN_YOUR_STORE)
74
84
  >
75
85
  ```
76
86
 
@@ -86,19 +96,31 @@ $ irb
86
96
 
87
97
  ### Testing Whiplash API Gem
88
98
 
89
- You can run the tests on the current version of this gem, by cloning it locally, and then, running:
99
+ You can run the tests on the current version of this gem, by cloning it
100
+ locally. Tests require setting up the following environment variables:
90
101
 
91
102
  ```
92
- WL_KEY=Hc2BHTn3bcrwyPooyYTP rspec spec
103
+ WL_API_KEY=Hc2BHTn3bcrwyPooyYTP # for testing if API v1 support
104
+ WL_OAUTH_KEY=23447e1eaeddf2d1c4af4c9cf88524af2863cb1f72d500dd9328b34735a3f3b0 # for testing if API v2 support
105
+ WL_API_VERSION=2 # By default, API v2 is used for all tests, but can be toggled using this.
106
+ WL_CUSTOMER_ID=242 # Use the given Customer ID for API v2
107
+ rspec spec
93
108
  ```
94
109
 
95
- You can skip teardown for the tests (where the test suite removes/deleted instances of created resources on the testing server) by setting an environment variable `NO_TEARDOWN`.
110
+ You can skip checking support for API v1 by not setting the corresp. env
111
+ variable.
112
+
113
+ You can skip teardown for the tests (where the test suite
114
+ removes/deleted instances of created resources on the testing server) by
115
+ setting an environment variable `NO_TEARDOWN`.
96
116
 
97
- Note that, testing can take a long time, since tests are performed directly on the testing server, and no mocks are used. This is to ensure that the API conforms to the tests, at the same time.
117
+ Note that, testing can take a long time, since tests are performed
118
+ directly on the testing server, and no mocks are used. This is to ensure
119
+ that the API conforms to the tests, at the same time.
98
120
 
99
121
  ### Copyright
100
122
 
101
- Copyright (c) 2012 Whiplash Merchandising/Mark Dickson. See LICENSE.txt for further details.
123
+ Copyright (c) 2015 Whiplash Merchandising/Mark Dickson. See LICENSE.txt for further details.
102
124
 
103
125
 
104
126
  [whiplash]: https://www.whiplashmerch.com/
@@ -8,3 +8,4 @@ require 'whiplash_api/order'
8
8
  require 'whiplash_api/order_item'
9
9
  require 'whiplash_api/shipnotice'
10
10
  require 'whiplash_api/shipnotice_item'
11
+ require 'whiplash_api/web_hook'
@@ -7,10 +7,13 @@ module WhiplashApi
7
7
  self.format = :json
8
8
 
9
9
  class << self
10
+ attr_accessor :api_version, :api_key, :customer_id
11
+
10
12
  def testing!
11
13
  self.site = 'http://testing.whiplashmerch.com/api/'
12
14
 
13
15
  ActiveSupport::Notifications.subscribe("request.active_resource") do |*args|
16
+ puts "[ActiveResource] Headers: #{WhiplashApi::Base.headers}"
14
17
  puts "[ActiveResource] Request: #{args.last[:request_uri]}"
15
18
  puts "[ActiveResource] Response: #{args.last[:result].body}"
16
19
  end if ENV['DEBUG'].present?
@@ -19,8 +22,6 @@ module WhiplashApi
19
22
  # Override the connection that ActiveResource uses, so that we can add our
20
23
  # own error messages for the weird cases when API returns 422 error.
21
24
  def connection(refresh = false)
22
- message = "You must set a valid API Key. Current: #{headers['X-API-KEY'].inspect}"
23
- raise WhiplashApi::Error, message if headers['X-API-KEY'].blank?
24
25
  @connection = WhiplashApi::Connection.new(site, format) if refresh || @connection.nil?
25
26
  super
26
27
  end
@@ -30,12 +31,35 @@ module WhiplashApi
30
31
  Thread.current["active.resource.currentthread.headers"]
31
32
  end
32
33
 
33
- def api_key=(api_key)
34
- headers['X-API-KEY'] = api_key
34
+ def api_version
35
+ @api_version ||= WhiplashApi::DEFAULT_API_VERSION
35
36
  end
36
37
 
37
38
  def api_version=(v = nil)
38
- headers['X-API-VERSION'] = v || 1
39
+ @api_version = v.to_i > 0 ? v.to_i : WhiplashApi::DEFAULT_API_VERSION
40
+ headers['X-API-VERSION'] = @api_version.to_s
41
+ end
42
+
43
+ def api_key=(api_key)
44
+ @api_key = api_key
45
+ raise Error, "You must set a valid API Key." if @api_key.blank?
46
+
47
+ if api_version == 1
48
+ headers['X-API-KEY'] = @api_key
49
+ else
50
+ headers['Authorization'] = "Bearer #{@api_key}"
51
+ end
52
+ end
53
+
54
+ def customer_id=(customer_id = nil)
55
+ @customer_id = customer_id
56
+ headers['X-CUSTOMER-ID'] = customer_id.to_s
57
+ end
58
+
59
+ def reset_headers!
60
+ %w[X-API-KEY X-API-VERSION Authorization].each do |key|
61
+ headers.delete(key)
62
+ end
39
63
  end
40
64
 
41
65
  protected
@@ -4,13 +4,13 @@ module WhiplashApi
4
4
 
5
5
  def request(*arguments)
6
6
  super
7
- rescue ActiveResource::ResourceInvalid => e
7
+ rescue ActiveResource::ResourceInvalid, ActiveResource::ForbiddenAccess, ActiveResource::UnauthorizedAccess => e
8
8
  data = JSON.parse e.response.body
9
9
  case
10
10
  when data['error'].present? && data['error'].downcase =~ /record.*not.*found/
11
- raise WhiplashApi::RecordNotFound
11
+ raise WhiplashApi::RecordNotFound, data['error']
12
12
  when data['error'].present?
13
- raise WhiplashApi::Error, data['error'].humanize
13
+ raise WhiplashApi::Error, "#{e.class}: #{data['error'].humanize}"
14
14
  when data['errors'].present? && data['errors']['base'].present?
15
15
  messages = data['errors']['base']
16
16
  raise WhiplashApi::Error, "Errors were encountered while creating the resource.\n- #{messages.join("\n- ")}"
@@ -18,7 +18,7 @@ module WhiplashApi
18
18
  messages = data['errors'].map{|k,v| v.map{|message| "#{k.humanize} #{message}"}}.flatten
19
19
  raise WhiplashApi::Error, "Errors were encountered while creating the resource.\n- #{messages.join("\n- ")}"
20
20
  else
21
- raise WhiplashApi::Error, "Errors were encountered while creating the resource.\nResponse was: #{e.response.body}"
21
+ raise
22
22
  end
23
23
  end
24
24
  end
@@ -35,16 +35,29 @@ module WhiplashApi
35
35
  raise RecordNotFound, "No order item found with given ID."
36
36
  end
37
37
 
38
+ def separate(id, args={})
39
+ response = self.post("#{id}/separate")
40
+ sanitize_as_resource JSON.parse(response.body)
41
+ rescue WhiplashApi::RecordNotFound
42
+ raise RecordNotFound, "No order item found with given ID."
43
+ end
44
+
38
45
  def delete(*args)
39
46
  super
40
47
  rescue WhiplashApi::RecordNotFound
41
48
  raise RecordNotFound, "No order item was found with given ID."
42
49
  end
50
+
43
51
  end
44
52
 
45
53
  # instance methods
46
54
  def destroy
47
55
  self.class.delete(self.id)
48
56
  end
57
+
58
+ def separate
59
+ self.class.separate(self.id)
60
+ end
61
+
49
62
  end
50
63
  end
@@ -1,3 +1,4 @@
1
1
  module WhiplashApi
2
- VERSION = "1.0.6"
2
+ VERSION = "1.2.3"
3
+ DEFAULT_API_VERSION = 1
3
4
  end
@@ -0,0 +1,18 @@
1
+ module WhiplashApi
2
+ class WebHook < Base
3
+ class << self
4
+
5
+ def delete(*args)
6
+ super
7
+ rescue WhiplashApi::RecordNotFound
8
+ raise RecordNotFound, "No Web Hook was found with given ID."
9
+ end
10
+
11
+ end
12
+
13
+ # instance methods
14
+ def destroy(args={})
15
+ self.class.delete(self.id, args)
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whiplash_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Dickson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-10-23 00:00:00.000000000 Z
12
+ date: 2016-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -138,6 +138,7 @@ files:
138
138
  - lib/whiplash_api/shipnotice.rb
139
139
  - lib/whiplash_api/shipnotice_item.rb
140
140
  - lib/whiplash_api/version.rb
141
+ - lib/whiplash_api/web_hook.rb
141
142
  - whiplash_api.gemspec
142
143
  homepage: http://github.com/ideaoforder/whiplash_api
143
144
  licenses:
@@ -159,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
160
  version: '0'
160
161
  requirements: []
161
162
  rubyforge_project:
162
- rubygems_version: 2.4.8
163
+ rubygems_version: 2.4.5.1
163
164
  signing_key:
164
165
  specification_version: 4
165
166
  summary: Ruby Gem for connecting to the Whiplash Merchandising API