davidsons 1.0.0

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: 74e0b821b223121ce9a0307d67fbad936233fe05
4
+ data.tar.gz: 2e5d000c3ab2104035e8ebc84a9b4341eac84bd3
5
+ SHA512:
6
+ metadata.gz: 3264a03c3d82a2f9f8409c1e4b42a02499cc5fdd2665c2fd5176f2612ac8379aed5236a093e2c221f3e9089a29aaf5fe61059927d061623eca38cf8659c4af90
7
+ data.tar.gz: 5a8cb6c1f19bd0ff32a906147741304a05e4d8dac543afda0ffff4d86422819c2159bc7eff297fae81ad205b91194b3a4288d0ad1b27b4679ef580389d3537df
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
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.15.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in davidsons.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 David Knight
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,39 @@
1
+ # Davidsons
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/davidsons`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'davidsons'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install davidsons
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/davidsons.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "davidsons"
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(__FILE__)
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/davidsons.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "davidsons/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "davidsons"
8
+ spec.version = Davidsons::VERSION
9
+ spec.authors = ["David Knight"]
10
+ spec.email = ["viacoffee@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby library for Davidson's}
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_dependency "savon", "~> 2.11.1"
25
+
26
+ spec.add_runtime_dependency "smarter_csv", "~> 1.1.4"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.14"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "activesupport", "~> 5"
32
+ end
@@ -0,0 +1,60 @@
1
+ module Davidsons
2
+ class Base
3
+
4
+ def self.connect(options = {})
5
+ requires!(options, :username, :password)
6
+
7
+ Net::FTP.open(Davidsons.config.ftp_host, options[:username], options[:password]) do |ftp|
8
+ ftp.passive = true
9
+
10
+ yield ftp
11
+ end
12
+ rescue Net::FTPPermError
13
+ raise Davidsons::NotAuthenticated
14
+ end
15
+
16
+ protected
17
+
18
+ # Wrapper to `self.requires!` that can be used as an instance method.
19
+ def requires!(*args)
20
+ self.class.requires!(*args)
21
+ end
22
+
23
+ def self.requires!(hash, *params)
24
+ params.each do |param|
25
+ if param.is_a?(Array)
26
+ raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash.has_key?(param.first)
27
+
28
+ valid_options = param[1..-1]
29
+ raise ArgumentError.new("Parameter: #{param.first} must be one of: #{valid_options.join(', ')}") unless valid_options.include?(hash[param.first])
30
+ else
31
+ raise ArgumentError.new("Missing required parameter: #{param}") unless hash.has_key?(param)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Instance methods become class methods through inheritance
37
+ def connect(options)
38
+ self.class.connect(options) do |ftp|
39
+ yield ftp
40
+ end
41
+ end
42
+
43
+ def get_file(filename)
44
+ connect(@options) do |ftp|
45
+ begin
46
+ tempfile = Tempfile.new
47
+
48
+ ftp.getbinaryfile(filename, tempfile.path)
49
+
50
+ tempfile.close
51
+
52
+ tempfile
53
+ ensure
54
+ ftp.close
55
+ end
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,67 @@
1
+ module Davidsons
2
+ class Catalog < Base
3
+
4
+ CATALOG_FILENAME = "davidsons_inventory.csv".freeze
5
+ CHUNK_SIZE = 100
6
+
7
+ def initialize(options = {})
8
+ requires!(options, :username, :password)
9
+
10
+ @options = options
11
+ end
12
+
13
+ def self.all(options = {}, &block)
14
+ requires!(options, :username, :password)
15
+ new(options).all(&block)
16
+ end
17
+
18
+ def all(&block)
19
+ tempfile = get_file(CATALOG_FILENAME)
20
+
21
+ SmarterCSV.process(tempfile.open, {
22
+ chunk_size: CHUNK_SIZE,
23
+ convert_values_to_numeric: false,
24
+ force_utf8: true,
25
+ key_mapping: {
26
+ "item_#": :item_identifier,
27
+ item_description: :name,
28
+ upc_code: :upc,
29
+ manufacturer: :brand,
30
+ msp: :msrp,
31
+ dealer_price: :price,
32
+ gun_type: :category,
33
+ }
34
+ }) do |chunk|
35
+ chunk.each do |item|
36
+ item.except!(:retail_price, :capacity, :sale_price, :sale_ends)
37
+
38
+ item[:mfg_number] = item[:item_identifier]
39
+ item[:msrp].tr!('$', '')
40
+ item[:price].tr!('$', '')
41
+ item[:upc].tr!('#', '')
42
+ item[:quantity] = item[:quantity].to_i
43
+
44
+ item[:features] = map_features(item)
45
+
46
+ item.except!(:finish, :barrel_length, :overall_length, :model_series, :sights, :stock)
47
+
48
+ yield(item)
49
+ end
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ def map_features(item)
56
+ {
57
+ finish: item[:finish],
58
+ barrel_length: item[:barrel_length],
59
+ overall_length: item[:overall_length],
60
+ model_series: item[:model_series],
61
+ sights: item[:sights],
62
+ stock: item[:stock]
63
+ }
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,57 @@
1
+ module Davidsons
2
+ class Inventory < Base
3
+
4
+ INVENTORY_FILENAME = "davidsons_quantity.csv"
5
+ CHUNK_SIZE = 100
6
+
7
+ def initialize(options = {})
8
+ requires!(options, :username, :password)
9
+
10
+ @options = options
11
+ end
12
+
13
+ def self.all(options = {}, &block)
14
+ requires!(options, :username, :password)
15
+ new(options).quantity(&block)
16
+ end
17
+
18
+ def self.quantity(options = {}, &block)
19
+ requires!(options, :username, :password)
20
+ new(options).quantity(&block)
21
+ end
22
+
23
+ def quantity(&block)
24
+ tempfile = get_file(INVENTORY_FILENAME)
25
+
26
+ SmarterCSV.process(tempfile.open, {
27
+ chunk_size: CHUNK_SIZE,
28
+ convert_values_to_numeric: false,
29
+ force_utf8: true,
30
+ key_mapping: {
31
+ item_number: :item_identifier
32
+ }
33
+ }) do |chunk|
34
+ chunk.each do |item|
35
+ item.except!(:upc_code)
36
+
37
+ item[:quantity] = item[:quantity_nc].to_i + item[:quantity_az].to_i
38
+ item[:locations] = map_location_quantities(item)
39
+
40
+ item.except!(:quantity_nc, :quantity_az)
41
+
42
+ yield(item)
43
+ end
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def map_location_quantities(item)
50
+ {
51
+ nc: item[:quantity_nc],
52
+ az: item[:quantity_az]
53
+ }
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,88 @@
1
+ require 'savon'
2
+ require 'pp'
3
+
4
+ module Davidsons
5
+ class Order < Base
6
+
7
+ TEST_URL = 'https://dealernetwork.davidsonsinc.com/testapi/orderservice.asmx?WSDL'
8
+ API_URL = 'https://dealernetwork.davidsonsinc.com/api/orderservice.asmx?WSDL'
9
+
10
+ attr :client
11
+
12
+ def initialize(options = {})
13
+ requires!(options, :token, :dealer_number)
14
+
15
+ @options = options
16
+
17
+ settings = Hash.new
18
+ settings[:wsdl] = @options[:test] ? TEST_URL : API_URL
19
+ settings[:env_namespace] = 'soap'
20
+ settings[:proxy] = Davidsons.config.proxy_url
21
+
22
+ if @options[:test]
23
+ settings[:log] = true
24
+ settings[:log_level] = :debug
25
+ settings[:pretty_print_xml] = true
26
+ end
27
+
28
+ @client = Savon.client(settings)
29
+ end
30
+
31
+ def create_order(items, partner_name, partner_phone, purchase_order_number, details = {})
32
+ request = @client.call(:create_order, attributes: { xmlns: Davidsons.config.xmlns }, message: { ':request': {
33
+ 'Token' => @options[:token],
34
+ 'DealerCustomerNumber' => @options[:dealer_number],
35
+ 'ConsumerName' => partner_name,
36
+ 'ConsumerPhone' => partner_phone,
37
+ 'ReferenceNumber' => purchase_order_number,
38
+ 'Items' => self.format_items(items),
39
+ 'UseExpeditedShipping' => 'false'
40
+ }})
41
+
42
+ order_info = request.body[:create_order_response][:create_order_result]
43
+
44
+ if order_info[:response_code] == "0"
45
+ { success: true, order_number: order_info[:order_number] }
46
+ else
47
+ { success: false, error_code: order_info[:response_code], error_message: order_info[:response_text] }
48
+ end
49
+ end
50
+
51
+ def get_order_tracking(order_number)
52
+ request = @client.call(:get_order_tracking, attributes: { xmlns: Davidsons.config.xmlns }, message: { ':request': {
53
+ 'Token' => @options[:token],
54
+ 'OrderNo' => order_number
55
+ }})
56
+
57
+ tracking_info = request.body[:get_order_tracking_response][:get_order_tracking_result]
58
+
59
+ if tracking_info[:response_code] == "0"
60
+ { success: true, tracking_number: tracking_info[:tracking_number] }
61
+ else
62
+ { success: false, error_code: tracking_info[:response_code], error_message: tracking_info[:response_text] }
63
+ end
64
+ end
65
+
66
+ protected
67
+
68
+ def format_items(items)
69
+ formatted_items = Array.new
70
+
71
+ items.each do |item|
72
+ formatted_items.push({
73
+ 'ItemNumber' => item[:item_identifier],
74
+ 'Quantity' => item[:quantity]
75
+ })
76
+ end
77
+
78
+ { 'ItemRequest' => formatted_items }
79
+ end
80
+
81
+ # NOTE-david
82
+ # Errors:
83
+ # Invalid Token
84
+ # Customer is not setup in dealer network.
85
+ # Insufficient quantity for item 798681529582
86
+
87
+ end
88
+ end
@@ -0,0 +1,18 @@
1
+ module Davidsons
2
+ class User < Base
3
+
4
+ def initialize(options = {})
5
+ requires!(options, :username, :password)
6
+
7
+ @options = options
8
+ end
9
+
10
+ def authenticated?
11
+ connect(@options) { |ftp| ftp.pwd }
12
+ true
13
+ rescue Davidsons::NotAuthenticated
14
+ false
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Davidsons
2
+ VERSION = "1.0.0"
3
+ end
data/lib/davidsons.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "davidsons/version"
2
+
3
+ require 'net/ftp'
4
+
5
+ require 'davidsons/base'
6
+ require 'davidsons/user'
7
+ require 'davidsons/catalog'
8
+ require 'davidsons/inventory'
9
+ require 'davidsons/order'
10
+
11
+ module Davidsons
12
+
13
+ class NotAuthenticated < StandardError; end
14
+
15
+ class << self
16
+ attr_accessor :config
17
+ end
18
+
19
+ def self.config
20
+ @config ||= Configuration.new
21
+ end
22
+
23
+ def self.configure
24
+ yield(config)
25
+ end
26
+
27
+ class Configuration
28
+ attr_accessor :debug
29
+ attr_accessor :ftp_host
30
+ attr_accessor :proxy_url
31
+ attr_accessor :xmlns
32
+
33
+ alias debug? debug
34
+
35
+ def initialize
36
+ @debug ||= false
37
+ @ftp_host ||= "ftp.davidsonsinventory.com"
38
+ @proxy_url ||= "http://172.31.69.148:8888"
39
+ @xmlns ||= "https://dealernetwork.davidsonsinc.com/"
40
+ end
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: davidsons
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Knight
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.11.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.11.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: smarter_csv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.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: '5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5'
97
+ description: ''
98
+ email:
99
+ - viacoffee@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - davidsons.gemspec
114
+ - lib/davidsons.rb
115
+ - lib/davidsons/base.rb
116
+ - lib/davidsons/catalog.rb
117
+ - lib/davidsons/inventory.rb
118
+ - lib/davidsons/order.rb
119
+ - lib/davidsons/user.rb
120
+ - lib/davidsons/version.rb
121
+ homepage: ''
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.5.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Ruby library for Davidson's
145
+ test_files: []