gun_accessory_supply 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +21 -0
- data/README.md +149 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +6 -0
- data/gun_accessory_supply.gemspec +30 -0
- data/lib/gun_accessory_supply/base.rb +90 -0
- data/lib/gun_accessory_supply/catalog.rb +90 -0
- data/lib/gun_accessory_supply/category.rb +42 -0
- data/lib/gun_accessory_supply/inventory.rb +73 -0
- data/lib/gun_accessory_supply/order.rb +147 -0
- data/lib/gun_accessory_supply/tracking.rb +40 -0
- data/lib/gun_accessory_supply/user.rb +17 -0
- data/lib/gun_accessory_supply/version.rb +3 -0
- data/lib/gun_accessory_supply.rb +50 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 713a82aca54c3918e02de8c1653811b9ad48312bcc8b85ecf541df271e56d9e3
|
4
|
+
data.tar.gz: 403f668797fb4bf16a6a96c11d6b64b102e55316ed03f47db0895882b5cc07e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 538ded1697f684265fe166db18599972a4ccc0c3d8b5fb09938e05e851bd2cfc7d53954e555763ba9c1543fd3bc1ff5d4a88e3f3339f12e71233e9f72e6fe233
|
7
|
+
data.tar.gz: 06573264b87a5ab1cfa66acbed323b3d2e8a9028352c693e760a2e06e6f4dce2960aeacb9a34627098514fcb658cd52d22fea1934b4ef67eeda5e88b10683a57
|
data/.DS_Store
ADDED
Binary file
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gun_accessory_supply (0.0.1)
|
5
|
+
activesupport (>= 5, < 7)
|
6
|
+
net-sftp
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.2.6)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
concurrent-ruby (1.1.9)
|
17
|
+
diff-lcs (1.4.4)
|
18
|
+
i18n (1.8.10)
|
19
|
+
concurrent-ruby (~> 1.0)
|
20
|
+
minitest (5.14.4)
|
21
|
+
net-sftp (3.0.0)
|
22
|
+
net-ssh (>= 5.0.0, < 7.0.0)
|
23
|
+
net-ssh (6.1.0)
|
24
|
+
rake (12.3.3)
|
25
|
+
rspec (3.10.0)
|
26
|
+
rspec-core (~> 3.10.0)
|
27
|
+
rspec-expectations (~> 3.10.0)
|
28
|
+
rspec-mocks (~> 3.10.0)
|
29
|
+
rspec-core (3.10.1)
|
30
|
+
rspec-support (~> 3.10.0)
|
31
|
+
rspec-expectations (3.10.1)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.10.0)
|
34
|
+
rspec-mocks (3.10.2)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.10.0)
|
37
|
+
rspec-support (3.10.2)
|
38
|
+
thread_safe (0.3.6)
|
39
|
+
tzinfo (1.2.9)
|
40
|
+
thread_safe (~> 0.1)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (>= 1.15)
|
47
|
+
gun_accessory_supply!
|
48
|
+
rake (~> 12.3.0)
|
49
|
+
rspec (~> 3.7)
|
50
|
+
|
51
|
+
BUNDLED WITH
|
52
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 AmmoReady
|
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,149 @@
|
|
1
|
+
# Gun Accessory Supply
|
2
|
+
|
3
|
+
Ruby library for Gun Accessory Supply.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gun_accessory_supply'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gun_accessory_supply
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
#TODO: Update info below
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
**Note:** Nearly all methods require `:username` and `:password` keys in the options hash.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
options = {
|
33
|
+
username: 'dealer@example.com',
|
34
|
+
password: 'sekret-passwd'
|
35
|
+
}
|
36
|
+
```
|
37
|
+
|
38
|
+
### GunAccessorySupply::Catalog
|
39
|
+
|
40
|
+
To get all items in the catalog:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
catalog = []
|
44
|
+
GunAccessorySupply::Catalog.new(options).all do |i|
|
45
|
+
catalog << i
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
See `GunAccessorySupply::Catalog` for the response structure.
|
50
|
+
|
51
|
+
### GunAccessorySupply::Inventory
|
52
|
+
|
53
|
+
To get your inventory details (availability, price, etc.):
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
inventory = []
|
57
|
+
GunAccessorySupply::Inventory.new(options).all do |i|
|
58
|
+
inventory << i
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
See `GunAccessorySupply::Inventory` for the response structure.
|
63
|
+
|
64
|
+
### GunAccessorySupply::Category
|
65
|
+
|
66
|
+
Returns an array of category codes and descriptions.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
categories = GunAccessorySupply::Category.all(options)
|
70
|
+
|
71
|
+
# [
|
72
|
+
# {:code=>"H648", :description=>"AIRGUNS"},
|
73
|
+
# {:code=>"H610", :description=>"AMMUNITION"},
|
74
|
+
# ...,
|
75
|
+
# ]
|
76
|
+
```
|
77
|
+
|
78
|
+
### GunAccessorySupply::Order
|
79
|
+
|
80
|
+
To build and submit an order, the basic steps are: 1) instantiate an Order object, 2) add header
|
81
|
+
information, 3) add item information (multiple items if needed), 4) submit the order.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# Instantiate the Order instance, passing in your :username and :password
|
85
|
+
order = GunAccessorySupply::Order.new(options)
|
86
|
+
|
87
|
+
# Add header information:
|
88
|
+
header_opts = {
|
89
|
+
customer: '...', # customer number
|
90
|
+
purchase_order: '...', # application specific purchase order
|
91
|
+
ffl: '...', # your FFL number
|
92
|
+
shipping: { # shipping information (all fields except :address_2 are required)
|
93
|
+
name: '...',
|
94
|
+
address_1: '...',
|
95
|
+
address_2: '...',
|
96
|
+
city: '...',
|
97
|
+
state: '...',
|
98
|
+
zip: '...',
|
99
|
+
},
|
100
|
+
|
101
|
+
# Optional fields:
|
102
|
+
shipping_method: '...',
|
103
|
+
notes: '...',
|
104
|
+
}
|
105
|
+
order.add_header(header_opts)
|
106
|
+
|
107
|
+
# Add item information:
|
108
|
+
item_opts = {
|
109
|
+
item_number: '...', # GunAccessorySupply item number
|
110
|
+
description: '...',
|
111
|
+
quantity: 1,
|
112
|
+
price: '123.45', # Decimal formatted price, without currency sign
|
113
|
+
}
|
114
|
+
order.add_item(item_opts) # Multiple items may be added, just call #add_item for each one.
|
115
|
+
|
116
|
+
# Submit the order (returns true on success, raises an exception on failure):
|
117
|
+
order.submit!
|
118
|
+
```
|
119
|
+
|
120
|
+
See `GunAccessorySupply::Order` for details on required options.
|
121
|
+
|
122
|
+
### GunAccessorySupply::Tracking
|
123
|
+
|
124
|
+
For fetching tracking data for individual order fulfillments.
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
tracking_details = GunAccessorySupply::Tracking.fetch_data(options)
|
128
|
+
|
129
|
+
# [
|
130
|
+
# {:po_number=>"12345", :carrier=>"UPS", :tracking_numbers=>['123456789']},
|
131
|
+
# {:po_number=>"54321", :carrier=>"UPS", :tracking_numbers=>['987654321']},
|
132
|
+
# ...,
|
133
|
+
# ]
|
134
|
+
```
|
135
|
+
|
136
|
+
## Development
|
137
|
+
|
138
|
+
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.
|
139
|
+
|
140
|
+
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).
|
141
|
+
|
142
|
+
## Contributing
|
143
|
+
|
144
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ammoready/gun_accessory_supply.
|
145
|
+
|
146
|
+
|
147
|
+
## License
|
148
|
+
|
149
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gun_accessory_supply"
|
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,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gun_accessory_supply/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gun_accessory_supply"
|
8
|
+
spec.version = GunAccessorySupply::VERSION
|
9
|
+
spec.authors = ["Tony Beninate"]
|
10
|
+
spec.email = ["tonybeninate@icloud.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby library for Gun Accessory Supply}
|
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_runtime_dependency "activesupport", ">= 5", "< 7"
|
25
|
+
spec.add_runtime_dependency "net-sftp"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", ">= 1.15"
|
28
|
+
spec.add_development_dependency "rake", "~> 12.3.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.7"
|
30
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module GunAccessorySupply
|
2
|
+
class Base
|
3
|
+
|
4
|
+
def self.connect(options = {})
|
5
|
+
requires!(options, :username, :password)
|
6
|
+
|
7
|
+
Net::SFTP.start(GunAccessorySupply.config.ftp_host, options[:username], password: options[:password], port: GunAccessorySupply.config.ftp_port) do |sftp|
|
8
|
+
yield(sftp)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
# Wrapper to `self.requires!` that can be used as an instance method.
|
15
|
+
def requires!(*args)
|
16
|
+
self.class.requires!(*args)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.requires!(hash, *params)
|
20
|
+
params.each do |param|
|
21
|
+
if param.is_a?(Array)
|
22
|
+
raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash.has_key?(param.first)
|
23
|
+
|
24
|
+
valid_options = param[1..-1]
|
25
|
+
raise ArgumentError.new("Parameter: #{param.first} must be one of: #{valid_options.join(', ')}") unless valid_options.include?(hash[param.first])
|
26
|
+
else
|
27
|
+
raise ArgumentError.new("Missing required parameter: #{param}") unless hash.has_key?(param)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Instance methods become class methods through inheritance
|
33
|
+
def connect(options)
|
34
|
+
self.class.connect(options) do |sftp|
|
35
|
+
begin
|
36
|
+
yield(sftp)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def content_for(xml_doc, field)
|
42
|
+
node = xml_doc.css(field).first
|
43
|
+
|
44
|
+
if node.nil?
|
45
|
+
nil
|
46
|
+
else
|
47
|
+
node.content.try(:strip)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_file(filename, file_directory=nil)
|
52
|
+
connect(@options) do |sftp|
|
53
|
+
begin
|
54
|
+
tempfile = Tempfile.new
|
55
|
+
|
56
|
+
sftp.download!(File.join(file_directory, filename), tempfile.path)
|
57
|
+
|
58
|
+
return tempfile
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_most_recent_file(file_prefix, file_directory=nil)
|
64
|
+
filenames = []
|
65
|
+
|
66
|
+
connect(@options) do |sftp|
|
67
|
+
sftp.dir.foreach(file_directory) { |entry| filenames << entry.name }
|
68
|
+
filename = filenames.select{ |n| n.include?(file_prefix) }.sort.last
|
69
|
+
|
70
|
+
tempfile = self.get_file(filename, file_directory)
|
71
|
+
return tempfile
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def parse_row(row)
|
76
|
+
row.gsub('"','').gsub("\r\n", '').encode('UTF-8', invalid: :replace).split(",")
|
77
|
+
end
|
78
|
+
|
79
|
+
def write_file(path, data)
|
80
|
+
connect(@options) do |sftp|
|
81
|
+
begin
|
82
|
+
sftp.file.open(path, "w") do |f|
|
83
|
+
f.puts data
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module GunAccessorySupply
|
2
|
+
class Catalog < Base
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
requires!(options, :username, :password)
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.all(options = {})
|
10
|
+
requires!(options, :username, :password)
|
11
|
+
new(options).all
|
12
|
+
end
|
13
|
+
|
14
|
+
def all
|
15
|
+
tempfile = get_most_recent_file(GunAccessorySupply.config.catalog_filename_prefix, GunAccessorySupply.config.top_level_dir)
|
16
|
+
items = []
|
17
|
+
|
18
|
+
File.open(tempfile).each_with_index do |row, i|
|
19
|
+
row = parse_row(row)
|
20
|
+
|
21
|
+
if i==0
|
22
|
+
@headers = row
|
23
|
+
next
|
24
|
+
end
|
25
|
+
|
26
|
+
category = row[@headers.index('Sub-Category')].try(:strip)
|
27
|
+
|
28
|
+
item = {
|
29
|
+
mfg_number: row[@headers.index('Item ID')].try(:strip),
|
30
|
+
upc: row[@headers.index('UPC')].try(:strip),
|
31
|
+
name: row[@headers.index('Item Description')].try(:strip),
|
32
|
+
quantity: row[@headers.index("Available Qty")].to_i,
|
33
|
+
price: row[@headers.index('MSRP')].try(:strip),
|
34
|
+
msrp: row[@headers.index('MSRP')].try(:strip),
|
35
|
+
map_price: row[@headers.index('MAP Price')].try(:strip),
|
36
|
+
brand: row[@headers.index('Manufacturer')].try(:strip),
|
37
|
+
item_identifier: row[@headers.index("Item ID")].try(:strip),
|
38
|
+
category: cleaned_category(category)[0],
|
39
|
+
subcategory: cleaned_category(category)[1],
|
40
|
+
weight: row[@headers.index('Shipping Weight')].try(:strip),
|
41
|
+
features: {
|
42
|
+
caliber: row[@headers.index('Caliber')].try(:strip),
|
43
|
+
image_name: row[@headers.index("Image Link")].try(:strip),
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
items << item
|
48
|
+
end
|
49
|
+
|
50
|
+
tempfile.close
|
51
|
+
tempfile.unlink
|
52
|
+
|
53
|
+
items
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
# Returns an array of ["Category", "Subcategory"]
|
59
|
+
def cleaned_category(category_string)
|
60
|
+
# ex. category_string = "Gun Care/Security/Safes"
|
61
|
+
categories = category_string.split('/')
|
62
|
+
|
63
|
+
category = case categories.first
|
64
|
+
when 'Gun Care'
|
65
|
+
if category_string.include?('Gun Care/Security')
|
66
|
+
["Accessories", "Security"]
|
67
|
+
else
|
68
|
+
["Accessories", "Cleaning"]
|
69
|
+
end
|
70
|
+
when 'Hunting'
|
71
|
+
if categories[1] == 'Knives'
|
72
|
+
categories[2]
|
73
|
+
else
|
74
|
+
["Hunting", "Accessories"]
|
75
|
+
end
|
76
|
+
when 'Miscellaneous'
|
77
|
+
["Accessories", "Miscellaneous"]
|
78
|
+
when 'Outdoor'
|
79
|
+
categories.last(2)
|
80
|
+
when 'Parts'
|
81
|
+
["Accessories", "Gun Parts"]
|
82
|
+
when 'Shooting'
|
83
|
+
categories.last(2)
|
84
|
+
end
|
85
|
+
|
86
|
+
category.present? ? category : categories.first(2)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module GunAccessorySupply
|
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 = {}, &block)
|
16
|
+
requires!(options, :username, :password)
|
17
|
+
new(options).all(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns an array of hashes with category details.
|
21
|
+
def all(&block)
|
22
|
+
categories = []
|
23
|
+
|
24
|
+
# Categories are listed in catalog xml, so fetch that.
|
25
|
+
catalog = Catalog.new(@options)
|
26
|
+
catalog.all do |item|
|
27
|
+
categories << {
|
28
|
+
code: item[:subcategory].gsub("&", "").gsub(/[^A-Za-z0-9]/, "_").squeeze("_").downcase,
|
29
|
+
description: item[:subcategory].gsub("&", "&")
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
categories.uniq! { |c| c[:code] }
|
34
|
+
categories.sort_by! { |c| c[:code] }
|
35
|
+
|
36
|
+
categories.each do |category|
|
37
|
+
yield category
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module GunAccessorySupply
|
2
|
+
class Inventory < Base
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
requires!(options, :username, :password)
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.all(options = {})
|
10
|
+
requires!(options, :username, :password)
|
11
|
+
new(options).all
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.quantity(options = {})
|
15
|
+
requires!(options, :username, :password)
|
16
|
+
new(options).quantity
|
17
|
+
end
|
18
|
+
|
19
|
+
def all
|
20
|
+
tempfile = get_most_recent_file(GunAccessorySupply.config.catalog_filename_prefix, GunAccessorySupply.config.top_level_dir)
|
21
|
+
items = []
|
22
|
+
|
23
|
+
File.open(tempfile).each_with_index do |row, i|
|
24
|
+
row = parse_row(row)
|
25
|
+
|
26
|
+
if i==0
|
27
|
+
@headers = row
|
28
|
+
next
|
29
|
+
end
|
30
|
+
|
31
|
+
item = {
|
32
|
+
item_identifier: row[@headers.index('Item ID')].try(:strip),
|
33
|
+
quantity: row[@headers.index('Available Qty')].to_i,
|
34
|
+
price: row[@headers.index('MSRP')].try(:strip),
|
35
|
+
}
|
36
|
+
|
37
|
+
items << item
|
38
|
+
end
|
39
|
+
|
40
|
+
tempfile.close
|
41
|
+
tempfile.unlink
|
42
|
+
|
43
|
+
items
|
44
|
+
end
|
45
|
+
|
46
|
+
def quantity
|
47
|
+
tempfile = get_most_recent_file(GunAccessorySupply.config.catalog_filename_prefix, GunAccessorySupply.config.top_level_dir)
|
48
|
+
items = []
|
49
|
+
|
50
|
+
File.open(tempfile).each_with_index do |row, i|
|
51
|
+
row = parse_row(row)
|
52
|
+
|
53
|
+
if i==0
|
54
|
+
@headers = row
|
55
|
+
next
|
56
|
+
end
|
57
|
+
|
58
|
+
item = {
|
59
|
+
item_identifier: row[@headers.index('Item ID')].try(:strip),
|
60
|
+
quantity: row[@headers.index('Available Qty')].to_i,
|
61
|
+
}
|
62
|
+
|
63
|
+
items << item
|
64
|
+
end
|
65
|
+
|
66
|
+
tempfile.close
|
67
|
+
tempfile.unlink
|
68
|
+
|
69
|
+
items
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
module GunAccessorySupply
|
2
|
+
# To submit an order:
|
3
|
+
#
|
4
|
+
# * Instantiate a new Order, passing in `:username`
|
5
|
+
# * Call {#add_recipient}
|
6
|
+
# * Call {#add_item} for each item on the order
|
7
|
+
#
|
8
|
+
# See each method for a list of required options.
|
9
|
+
class Order < Base
|
10
|
+
|
11
|
+
# @option options [String] :username *required*
|
12
|
+
def initialize(options = {})
|
13
|
+
requires!(options, :username, :password, :po_number, :ship_to_id)
|
14
|
+
|
15
|
+
@po_number = options[:po_number]
|
16
|
+
@ship_to_id = options[:ship_to_id]
|
17
|
+
@items = []
|
18
|
+
@options = options
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param header [Hash]
|
22
|
+
# * :dealer_name [String] *required*
|
23
|
+
# * :ffl [String]
|
24
|
+
# * :shipping [Hash] *required*
|
25
|
+
# * :name [String] *required*
|
26
|
+
# * :address [String] *required*
|
27
|
+
# * :city [String] *required*
|
28
|
+
# * :state [String] *required*
|
29
|
+
# * :zip [String] *required*
|
30
|
+
# * :email [String] *required*
|
31
|
+
# * :phone [String] *required*
|
32
|
+
# * :special_instructions [String] optional
|
33
|
+
def add_recipient(hash = {})
|
34
|
+
requires!(hash, :dealer_name, :shipping)
|
35
|
+
requires!(hash[:shipping], :name, :address, :city, :state, :zip, :email, :phone)
|
36
|
+
|
37
|
+
@recipient = hash
|
38
|
+
end
|
39
|
+
|
40
|
+
# @param item [Hash]
|
41
|
+
# * :identifier [String] *required*
|
42
|
+
# * :description [String]
|
43
|
+
# * :upc [String] *required*
|
44
|
+
# * :qty [Integer] *required*
|
45
|
+
# * :price [String]
|
46
|
+
def add_item(item = {})
|
47
|
+
requires!(item, :identifier, :upc, :qty)
|
48
|
+
@items << item
|
49
|
+
end
|
50
|
+
|
51
|
+
def filename
|
52
|
+
return @filename if defined?(@filename)
|
53
|
+
timestamp = Time.now.strftime('%Y%m%d%T').gsub(':', '')
|
54
|
+
@filename = "GUN-ACCESSORY-SUPPLY-#{@po_number}-#{timestamp}.xml"
|
55
|
+
end
|
56
|
+
|
57
|
+
def submit!
|
58
|
+
write_file("/in/#{filename}", self.to_xml)
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_xml
|
62
|
+
output = ""
|
63
|
+
|
64
|
+
xml = Builder::XmlMarkup.new(target: output, indent: 2)
|
65
|
+
|
66
|
+
xml.instruct!(:xml)
|
67
|
+
|
68
|
+
xml.cXML(timestamp: Time.now) do
|
69
|
+
xml.Header do
|
70
|
+
xml.From do
|
71
|
+
xml.Credential(domain: @xml_domain) do
|
72
|
+
xml.Identity @xml_domain
|
73
|
+
xml.SharedSecret @xml_secret
|
74
|
+
xml.OrderID @po_number
|
75
|
+
end
|
76
|
+
end
|
77
|
+
xml.To do
|
78
|
+
xml.Credential(domain: @xml_domain) do
|
79
|
+
xml.Identity @xml_domain
|
80
|
+
xml.SharedSecret @xml_secret
|
81
|
+
xml.OrderID @po_number
|
82
|
+
end
|
83
|
+
end
|
84
|
+
xml.Sender do
|
85
|
+
xml.Credential(domain: @xml_domain) do
|
86
|
+
xml.Identity @xml_domain
|
87
|
+
xml.SharedSecret @xml_secret
|
88
|
+
xml.OrderID @po_number
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
xml.Request do
|
94
|
+
xml.OrderRequest do
|
95
|
+
xml.OrderRequestHeader(orderDate: Time.now, type: 'new') do
|
96
|
+
xml.ShipTo do
|
97
|
+
xml.Address do
|
98
|
+
xml.Name @recipient[:dealer_name]
|
99
|
+
xml.Email @recipient[:shipping][:email]
|
100
|
+
xml.PostalAddress do
|
101
|
+
xml.DeliverTo @recipient[:shipping][:name]
|
102
|
+
xml.Street @recipient[:shipping][:address]
|
103
|
+
xml.City @recipient[:shipping][:city]
|
104
|
+
xml.State @recipient[:shipping][:state]
|
105
|
+
xml.PostalCode @recipient[:shipping][:zip]
|
106
|
+
xml.Country "US"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
xml.BillTo do
|
111
|
+
xml.Address(addressID: @ship_to_id) do
|
112
|
+
xml.Name @recipient[:dealer_name]
|
113
|
+
xml.Email @recipient[:shipping][:email]
|
114
|
+
xml.PostalAddress do
|
115
|
+
xml.DeliverTo @recipient[:shipping][:name]
|
116
|
+
xml.Street @recipient[:shipping][:address]
|
117
|
+
xml.City @recipient[:shipping][:city]
|
118
|
+
xml.State @recipient[:shipping][:state]
|
119
|
+
xml.PostalCode @recipient[:shipping][:zip]
|
120
|
+
xml.Country "US"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
@items.each do |item|
|
127
|
+
xml.ItemOut(quantity: item[:qty]) do
|
128
|
+
xml.ItemID do
|
129
|
+
xml.SupplierPartID item[:identifier]
|
130
|
+
end
|
131
|
+
xml.ItemDetail do
|
132
|
+
xml.UnitPrice do
|
133
|
+
xml.Money item[:price]
|
134
|
+
end
|
135
|
+
xml.Description item[:upc]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
output
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module GunAccessorySupply
|
2
|
+
class Tracking < Base
|
3
|
+
|
4
|
+
BASE_TRACKING_FILE_DIRECTORY = "%s-TRACK".freeze
|
5
|
+
BASE_TRACKING_FILE_NAME = "%s_PackageTracking.csv".freeze
|
6
|
+
CARRIER = 'UPS'.freeze
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
requires!(options, :username, :password, :dealer_number)
|
10
|
+
|
11
|
+
@options = options
|
12
|
+
@dealer_number = options[:dealer_number]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.fetch_data(options = {})
|
16
|
+
requires!(options, :username, :password, :dealer_number)
|
17
|
+
|
18
|
+
new(options).fetch_data
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetch_data
|
22
|
+
tracking_file_name = BASE_TRACKING_FILE_NAME % @dealer_number
|
23
|
+
tracking_file_directory = BASE_TRACKING_FILE_DIRECTORY % @dealer_number
|
24
|
+
tracking_file = get_file(tracking_file_name, tracking_file_directory)
|
25
|
+
|
26
|
+
tracking_details = CSV.foreach(tracking_file, { headers: :first_row }).map do |row|
|
27
|
+
{
|
28
|
+
po_number: row['USR_VEND_ORD_NO'],
|
29
|
+
carrier: CARRIER,
|
30
|
+
tracking_numbers: [row['TRK_NO_1'], row['TRK_NO_2'], row['TRK_NO_3'], row['TRK_NO_4'], row['TRK_NO_5']].reject(&:blank?),
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
tracking_file.unlink
|
35
|
+
|
36
|
+
tracking_details
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module GunAccessorySupply
|
2
|
+
class User < Base
|
3
|
+
|
4
|
+
STANDARD_CUSTOMER_NUMBER_LENGTH = 6.freeze
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
requires!(options, :username, :password)
|
8
|
+
|
9
|
+
@customer_number = options[:username]
|
10
|
+
end
|
11
|
+
|
12
|
+
def authenticated?
|
13
|
+
@customer_number.length == STANDARD_CUSTOMER_NUMBER_LENGTH && @customer_number[0].to_i == 1
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'gun_accessory_supply/version'
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require 'net/sftp'
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
require 'active_support/all'
|
8
|
+
|
9
|
+
require 'gun_accessory_supply/base'
|
10
|
+
require 'gun_accessory_supply/catalog'
|
11
|
+
require 'gun_accessory_supply/category'
|
12
|
+
require 'gun_accessory_supply/inventory'
|
13
|
+
require 'gun_accessory_supply/order'
|
14
|
+
require 'gun_accessory_supply/tracking'
|
15
|
+
require 'gun_accessory_supply/user'
|
16
|
+
|
17
|
+
module GunAccessorySupply
|
18
|
+
class InvalidOrder < StandardError; end
|
19
|
+
class NotAuthenticated < StandardError; end
|
20
|
+
class FileOrDirectoryNotFound < StandardError; end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
attr_accessor :config
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.config
|
27
|
+
@config ||= Configuration.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.configure
|
31
|
+
yield(config)
|
32
|
+
end
|
33
|
+
|
34
|
+
class Configuration
|
35
|
+
attr_accessor :debug_mode
|
36
|
+
attr_accessor :ftp_host
|
37
|
+
attr_accessor :ftp_port
|
38
|
+
attr_accessor :top_level_dir
|
39
|
+
attr_accessor :catalog_filename_prefix
|
40
|
+
attr_accessor :xml_domain
|
41
|
+
attr_accessor :xml_secret
|
42
|
+
|
43
|
+
def initialize
|
44
|
+
@debug_mode ||= false
|
45
|
+
@ftp_host ||= "50.233.131.250"
|
46
|
+
@ftp_port ||= "2222"
|
47
|
+
@top_level_dir ||= "/out"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gun_accessory_supply
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Beninate
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: net-sftp
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.15'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.15'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 12.3.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 12.3.0
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.7'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.7'
|
89
|
+
description: ''
|
90
|
+
email:
|
91
|
+
- tonybeninate@icloud.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- ".DS_Store"
|
97
|
+
- Gemfile
|
98
|
+
- Gemfile.lock
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- bin/console
|
103
|
+
- bin/setup
|
104
|
+
- gun_accessory_supply.gemspec
|
105
|
+
- lib/gun_accessory_supply.rb
|
106
|
+
- lib/gun_accessory_supply/base.rb
|
107
|
+
- lib/gun_accessory_supply/catalog.rb
|
108
|
+
- lib/gun_accessory_supply/category.rb
|
109
|
+
- lib/gun_accessory_supply/inventory.rb
|
110
|
+
- lib/gun_accessory_supply/order.rb
|
111
|
+
- lib/gun_accessory_supply/tracking.rb
|
112
|
+
- lib/gun_accessory_supply/user.rb
|
113
|
+
- lib/gun_accessory_supply/version.rb
|
114
|
+
homepage: ''
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubygems_version: 3.0.9
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Ruby library for Gun Accessory Supply
|
137
|
+
test_files: []
|