bill_hicks 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27f9faa7867304abe4e10327832c3149c0aa4d6c
4
+ data.tar.gz: c23114e4d48e20de341bbefb79f534f2e683d9ce
5
+ SHA512:
6
+ metadata.gz: 3e1654caa5bf30a1efa071c02b728674d72157ac8ea182a16e13be6b10055aaeb727a1ba076767915f083b4ece635af6c2d8208d227c0d08acaeb56efab3a5f5
7
+ data.tar.gz: 0df57a670548257548f98773e42561db169c2fda0a00355b9b2f428ea2cc95a68e3c17b68d4ad48e57bc64474cc1e6b2a8e09194f7478f79df7e4819674216b9
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ bill_hicks
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bill_hicks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dale Campbell
4
+
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:
11
+
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.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # BillHicks
2
+
3
+ Ruby library for Bill Hicks ERP system.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bill_hicks'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bill_hicks
20
+
21
+ ## Usage
22
+
23
+ **Note:** Nearly all methods require `:username` and `:password` keys in the options hash.
24
+
25
+ ```ruby
26
+ options = {
27
+ username: 'dealer@example.com',
28
+ password: 'sekret-passwd'
29
+ }
30
+ ```
31
+
32
+ ### BillHicks::Catalog
33
+
34
+ To get all items in the catalog:
35
+
36
+ ```ruby
37
+ catalog = BillHicks::Catalog.all(options)
38
+ ```
39
+
40
+ See `BillHicks::Catalog` for the response structure.
41
+
42
+ ### BillHicks::Inventory
43
+
44
+ To get your inventory details (availability, price, etc.):
45
+
46
+ ```ruby
47
+ inventory = BillHicks::Inventory.all(options)
48
+ ```
49
+
50
+ See `BillHicks::Inventory` for the response structure.
51
+
52
+ ### BillHicks::Category
53
+
54
+ Returns an array of category codes and descriptions.
55
+
56
+ ```ruby
57
+ categories = BillHicks::Category.all(options)
58
+
59
+ # [
60
+ # {:code=>"H648", :description=>"AIRGUNS"},
61
+ # {:code=>"H610", :description=>"AMMUNITION"},
62
+ # ...,
63
+ # ]
64
+ ```
65
+
66
+ ### BillHicks::Order
67
+
68
+ To build and submit an order, the basic steps are: 1) instantiate an Order object, 2) add header
69
+ information, 3) add item information (multiple items if needed), 4) submit the order.
70
+
71
+ ```ruby
72
+ # Instantiate the Order instance, passing in your :username and :password
73
+ order = BillHicks::Order.new(options)
74
+
75
+ # Add header information:
76
+ header_opts = {
77
+ customer: '...', # customer number
78
+ purchase_order: '...', # application specific purchase order
79
+ ffl: '...', # your FFL number
80
+ shipping: { # shipping information (all fields except :address_2 are required)
81
+ name: '...',
82
+ address_1: '...',
83
+ address_2: '...',
84
+ city: '...',
85
+ state: '...',
86
+ zip: '...',
87
+ },
88
+
89
+ # Optional fields:
90
+ shipping_method: '...',
91
+ notes: '...',
92
+ }
93
+ order.add_header(header_opts)
94
+
95
+ # Add item information:
96
+ item_opts = {
97
+ item_number: '...', # Bill Hicks item number
98
+ description: '...',
99
+ quantity: 1,
100
+ price: '123.45', # Decimal formatted price, without currency sign
101
+ }
102
+ order.add_item(item_opts) # Multiple items may be added, just call #add_item for each one.
103
+
104
+ # Submit the order (returns true on success, raises an exception on failgure):
105
+ order.submit!
106
+ ```
107
+
108
+ See `BillHicks::Order` for details on required options.
109
+
110
+ ## Development
111
+
112
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
113
+
114
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ammoready/bill_hicks.
119
+
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
124
+
data/Rakefile ADDED
@@ -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,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bill_hicks/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bill_hicks"
8
+ spec.version = BillHicks::VERSION
9
+ spec.authors = ["Dale Campbell"]
10
+ spec.email = ["oshuma@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby library for Bill Hicks ERP system}
13
+ spec.description = %q{}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bill_hicks"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/bill_hicks.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'bill_hicks/version'
2
+
3
+ require 'csv'
4
+ require 'net/ftp'
5
+
6
+ require 'bill_hicks/base'
7
+ require 'bill_hicks/catalog'
8
+ require 'bill_hicks/category'
9
+ require 'bill_hicks/inventory'
10
+ require 'bill_hicks/order'
11
+
12
+ module BillHicks
13
+ class InvalidOrder < StandardError; end
14
+ class NotAuthenticated < StandardError; end
15
+ end
@@ -0,0 +1,39 @@
1
+ module BillHicks
2
+ class Base
3
+
4
+ FTP_HOST = 'billhicksco.hostedftp.com'
5
+ FTP_DIR = 'AmmoReady'
6
+
7
+ protected
8
+
9
+ # Wrapper to `self.requires!` that can be used as an instance method.
10
+ def requires!(*args)
11
+ self.class.requires!(*args)
12
+ end
13
+
14
+ def self.requires!(hash, *params)
15
+ params.each do |param|
16
+ if param.is_a?(Array)
17
+ raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash.has_key?(param.first)
18
+
19
+ valid_options = param[1..-1]
20
+ raise ArgumentError.new("Parameter: #{param.first} must be one of: #{valid_options.join(', ')}") unless valid_options.include?(hash[param.first])
21
+ else
22
+ raise ArgumentError.new("Missing required parameter: #{param}") unless hash.has_key?(param)
23
+ end
24
+ end
25
+ end
26
+
27
+ def connect(options = {})
28
+ requires!(options, :username, :password)
29
+
30
+ Net::FTP.open(FTP_HOST, options[:username], options[:password]) do |ftp|
31
+ ftp.passive = true
32
+ yield ftp
33
+ end
34
+ rescue Net::FTPPermError
35
+ raise BillHicks::NotAuthenticated
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,64 @@
1
+ module BillHicks
2
+ # Catalog item response structure:
3
+ #
4
+ # {
5
+ # product_name: "...",
6
+ # universal_product_code: "...",
7
+ # short_description: "...",
8
+ # long_description: "...",
9
+ # category_code: "...",
10
+ # category_description: "...",
11
+ # product_price: "...",
12
+ # small_image_path: "...",
13
+ # large_image_path: "...",
14
+ # product_weight: "...",
15
+ # marp: "...",
16
+ # msrp: "...",
17
+ # upc: "..." # alias of ':universal_product_code'
18
+ # }
19
+ class Catalog < Base
20
+
21
+ # FIXME: Change this back to the normal filename as soon as they fix the headers in this file.
22
+ # CATALOG_FILENAME = 'billhickscatalog.csv'
23
+ CATALOG_FILENAME = 'billhickscatalog-fixed.csv'
24
+
25
+
26
+ def initialize(options = {})
27
+ requires!(options, :username, :password)
28
+ @options = options
29
+ end
30
+
31
+ def self.all(options = {})
32
+ requires!(options, :username, :password)
33
+ new(options).all
34
+ end
35
+
36
+ # Returns an array of hashes with the catalog item details.
37
+ def all
38
+ catalog = []
39
+
40
+ connect(@options) do |ftp|
41
+ ftp.chdir(FTP_DIR)
42
+
43
+ lines = ftp.gettextfile(CATALOG_FILENAME, nil)
44
+
45
+ STDOUT.puts "-- DEBUG: #{self.class}: #{lines.inspect}"
46
+
47
+ CSV.parse(lines, headers: :first_row) do |row|
48
+ row_hash = {}
49
+
50
+ # Turn the row into a hash with header names as symbolized keys.
51
+ row.each { |r| row_hash[r.first.to_sym] = r.last }
52
+
53
+ # Alias the ':universal_product_code' as ':upc'.
54
+ row_hash[:upc] = row_hash[:universal_product_code]
55
+
56
+ catalog << row_hash
57
+ end
58
+ end
59
+
60
+ catalog
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,40 @@
1
+ module BillHicks
2
+ # Category item response structure:
3
+ #
4
+ # {
5
+ # code: "...", # ':category_code' in Catalog response.
6
+ # description: "..." # ':category_description' in Catalog response.
7
+ # }
8
+ class Category < Base
9
+
10
+ def initialize(options = {})
11
+ requires!(options, :username, :password)
12
+ @options = options
13
+ end
14
+
15
+ def self.all(options = {})
16
+ requires!(options, :username, :password)
17
+ new(options).all
18
+ end
19
+
20
+ # Returns an array of hashes with category details.
21
+ def all
22
+ categories = []
23
+
24
+ # Categories are listed in catalog csv, so fetch that.
25
+ catalog = Catalog.all(@options)
26
+ catalog.each do |item|
27
+ categories << {
28
+ code: item[:category_code],
29
+ description: item[:category_description]
30
+ }
31
+ end
32
+
33
+ categories.uniq! { |c| c[:description] }
34
+ categories.sort_by! { |c| c[:description] }
35
+
36
+ categories
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,46 @@
1
+ module BillHicks
2
+ # Inventory item response structure:
3
+ #
4
+ # {
5
+ # product: "...",
6
+ # upc: "...",
7
+ # quantity: "..."
8
+ # }
9
+ class Inventory < Base
10
+
11
+ INVENTORY_FILENAME = 'billhicksinventory.csv'
12
+
13
+
14
+ def initialize(options = {})
15
+ requires!(options, :username, :password)
16
+ @options = options
17
+ end
18
+
19
+ def self.all(options = {})
20
+ requires!(options, :username, :password)
21
+ new(options).all
22
+ end
23
+
24
+ # Returns an array of hashes with the inventory item details.
25
+ def all
26
+ inventory = []
27
+
28
+ connect(@options) do |ftp|
29
+ ftp.chdir(FTP_DIR)
30
+
31
+ lines = ftp.gettextfile(INVENTORY_FILENAME, nil)
32
+
33
+ CSV.parse(lines, headers: :first_row) do |row|
34
+ inventory << {
35
+ product: row.fetch('Product'),
36
+ upc: row.fetch('UPC'),
37
+ quantity: (Integer(row.fetch('Qty Avail')) rescue 0)
38
+ }
39
+ end
40
+ end
41
+
42
+ inventory
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,120 @@
1
+ module BillHicks
2
+ # To submit an order:
3
+ #
4
+ # * Instantiate a new Order, passing in `:username` and `:password`
5
+ # * Call {#add_header}
6
+ # * Call {#add_item} for each item on the order
7
+ # * Call {#submit!} to send the order
8
+ #
9
+ # See each method for a list of required options.
10
+ class Order < Base
11
+
12
+ ORDER_UPLOAD_DIR = [FTP_DIR, 'toBHC'].join('/')
13
+
14
+ # @option options [String] :username *required*
15
+ # @option options [String] :password *required*
16
+ def initialize(options = {})
17
+ requires!(options, :username, :password)
18
+ @options = options
19
+ @items = []
20
+ end
21
+
22
+ # @param [Hash] header
23
+ # * :customer [String] *required*
24
+ # * :purchase_order [String] *required*
25
+ # * :ffl [String] *required*
26
+ # * :shipping [Hash] *required*
27
+ # * :name [String] *required*
28
+ # * :address_1 [String] *required*
29
+ # * :address_2 [String] optional
30
+ # * :city [String] *required*
31
+ # * :state [String] *required*
32
+ # * :zip [String] *required*
33
+ def add_header(header = {})
34
+ requires!(header, :customer, :purchase_order, :ffl, :shipping)
35
+ requires!(header[:shipping], :name, :address_1, :city, :state, :zip)
36
+ @header = header
37
+ end
38
+
39
+ # @option item [String] :item_number *required*
40
+ # @option item [Integer] :quantity *required*
41
+ # @option item [String] :price *required* - Decimal formatted price, without currency sign
42
+ # @option item [String] :description optional
43
+ def add_item(item = {})
44
+ requires!(item, :item_number, :quantity, :price)
45
+ @items << item
46
+ end
47
+
48
+ def submit!
49
+ raise BillHicks::InvalidOrder.new("Must call #add_header before submitting") if @header.nil?
50
+ raise BillHicks::InvalidOrder.new("Must add items with #add_item before submitting") if @items.empty?
51
+
52
+ @order_filename = "#{@header[:purchase_order]}-order.txt"
53
+ @order_file = Tempfile.new(@order_filename)
54
+ begin
55
+ CSV.open(@order_file.path, 'w+') do |csv|
56
+ csv << header_names
57
+ csv << header_fields
58
+ csv << items_header
59
+ @items.each do |item|
60
+ csv << item_fields(item)
61
+ end
62
+ end
63
+
64
+ upload!
65
+ ensure
66
+ # Close and delete (unlink) file.
67
+ @order_file.close
68
+ @order_file.unlink
69
+ end
70
+
71
+ # TODO: Find some way of returning a meaningful true/false. Currently, if there's a problem, an exception is raised.
72
+ true
73
+ end
74
+
75
+ private
76
+
77
+ def header_names
78
+ ['HL', 'Customer#', 'Ship to Name1', 'Address 1', 'Address 2', 'city', 'state', 'zip', 'cust po', 'ship method', 'notes', 'FFL#']
79
+ end
80
+
81
+ def header_fields
82
+ [
83
+ 'H',
84
+ @header[:customer],
85
+ @header[:shipping][:name],
86
+ @header[:shipping][:address_1],
87
+ @header[:shipping][:address_2],
88
+ @header[:shipping][:city],
89
+ @header[:shipping][:state],
90
+ @header[:shipping][:zip],
91
+ @header[:purchase_order],
92
+ @header[:shipping_method],
93
+ @header[:notes],
94
+ @header[:ffl],
95
+ ]
96
+ end
97
+
98
+ def items_header
99
+ ['LL', 'Item', 'Description', 'Qty', 'Price']
100
+ end
101
+
102
+ def item_fields(item)
103
+ [
104
+ 'L',
105
+ item[:item_number],
106
+ item[:description],
107
+ item[:quantity],
108
+ item[:price],
109
+ ]
110
+ end
111
+
112
+ def upload!
113
+ connect(@options) do |ftp|
114
+ ftp.chdir(ORDER_UPLOAD_DIR)
115
+ ftp.puttextfile(@order_file.path, @order_filename)
116
+ end
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ module BillHicks
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bill_hicks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dale Campbell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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
35
+ prerelease: false
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:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: ''
56
+ email:
57
+ - oshuma@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bill_hicks.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/bill_hicks.rb
75
+ - lib/bill_hicks/base.rb
76
+ - lib/bill_hicks/catalog.rb
77
+ - lib/bill_hicks/category.rb
78
+ - lib/bill_hicks/inventory.rb
79
+ - lib/bill_hicks/order.rb
80
+ - lib/bill_hicks/version.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Ruby library for Bill Hicks ERP system
105
+ test_files: []