vending-machine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +19 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +155 -0
  6. data/Rakefile +14 -0
  7. data/lib/vend.rb +46 -0
  8. data/lib/vend/errors.rb +12 -0
  9. data/lib/vend/has_resources.rb +34 -0
  10. data/lib/vend/modules/declarative_setters.rb +74 -0
  11. data/lib/vend/modules/finders.rb +5 -0
  12. data/lib/vend/modules/paths.rb +37 -0
  13. data/lib/vend/remote_resource.rb +46 -0
  14. data/lib/vend/resource.rb +100 -0
  15. data/lib/vend/resource_collection.rb +72 -0
  16. data/lib/vend/resources/brand.rb +4 -0
  17. data/lib/vend/resources/contact.rb +4 -0
  18. data/lib/vend/resources/customer.rb +8 -0
  19. data/lib/vend/resources/inventory.rb +14 -0
  20. data/lib/vend/resources/outlet.rb +4 -0
  21. data/lib/vend/resources/payment_type.rb +4 -0
  22. data/lib/vend/resources/price_book_entry.rb +10 -0
  23. data/lib/vend/resources/product.rb +30 -0
  24. data/lib/vend/resources/product_type.rb +7 -0
  25. data/lib/vend/resources/register.rb +9 -0
  26. data/lib/vend/resources/register_sale_payment.rb +6 -0
  27. data/lib/vend/resources/register_sale_product.rb +12 -0
  28. data/lib/vend/resources/sale.rb +17 -0
  29. data/lib/vend/resources/supplier.rb +6 -0
  30. data/lib/vend/resources/tax.rb +13 -0
  31. data/lib/vend/resources/user.rb +4 -0
  32. data/lib/vend/response.rb +9 -0
  33. data/lib/vend/store.rb +103 -0
  34. data/lib/vend/version.rb +3 -0
  35. data/lib/vending-machine.rb +1 -0
  36. data/spec/resource_spec.rb +22 -0
  37. data/spec/resource_urls_spec.rb +59 -0
  38. data/spec/spec_helper.rb +8 -0
  39. data/spec/store_spec.rb +21 -0
  40. data/spec/support/url_assertions.rb +23 -0
  41. data/spec/support/webmock.rb +53 -0
  42. data/vending-machine.gemspec +46 -0
  43. metadata +238 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vending-machine.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 by Mal Curtis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Mal Curtis
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,155 @@
1
+ # Vending Machine
2
+
3
+ Access your [Vend Store](http://vendhq.com) in Ruby! Vending Machine turns the [Vend API](http://docs.vendhq.com/) into Ruby objects, including type coercion and nested objects.
4
+
5
+ ```ruby
6
+ # Create an instance of your store
7
+ store = Vend::Store.new('store_name', 'user_name', 'password')
8
+
9
+ # Get all your products
10
+ store.products.order('name', 'ASC').map &:name
11
+ => ["Coffee (Demo)", "Discount", "T-shirt (Demo)", "test"]
12
+
13
+ # Type coercion
14
+ store.sales.first.total_price.class
15
+ => Float
16
+
17
+ # Nested objects
18
+ store.sales.first.register_sale_products.first.class
19
+ => Vend::RegisterSaleProduct
20
+ ```
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'vending-machine'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install vending-machine
35
+
36
+ ## Usage
37
+
38
+ ### Get a store instance
39
+
40
+ All usage revolves around your store, as per Vend itself
41
+
42
+
43
+ ```ruby
44
+ store = Vend::Store.new('store_name', 'user_name', 'password')
45
+ ```
46
+
47
+ Once you have a store instance you can retrieve your objects
48
+
49
+ ### Customers
50
+
51
+ ```ruby
52
+ customers = store.customers
53
+ customers.first.customer_code
54
+ => ["WALKIN"]
55
+ ```
56
+
57
+ ### Payment Types
58
+
59
+ ```ruby
60
+ types = store.payment_types
61
+ types.first.name
62
+ => "Cash"
63
+ ```
64
+
65
+ ### Products
66
+
67
+ ```ruby
68
+ store.products.map &:name
69
+ => ["Coffee (Demo)", "T-shirt (Demo)", "Discount", "test"]
70
+ ```
71
+
72
+ Scopes available: `active`, `order_by`, `order_direction` and an `order` shortcut to do both order by and direction at once. Scopes are chainable.
73
+
74
+ ```ruby
75
+ store.products.active.map &:name
76
+ => ["T-shirt (Demo)", "Discount", "test"]
77
+
78
+ store.products.order_by('name').map &:name
79
+ => ["Coffee (Demo)", "Discount", "T-shirt (Demo)", "test"]
80
+
81
+ store.products.order_by('name').order_direction('desc').map &:name
82
+ => ["test", "T-shirt (Demo)", "Discount", "Coffee (Demo)"]
83
+
84
+ store.products.order('name', 'DESC').map &:name
85
+ => ["test", "T-shirt (Demo)", "Discount", "Coffee (Demo)"]
86
+ ```
87
+
88
+ ### Register Sales
89
+
90
+ ```ruby
91
+ sales = store.sales
92
+
93
+ sales.map{|sale| sale.total_price + sale.total_tax }
94
+ => [1337.0, 1341.95]
95
+
96
+ sales.first.register_sale_products.map &:name
97
+ => ["T-shirt (Demo)"]
98
+
99
+ sales.first.register_sale_products.first.to_s
100
+ => "#<Vend::RegisterSaleProduct:0x007fe238720d68>"
101
+ ```
102
+
103
+ ### Registers
104
+
105
+ ```ruby
106
+ store.registers.map &:name
107
+ => ["Main Register"]
108
+ ```
109
+
110
+ ### Suppliers
111
+
112
+ ```ruby
113
+ store.suppliers.map &:name
114
+ => ["Mal Curtis", "Vend"]
115
+ ```
116
+
117
+ ### Taxes
118
+
119
+ ```ruby
120
+ store.taxes.map &:name
121
+ => ["NZ GST", "AU GST", "Some other tax"]
122
+ ```
123
+
124
+ The `default` scope will provide you the first returned default tax (note, this still retrieves all taxes).
125
+
126
+ ```ruby
127
+ store.taxes.default.name
128
+ => "NZ GST"
129
+ ```
130
+
131
+ ### Users
132
+
133
+ ```ruby
134
+ store.users.map &:name
135
+ => ["A cashier", "Mal Curtis", "Joe Bloggs"]
136
+ ```
137
+
138
+ ## Status
139
+ * COMPLETE: GET index resources are implemented, with the exception of Stock Control
140
+ * IN PROGRESS: find singular resources `store.products.find('some_id')`
141
+ * IN PROGRESS: create resources `store.sale.create products: [product1, product2]`
142
+ * TODO: Delete resources
143
+ * TODO: Scopes for Customer and Register Sales
144
+
145
+ ## Created By
146
+
147
+ * [Mal Curtis](https://github.com/snikch). Twitter: [snikchnz](https://twitter.com/snikchnz)
148
+
149
+ ## Contributing
150
+
151
+ 1. Fork it
152
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
153
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
154
+ 4. Push to the branch (`git push origin my-new-feature`)
155
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task default: :test
4
+
5
+ # MiniTest
6
+ require "rake/testtask"
7
+ Rake::TestTask.new do |t|
8
+ ENV["TESTOPTS"] = "-v"
9
+ t.pattern = "spec/*_spec.rb"
10
+ end
11
+
12
+ task :console do
13
+ sh "irb -rubygems -I lib -r vend.rb"
14
+ end
data/lib/vend.rb ADDED
@@ -0,0 +1,46 @@
1
+ $: << File.expand_path(File.dirname(__FILE__))
2
+ %w{
3
+ version
4
+ errors
5
+
6
+ modules/declarative_setters
7
+ modules/finders
8
+ modules/paths
9
+
10
+ response
11
+ resource
12
+ remote_resource
13
+ resource_collection
14
+
15
+ resources/brand
16
+ resources/contact
17
+ resources/customer
18
+ resources/outlet
19
+ resources/payment_type
20
+ resources/product_type
21
+ resources/supplier
22
+ resources/register_sale_product
23
+ resources/register_sale_payment
24
+ resources/price_book_entry
25
+ resources/register
26
+ resources/inventory
27
+ resources/product
28
+ resources/user
29
+ resources/sale
30
+ resources/tax
31
+
32
+ has_resources
33
+ store
34
+ }.each { |r| require "vend/#{r}" }
35
+
36
+ module Vend
37
+ class << self
38
+ def scheme
39
+ @scheme || "https"
40
+ end
41
+
42
+ def domain
43
+ @domain || "vendhq.com"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ module Vend
2
+ class Error < StandardError; end
3
+
4
+ class Unauthorized < Error
5
+ def message; "Unauthorized"; end
6
+ end
7
+
8
+ class ValidationFailed < Error; end
9
+
10
+ class ServerError < Error; end
11
+
12
+ end
@@ -0,0 +1,34 @@
1
+ module Vend::HasResources
2
+ def self.included base
3
+ %w(
4
+ customers
5
+ payment_types
6
+ products
7
+ registers
8
+ sales
9
+ suppliers
10
+ taxes
11
+ users
12
+ ).each do |r|
13
+ define_method r do
14
+ if resource = instance_variable_get("@_#{r}")
15
+ resource
16
+ else
17
+ resource_name = r.
18
+ gsub(/s$/,'').
19
+ sub(/^[a-z]/) { $&.capitalize }.
20
+ gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
21
+
22
+ case resource_name
23
+ when 'Taxe'
24
+ resource_name.gsub! /e$/, ''
25
+ end
26
+
27
+ resource = Vend::ResourceCollection.new(self, "Vend::#{resource_name}".constantize)
28
+ instance_variable_set("@_#{r}", resource)
29
+ resource
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,74 @@
1
+ module Vend::DeclarativeSetters
2
+ module ClassMethods
3
+ # The path fragment used for this resource
4
+ def path(path = nil)
5
+ @path = path if path
6
+ @path || (
7
+ self.
8
+ name.
9
+ split('::').
10
+ last.
11
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
12
+ downcase
13
+ ) # Underscored class name
14
+ end
15
+
16
+ # Customize the collection api path
17
+ def collection_api_path(path=nil)
18
+ @collection_api_path = path if path
19
+ @collection_api_path || "#{self.path}s"
20
+ end
21
+
22
+ # Customize the resource api path
23
+ def resource_api_path(path=nil)
24
+ @resource_api_path = path if path
25
+ @resource_api_path || "1.0/#{self.path}"
26
+ end
27
+
28
+ # The name of a child collection.
29
+ def has_many(name, class_proc)
30
+ define_method "#{name}=" do |items|
31
+ (@_has_many ||= {})[name] = items
32
+ end
33
+ define_method name do
34
+ has_many_reader(name, class_proc)
35
+ end
36
+ end
37
+
38
+ # The name of a one to one resource
39
+ def has_one(name, class_proc)
40
+ define_method "#{name}=" do |instance|
41
+ (@_has_one ||= {})[name] = instance
42
+ end
43
+ define_method name do
44
+ has_one_reader(name, class_proc)
45
+ end
46
+ end
47
+
48
+ # Defines a resource collection scope
49
+ def collection_scope name, options = {}
50
+ default = options[:default] || nil
51
+ collection_method name do |value=default|
52
+ value = value == false ? "0" : value.to_s
53
+ current_value = (@parameters ||= {})[name]
54
+ clear if value != current_value
55
+ (@parameters ||= {})[name] = value
56
+ self
57
+ end
58
+ end
59
+
60
+ # Defines a resource collection method
61
+ def collection_method name, &block
62
+ collection_methods[name] = block
63
+ end
64
+
65
+ # Returns all collection methods
66
+ def collection_methods
67
+ (@_collection_methods ||= {})
68
+ end
69
+ end
70
+
71
+ def self.included(base)
72
+ base.extend(ClassMethods)
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ module Vend::Finders
2
+ def find(id)
3
+ @klass.new(id: id, store: store)
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ module Vend::Paths
2
+ # Collection Paths
3
+ module ClassMethods
4
+ def collection_path
5
+ collection_api_path
6
+ end
7
+
8
+ def index_path
9
+ collection_path
10
+ end
11
+
12
+ def create_path
13
+ collection_path
14
+ end
15
+ end
16
+
17
+ # Instance Paths
18
+ def resource_path
19
+ [ self.class.resource_api_path, id ].compact.join("/")
20
+ end
21
+
22
+ def show_path
23
+ resource_path
24
+ end
25
+
26
+ def update_path
27
+ self.class.collection_path
28
+ end
29
+
30
+ def destroy_path
31
+ [ self.class.collection_api_path, id ].compact.join("/")
32
+ end
33
+
34
+ def self.included(base)
35
+ base.extend(ClassMethods)
36
+ end
37
+ end