whiplash_api 0.1.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74a85dbf10a36892fe32bd9e139c24c224e8f646
4
+ data.tar.gz: 098b22a96b9eb3b97473feb2c5731cfdfb2f6261
5
+ SHA512:
6
+ metadata.gz: 838992e0107a5a1f879e36310b758769260b480696e1bf94fd3f5986218b1169ed7b63d1a084c14096a34260433828d450827cf7e985b6d6b53c2211d1adff0f
7
+ data.tar.gz: 0ea784819cc42c368884c4ae1093d032064edf2ee4b50fa4f054155655825a4f6d28c08819166ad6e7cac5b8cc1e50a1667c1b0f4a083b23ce4339a0b61c0dc7
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-gemset
11
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.10.6
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in whiplash_api.gemspec
4
+ gemspec
@@ -1,20 +1,21 @@
1
- Copyright (c) 2012 Whiplash Merchandising/Mark Dickson
1
+ The MIT License (MIT)
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
3
+ Copyright (c) 2015 Nikhil Gupta
10
4
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
13
11
 
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -1,22 +1,26 @@
1
1
  Whiplash API V1 - Ruby Client
2
2
  ================================
3
3
 
4
- This library provides a wrapper around the Whiplash Merchandising REST API for use within Ruby apps or via the console.
4
+ [![Build Status](https://travis-ci.org/nikhgupta/whiplash_api.svg?branch=master)](https://travis-ci.org/nikhgupta/whiplash_api)
5
5
 
6
- == Note
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
- **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: https://github.com/ideaoforder/whiplash-rails-example**
8
+ ### Note
9
9
 
10
- == Requirements
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
11
 
12
- - Ruby 1.8.7+
12
+ ### Requirements
13
+
14
+ - Ruby 1.9.3+
13
15
  - Rubygems
14
16
  - JSON
15
17
  - ActiveResource
16
18
 
17
19
  A valid API key is required to authenticate requests. You can find your API key on your customer account page.
18
20
 
19
- == Installation
21
+ You can, also, use the test API key `Hc2BHTn3bcrwyPooyYTP` to test this client.
22
+
23
+ ### Installation
20
24
 
21
25
  ```
22
26
  gem install whiplash_api
@@ -28,7 +32,7 @@ Or if you're using Bundler:
28
32
  gem 'whiplash_api'
29
33
  ```
30
34
 
31
- == Configuration
35
+ ### Configuration
32
36
 
33
37
  To use the API client in your Ruby code, provide the required credentials as follows:
34
38
 
@@ -36,22 +40,27 @@ To use the API client in your Ruby code, provide the required credentials as fol
36
40
  require 'rubygems'
37
41
  require 'whiplash_api'
38
42
 
39
- WhiplashApi::Base.api_key = 'XXXXXXXXXXXXX'
43
+ WhiplashAPI::Base.api_key = 'XXXXXXXXXXXXX'
40
44
  ```
45
+
46
+ An error will be raised if no API key is provided.
47
+
41
48
  You'll likely want to start by testing in the Sandbox:
42
49
 
43
50
  ```
44
- WhiplashApi::Base.test = true
51
+ WhiplashAPI::Base.testing!
45
52
  ```
46
53
 
47
- == Usage
54
+ You can, also, check a very basic implementation/example inside `exe/whiplash_api` file.
55
+
56
+ ### Usage
48
57
 
49
- The API currently gives you access to your Orders, Items, and OrderItems.
58
+ The API currently gives you access to all endpoints supported by [Whiplash][whiplash] [Merchandising API][api].
50
59
 
51
60
  ```
52
61
  $ irb
53
62
  >
54
- > WhiplashApi::Base.api_key = 'XXXXXXXXXXXXX'
63
+ > WhiplashAPI::Base.api_key = 'XXXXXXXXXXXXX'
55
64
  >
56
65
  > items = WhiplashAPI::Item.all
57
66
  >
@@ -59,13 +68,13 @@ $ irb
59
68
  >
60
69
  > order = WhiplashAPI::Order.last
61
70
  >
62
- > orders = WhiplashAPI::Order.status('shipped')
71
+ > orders = WhiplashAPI::Order.all(:params => {:status => 'shipped', :created_at_min => '2008-01-01'})
63
72
  >
64
73
  > order_item = WhiplashAPI::OrderItem.originator(ID_IN_YOUR_STORE)
65
74
  >
66
75
  ```
67
76
 
68
- == Contributing to the Whiplash API Gem
77
+ ### Contributing to the Whiplash API Gem
69
78
 
70
79
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
71
80
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
@@ -75,7 +84,21 @@ $ irb
75
84
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
76
85
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
77
86
 
78
- == Copyright
87
+ ### Testing Whiplash API Gem
88
+
89
+ You can run the tests on the current version of this gem, by cloning it locally, and then, running:
90
+
91
+ ```
92
+ WL_KEY=Hc2BHTn3bcrwyPooyYTP rspec spec
93
+ ```
94
+
95
+ 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.
96
+
97
+ ### Copyright
79
98
 
80
99
  Copyright (c) 2012 Whiplash Merchandising/Mark Dickson. See LICENSE.txt for further details.
81
100
 
101
+
102
+ [whiplash]: https://www.whiplashmerch.com/
103
+ [api]: https://www.whiplashmerch.com/developers/api
104
+ [app]: https://github.com/ideaoforder/whiplash-rails-example
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "whiplash_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "thor"
4
+ require "whiplash_api"
5
+ require "whiplash_api/cli"
6
+
7
+ WhiplashApi::CLI.start ARGV
@@ -1,7 +1,10 @@
1
- require "rubygems"
2
- require 'active_resource'
1
+ require "active_resource"
2
+ require "whiplash_api/version"
3
3
 
4
4
  require 'whiplash_api/base'
5
+ require 'whiplash_api/connection'
5
6
  require 'whiplash_api/item'
6
7
  require 'whiplash_api/order'
7
- require 'whiplash_api/order_item'
8
+ require 'whiplash_api/order_item'
9
+ require 'whiplash_api/shipnotice'
10
+ require 'whiplash_api/shipnotice_item'
@@ -1,54 +1,50 @@
1
- module WhiplashAPI
2
-
3
- # Example Class Method
4
- # def reserved(*names)
5
- # class_variable_set(:@@reserved, names.collect{|x| x.to_s})
6
- # end
1
+ module WhiplashApi
2
+ class Error < ::StandardError; end
3
+ class RecordNotFound < Error; end
7
4
 
8
5
  class Base < ActiveResource::Base
9
- extend WhiplashAPI
10
-
11
- self.site = 'https://www.whiplashmerch.com/api/'
6
+ self.site = 'https://www.whiplashmerch.com/api/'
12
7
  self.format = :json
13
-
14
- # Thanks to Brandon Keepers for this little nugget:
15
- # http://opensoul.org/blog/archives/2010/02/16/active-resource-in-practice/
8
+
16
9
  class << self
17
- # If headers are not defined in a given subclass, then obtain
18
- # headers from the superclass.
19
- def headers
20
- if defined?(@headers)
21
- @headers
22
- elsif superclass != Object && superclass.headers
23
- superclass.headers
24
- else
25
- @headers ||= {}
26
- end
10
+ def testing!
11
+ self.site = 'http://testing.whiplashmerch.com/api/'
12
+
13
+ ActiveSupport::Notifications.subscribe("request.active_resource") do |*args|
14
+ puts "[ActiveResource] Request: #{args.last[:request_uri]}"
15
+ puts "[ActiveResource] Response: #{args.last[:result].body}"
16
+ end if ENV['DEBUG'].present?
27
17
  end
28
-
18
+
19
+ # Override the connection that ActiveResource uses, so that we can add our
20
+ # own error messages for the weird cases when API returns 422 error.
21
+ 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
+ @connection = WhiplashApi::Connection.new(site, format) if refresh || @connection.nil?
25
+ super
26
+ end
27
+
29
28
  def api_key=(api_key)
30
29
  headers['X-API-KEY'] = api_key
31
30
  end
32
-
31
+
33
32
  def api_version=(v)
34
33
  headers['X-API-VERSION'] = v
35
34
  end
36
-
37
- def local=(v)
38
- self.site = 'http://localhost:3000/api/' if v
39
- end
40
-
41
- def test=(v)
42
- self.site = 'http://testing.whiplashmerch.com/api/' if v
35
+
36
+ protected
37
+
38
+ def sanitize_as_resource(collection)
39
+ return collection if collection.blank?
40
+ as_array = collection.is_a?(Array)
41
+ collection = [collection].flatten.map do |resource|
42
+ resource = self.new(resource)
43
+ resource.instance_variable_set("@persisted", true)
44
+ resource
45
+ end
46
+ as_array ? collection : collection.first
43
47
  end
44
-
45
48
  end
46
-
47
- # Example Instance Method
48
- # def class_name
49
- # self.class.name.split('::').last.downcase
50
- # end
51
-
52
49
  end
53
-
54
- end
50
+ end
@@ -0,0 +1,49 @@
1
+ require 'pry'
2
+
3
+ # An example implementation of this gem.
4
+ module WhiplashApi
5
+ class CLI < Thor
6
+
7
+ class_option :test, boolean: true, desc: "run using test API endpoints."
8
+ class_option :local, boolean: true, desc: "run using localhost API endpoints."
9
+
10
+ desc "list [API_KEY]", "Fetch items for the current account"
11
+ def list(api_key = nil)
12
+ setup! api_key
13
+
14
+ items = WhiplashApi::Item.all
15
+ items.each do |item|
16
+ message = "%16s" % item.sku
17
+ message += "%20s" % item.description
18
+ message += " %3s " % (item.available ? "Yes" : "No")
19
+ message += item.title
20
+ say_status "Product", message
21
+ end
22
+ end
23
+
24
+ desc "sku ID [API_KEY]", "Fetch item by SKU"
25
+ def sku(id, api_key = nil)
26
+ setup! api_key
27
+
28
+ item = WhiplashApi::Item.sku id
29
+ message = "%16s" % item.sku
30
+ message += "%20s" % item.description
31
+ message += " %3s " % (item.available ? "Yes" : "No")
32
+ message += item.title
33
+ say_status "Product", message
34
+ end
35
+
36
+ desc "test", "test"
37
+ def test
38
+ setup!
39
+ binding.pry
40
+ end
41
+
42
+ private
43
+
44
+ def setup!(api_key = nil)
45
+ WhiplashApi::Base.api_key = api_key || ENV['WL_KEY']
46
+ WhiplashApi::Base.testing! if ENV['API_ENV'] == "test" || options[:test]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ module WhiplashApi
2
+ class Connection < ActiveResource::Connection
3
+ private
4
+
5
+ def request(*arguments)
6
+ super
7
+ rescue ActiveResource::ResourceInvalid => e
8
+ data = JSON.parse e.response.body
9
+ case
10
+ when data['error'].present? && data['error'].downcase =~ /record.*not.*found/
11
+ raise WhiplashApi::RecordNotFound
12
+ when data['error'].present?
13
+ raise WhiplashApi::Error, data['error'].humanize
14
+ when data['errors'].present? && data['errors']['base'].present?
15
+ messages = data['errors']['base']
16
+ raise WhiplashApi::Error, "Errors were encountered while creating the resource.\n- #{messages.join("\n- ")}"
17
+ when data['errors'].present?
18
+ messages = data['errors'].map{|k,v| v.map{|message| "#{k.humanize} #{message}"}}.flatten
19
+ raise WhiplashApi::Error, "Errors were encountered while creating the resource.\n- #{messages.join("\n- ")}"
20
+ else
21
+ raise WhiplashApi::Error, "Errors were encountered while creating the resource.\nResponse was: #{e.response.body}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -1,15 +1,57 @@
1
- module WhiplashAPI
2
-
1
+ module WhiplashApi
3
2
  class Item < Base
4
3
  class << self
4
+ def count(args={})
5
+ self.get(:count, args)
6
+ end
7
+
5
8
  def sku(sku, args={})
6
- self.get(:sku, {:sku => sku}.merge(args))
9
+ sanitize_as_resource self.get(:sku, { sku: sku }.merge(args))
7
10
  end
8
-
11
+
12
+ def group(id, args={})
13
+ sanitize_as_resource self.get("group/#{id}", args)
14
+ end
15
+
9
16
  def originator(id, args={})
10
- self.get(:originator, {:originator_id => id}.merge(args))
17
+ sanitize_as_resource self.get("originator/#{id}", args)
18
+ end
19
+
20
+ def find_or_create(id, args={})
21
+ self.find(id)
22
+ rescue WhiplashApi::RecordNotFound
23
+ create(args)
24
+ end
25
+
26
+ def find_or_create_by_sku(sku, args={})
27
+ first_by_sku(sku) || create(args.merge(sku: sku))
28
+ end
29
+
30
+ def find_or_create_by_originator_id(id, args={})
31
+ originator(id) || create(args.merge(originator_id: id))
32
+ end
33
+
34
+ def update(args={})
35
+ item = self.originator(args[:originator_id])
36
+ raise RecordNotFound, "No item was found with given Originator ID." unless item
37
+ item.update_attributes(args) ? item : nil
38
+ end
39
+
40
+ def delete(*args)
41
+ super
42
+ rescue WhiplashApi::RecordNotFound
43
+ raise RecordNotFound, "No Item was found with given ID."
44
+ end
45
+
46
+ # additional useful methods:
47
+ def first_by_sku(sku, args={})
48
+ self.sku(sku, args).first
11
49
  end
12
50
  end
13
- end
14
51
 
15
- end
52
+ # instance methods
53
+ def destroy(args={})
54
+ self.class.delete(self.id, args)
55
+ end
56
+ end
57
+ end
@@ -1,15 +1,134 @@
1
- module WhiplashAPI
2
-
1
+ module WhiplashApi
3
2
  class Order < Base
4
3
  class << self
5
- def status(status, args={})
6
- self.get(:status, {:status => status}.merge(args))
4
+ def status_for(status)
5
+ status_mapping[status].to_s.titleize
7
6
  end
8
-
7
+ def status_code_for(status)
8
+ status_mapping.invert[status.to_s.underscore.to_sym]
9
+ end
10
+
11
+ def count(args={})
12
+ self.get(:count, args)
13
+ end
14
+
9
15
  def originator(id, args={})
10
- self.get(:originator, {:originator_id => id}.merge(args))
16
+ sanitize_as_resource self.get("originator/#{id}", args)
17
+ end
18
+
19
+ def find_or_create_by_originator_id(id, args={})
20
+ originator(id) || create(args.merge(originator_id: id))
21
+ end
22
+
23
+ def update(args={})
24
+ order = self.originator(args[:originator_id])
25
+ raise RecordNotFound, "No order found with given Originator ID." unless order
26
+ order.update_attributes(args) ? order : false
27
+ end
28
+
29
+ def pause(id, args={})
30
+ self.put("#{id}/pause")
31
+ end
32
+
33
+ def release(id, args={})
34
+ self.put("#{id}/release")
35
+ end
36
+
37
+ def cancel(id, args={})
38
+ self.put("#{id}/cancel")
39
+ end
40
+
41
+ def uncancel(id, args={})
42
+ self.put("#{id}/uncancel")
43
+ end
44
+
45
+ private
46
+
47
+ def status_mapping
48
+ {
49
+ 35 => :quote,
50
+ 40 => :cancelled,
51
+ 45 => :closed_by_originator,
52
+ 50 => :unpaid,
53
+ 75 => :pending_return,
54
+ 77 => :return_verified,
55
+ 80 => :items_unavailable,
56
+ 90 => :paused,
57
+ 95 => :unassignable,
58
+ 100 => :processing,
59
+ 120 => :printed,
60
+ 150 => :picked,
61
+ 155 => :prepacking_in_progress,
62
+ 160 => :packed,
63
+ 200 => :label_scheduled_for_purchase,
64
+ 250 => :label_purchased,
65
+ 300 => :shipped,
66
+ 350 => :delivered,
67
+ 400 => :returned_undeliverable,
68
+ 410 => :replacement_requested,
69
+ 430 => :exchanged,
70
+ 450 => :refund_requested
71
+ }
11
72
  end
12
73
  end
13
- end
14
74
 
15
- end
75
+ # instance methods
76
+
77
+ def unshipped?
78
+ self.status.to_i < 300
79
+ end
80
+
81
+ def paused?
82
+ self.status.to_i == 90
83
+ end
84
+
85
+ def cancelled?
86
+ self.status.to_i == 40
87
+ end
88
+
89
+ def unprocessed?
90
+ self.status.to_i < 100
91
+ end
92
+
93
+ def being_processed?
94
+ self.status.to_i == 100
95
+ end
96
+ alias :processing? :being_processed?
97
+
98
+ def pause
99
+ self.put(:pause)
100
+ end
101
+
102
+ def release
103
+ self.put(:release)
104
+ end
105
+
106
+ def cancel
107
+ self.put(:cancel)
108
+ end
109
+
110
+ def uncancel
111
+ self.put(:uncancel)
112
+ end
113
+
114
+ # def pause
115
+ # self.put(:pause) and return if unshipped?
116
+ # raise Error, "Orders may only be paused before they have been shipped."
117
+ # end
118
+
119
+ # def release
120
+ # self.put(:release) and return if paused?
121
+ # raise Error, "Cannot release an order that has not been paused."
122
+ # end
123
+
124
+ # def cancel
125
+ # self.put(:cancel) and return if unshipped?
126
+ # raise Error, "Orders may only be cancelled before they have been shipped."
127
+ # end
128
+
129
+ # def uncancel
130
+ # self.put(:uncancel) and return if cancelled?
131
+ # raise Error, "Cannot uncancel an order that has not been cancelled."
132
+ # end
133
+ end
134
+ end
@@ -1,11 +1,50 @@
1
- module WhiplashAPI
2
-
1
+ module WhiplashApi
3
2
  class OrderItem < Base
4
3
  class << self
4
+
5
+ # Finders for OrderItem require `order_id` key to be present.
6
+ # The following routine enforces this condition.
7
+ #
8
+ def find(*args)
9
+ scope = args.slice!(0)
10
+ options = args.slice!(0) || {}
11
+
12
+ if [:all, :first, :last, :one].include?(scope)
13
+ invalid = options[:params].to_s.empty? || options[:params][:order_id].to_s.empty?
14
+ raise Error, "You must supply an Order ID (as parameter) to retrieve order items for." if invalid
15
+ end
16
+
17
+ super(scope, options)
18
+ end
19
+
5
20
  def originator(id, args={})
6
- self.get(:originator, {:originator_id => id}.merge(args))
21
+ self.collection_name = "order_items/originator"
22
+ order = self.find(id, args) rescue nil
23
+ self.collection_name = "order_items"
24
+ order
25
+ end
26
+
27
+ def find_or_create_by_originator_id(id, args={})
28
+ originator(id) || create(args.merge(originator_id: id))
29
+ end
30
+
31
+ def update(id, args={})
32
+ response = self.put(id, {}, args.to_json)
33
+ response.code.to_i >= 200 && response.code.to_i < 300
34
+ rescue WhiplashApi::RecordNotFound
35
+ raise RecordNotFound, "No order item found with given ID."
36
+ end
37
+
38
+ def delete(*args)
39
+ super
40
+ rescue WhiplashApi::RecordNotFound
41
+ raise RecordNotFound, "No order item was found with given ID."
7
42
  end
8
43
  end
9
- end
10
44
 
11
- end
45
+ # instance methods
46
+ def destroy
47
+ self.class.delete(self.id)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,68 @@
1
+ module WhiplashApi
2
+ class Shipnotice < Base
3
+ class << self
4
+
5
+ def status_for(status_code)
6
+ status_mapping[status_code].to_s.titleize
7
+ end
8
+ def status_code_for(status)
9
+ status_mapping.invert[status.to_s.underscore.to_sym]
10
+ end
11
+
12
+ def warehouse_for(warehouse_code)
13
+ warehouse_mapping[warehouse_code].to_s.titleize
14
+ end
15
+ def warehouse_code_for(warehouse)
16
+ warehouse_mapping.invert[warehouse]
17
+ end
18
+
19
+ def count(args={})
20
+ self.get(:count, args)
21
+ end
22
+
23
+ def update(id, args={})
24
+ response = self.put(id, {}, args.to_json)
25
+ response.code.to_i >= 200 && response.code.to_i < 300
26
+ rescue WhiplashApi::RecordNotFound
27
+ raise RecordNotFound, "No Shipment notice found with given ID."
28
+ end
29
+
30
+ def delete(*args)
31
+ super
32
+ rescue WhiplashApi::RecordNotFound
33
+ raise RecordNotFound, "No Shipment notice found with given ID."
34
+ end
35
+
36
+ private
37
+
38
+ def status_mapping
39
+ {
40
+ 25 => :unexpected,
41
+ 50 => :draft,
42
+ 100 => :in_transit,
43
+ 150 => :arrived,
44
+ 200 => :processing,
45
+ 250 => :problem,
46
+ 300 => :completed,
47
+ }
48
+ end
49
+
50
+ def warehouse_mapping
51
+ { 1 => "Ann Arbor", 2 => "San Francisco", 3 => "London" }
52
+ end
53
+ end
54
+
55
+ # instance methods
56
+ def received?
57
+ self.status.to_i > 100
58
+ end
59
+
60
+ def processed?
61
+ self.status.to_i > 200
62
+ end
63
+
64
+ def destroy
65
+ self.class.delete(self.id)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,39 @@
1
+ module WhiplashApi
2
+ class ShipnoticeItem < Base
3
+ class << self
4
+
5
+ # Finders for ShipnoticeItem require `shipnotice_id` key to be present.
6
+ # The following routine enforces this condition.
7
+ #
8
+ def find(*args)
9
+ scope = args.slice!(0)
10
+ options = args.slice!(0) || {}
11
+
12
+ if [:all, :first, :last, :one].include?(scope)
13
+ invalid = options[:params].to_s.empty? || options[:params][:shipnotice_id].to_s.empty?
14
+ raise Error, "You must supply a Shipment Notice ID (as parameter) to retrieve shipnotice items for." if invalid
15
+ end
16
+
17
+ super(scope, options)
18
+ end
19
+
20
+ def update(id, args={})
21
+ response = self.put(id, {}, args.to_json)
22
+ response.code.to_i >= 200 && response.code.to_i < 300
23
+ rescue WhiplashApi::RecordNotFound
24
+ raise RecordNotFound, "No shipnotice item found with given ID."
25
+ end
26
+
27
+ def delete(*args)
28
+ super
29
+ rescue WhiplashApi::RecordNotFound
30
+ raise RecordNotFound, "No shipnotice item found with given ID."
31
+ end
32
+ end
33
+
34
+ # instance methods
35
+ def destroy
36
+ self.class.delete(self.id)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module WhiplashApi
2
+ VERSION = "1.0.3"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'whiplash_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "whiplash_api"
8
+ spec.version = WhiplashApi::VERSION
9
+ spec.authors = ["Mark Dickson"]
10
+ spec.email = ["mark@sitesteaders.com"]
11
+
12
+ spec.summary = %q{Ruby Gem for connecting to the Whiplash Merchandising API}
13
+ spec.description = %q{Ruby Gem for connecting to the Whiplash Merchandising API}
14
+ spec.homepage = "http://github.com/ideaoforder/whiplash_api"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+
27
+ spec.add_dependency "thor"
28
+ spec.add_dependency "activesupport"
29
+ spec.add_dependency "activeresource", "~> 4.0.0"
30
+ end
metadata CHANGED
@@ -1,175 +1,164 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: whiplash_api
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- segments_generated: true
11
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
12
5
  platform: ruby
13
- authors:
6
+ authors:
14
7
  - Mark Dickson
15
8
  autorequire:
16
- bindir: bin
9
+ bindir: exe
17
10
  cert_chain: []
18
-
19
- date: 2012-09-17 00:00:00 -04:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
23
21
  prerelease: false
24
- type: :runtime
25
- name: activesupport
26
- version_requirements: &id001 !ruby/object:Gem::Requirement
27
- none: false
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- hash: 9
32
- segments:
33
- - 2
34
- - 3
35
- - 5
36
- segments_generated: true
37
- version: 2.3.5
38
- requirement: *id001
39
- - !ruby/object:Gem::Dependency
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
40
35
  prerelease: false
41
- type: :runtime
42
- name: activeresource
43
- version_requirements: &id002 !ruby/object:Gem::Requirement
44
- none: false
45
- requirements:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
46
45
  - - ">="
47
- - !ruby/object:Gem::Version
48
- hash: 9
49
- segments:
50
- - 2
51
- - 3
52
- - 5
53
- segments_generated: true
54
- version: 2.3.5
55
- requirement: *id002
56
- - !ruby/object:Gem::Dependency
57
- prerelease: false
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
58
48
  type: :development
59
- name: rdoc
60
- version_requirements: &id003 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ~>
64
- - !ruby/object:Gem::Version
65
- hash: 31
66
- segments:
67
- - 3
68
- - 12
69
- segments_generated: true
70
- version: "3.12"
71
- requirement: *id003
72
- - !ruby/object:Gem::Dependency
73
49
  prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
74
62
  type: :development
75
- name: bundler
76
- version_requirements: &id004 !ruby/object:Gem::Requirement
77
- none: false
78
- requirements:
79
- - - ~>
80
- - !ruby/object:Gem::Version
81
- hash: 23
82
- segments:
83
- - 1
84
- - 0
85
- - 0
86
- segments_generated: true
87
- version: 1.0.0
88
- requirement: *id004
89
- - !ruby/object:Gem::Dependency
90
63
  prerelease: false
91
- type: :development
92
- name: jeweler
93
- version_requirements: &id005 !ruby/object:Gem::Requirement
94
- none: false
95
- requirements:
96
- - - ~>
97
- - !ruby/object:Gem::Version
98
- hash: 63
99
- segments:
100
- - 1
101
- - 8
102
- - 4
103
- segments_generated: true
104
- version: 1.8.4
105
- requirement: *id005
106
- - !ruby/object:Gem::Dependency
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
107
77
  prerelease: false
108
- type: :development
109
- name: rcov
110
- version_requirements: &id006 !ruby/object:Gem::Requirement
111
- none: false
112
- requirements:
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
113
80
  - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- segments_generated: true
119
- version: "0"
120
- requirement: *id006
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activeresource
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 4.0.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 4.0.0
121
111
  description: Ruby Gem for connecting to the Whiplash Merchandising API
122
- email: mark@sitesteaders.com
123
- executables: []
124
-
112
+ email:
113
+ - mark@sitesteaders.com
114
+ executables:
115
+ - whiplash_api
125
116
  extensions: []
126
-
127
- extra_rdoc_files:
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
128
123
  - LICENSE.txt
129
- - README.rdoc
130
- files:
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - exe/whiplash_api
131
129
  - lib/whiplash_api.rb
132
130
  - lib/whiplash_api/base.rb
131
+ - lib/whiplash_api/cli.rb
132
+ - lib/whiplash_api/connection.rb
133
133
  - lib/whiplash_api/item.rb
134
134
  - lib/whiplash_api/order.rb
135
135
  - lib/whiplash_api/order_item.rb
136
- - LICENSE.txt
137
- - README.rdoc
138
- has_rdoc: true
136
+ - lib/whiplash_api/shipnotice.rb
137
+ - lib/whiplash_api/shipnotice_item.rb
138
+ - lib/whiplash_api/version.rb
139
+ - whiplash_api.gemspec
139
140
  homepage: http://github.com/ideaoforder/whiplash_api
140
- licenses:
141
+ licenses:
141
142
  - MIT
143
+ metadata: {}
142
144
  post_install_message:
143
145
  rdoc_options: []
144
-
145
- require_paths:
146
+ require_paths:
146
147
  - lib
147
- required_ruby_version: !ruby/object:Gem::Requirement
148
- none: false
149
- requirements:
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
150
  - - ">="
151
- - !ruby/object:Gem::Version
152
- hash: 3
153
- segments:
154
- - 0
155
- segments_generated: true
156
- version: "0"
157
- required_rubygems_version: !ruby/object:Gem::Requirement
158
- none: false
159
- requirements:
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
160
155
  - - ">="
161
- - !ruby/object:Gem::Version
162
- hash: 3
163
- segments:
164
- - 0
165
- segments_generated: true
166
- version: "0"
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
167
158
  requirements: []
168
-
169
159
  rubyforge_project:
170
- rubygems_version: 1.3.7
160
+ rubygems_version: 2.4.8
171
161
  signing_key:
172
- specification_version: 3
162
+ specification_version: 4
173
163
  summary: Ruby Gem for connecting to the Whiplash Merchandising API
174
164
  test_files: []
175
-