honeys-place 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71351a33d6e2c6f96e7092ab8c809c139b7ed1a7
4
+ data.tar.gz: 1ddc4dff2ab7cd2a336a8d1acc0c2508e576d778
5
+ SHA512:
6
+ metadata.gz: df23261220ad988a1b154423a2fb7113e4548314261d23d69ff27ee25b75366b31ba206d8ab8605ef583ff1b41df2c965f04fa5f6ad1d2b9c72d89c2228861be
7
+ data.tar.gz: 7c49d62cbb2c9d86e734a278a5b9a31ce666d5aa8487156421c1a9ab5df565bf4a4dd5a3b6d69c925d2a61e79d8c129d21601d6b2c4d4dbf78382c09789a8e7a
data/lib/honey.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'nokogiri'
4
+ require 'nokogiri/xml/document_to_hash'
5
+
6
+ require 'honey/client'
7
+ require 'honey/builder'
8
+ require 'honey/order'
9
+ require 'honey/version'
@@ -0,0 +1,52 @@
1
+ module Honey
2
+ class Builder
3
+ attr_reader :username, :password
4
+
5
+ def initialize(username, password)
6
+ @username = username
7
+ @password = password
8
+ end
9
+
10
+ def build(data)
11
+ @data = Nokogiri::XML::Builder.new do |xml|
12
+ xml.HPEnvelope do
13
+ xml.username(username)
14
+ xml.password(password)
15
+ data.each_pair do |key, value|
16
+ xml_pair(xml, key, value)
17
+ end
18
+ end
19
+ end.to_xml.strip
20
+ end
21
+
22
+ def destruct(data)
23
+ Nokogiri::XML(data).to_hash
24
+ end
25
+
26
+ private
27
+ def xml_pair(xml, key, value)
28
+ key = key.to_sym
29
+ if value.respond_to? :each_pair
30
+ xml.send(key) do
31
+ value.each_pair { |k, v| xml_pair(xml, k, v) }
32
+ end
33
+ elsif value.respond_to? :each
34
+ xml.send(key) do
35
+ value.each { |val| xml_array(xml, val) }
36
+ end
37
+ else
38
+ xml.send(key.to_sym, value)
39
+ end
40
+ end
41
+
42
+ def xml_array(xml, value)
43
+ if value.respond_to? :each_pair
44
+ value.each_pair { |k, v| xml_pair(xml, k, v) }
45
+ elsif value.respond_to? :each
46
+ value.each { |v| v }
47
+ else
48
+ value
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,75 @@
1
+ module Honey
2
+ class Client
3
+ include HTTParty
4
+ base_uri 'http://ws.honeysplace.com/ws/'
5
+
6
+ def initialize(username = '', password = '')
7
+ login(username, password)
8
+ self
9
+ end
10
+
11
+ # type :xml, :text
12
+ def self.data_feed(type = :xml, path = '/tmp/honey')
13
+ IO.popen("curl #{self.class.base_uri}/#{data_feed_url(type)} -o #{path}.#{type}")
14
+ end
15
+
16
+ def self.data_feed_url(type = :xml)
17
+ "xml/honeysinventory" << case type
18
+ when :xml
19
+ "v2_0.xml"
20
+ when :txt
21
+ "_v_1.0.txt"
22
+ end
23
+ end
24
+
25
+ def submit_order(order)
26
+ post build_order(order)
27
+ end
28
+
29
+ def order_status(reference_number)
30
+ post build_order_status(reference_number)
31
+ end
32
+
33
+ def stock_check(*skus)
34
+ results = {}
35
+ if skus.first.is_a? Array
36
+ skus = skus.first
37
+ end
38
+ skus.each_slice(25) do |sku_group|
39
+ response = post(build_stock_check(*sku_group)).parsed_response
40
+ data = response["message"]["stock"]["item"]
41
+ data = [data] if sku_group.count == 1 # Honey's API Quirk
42
+ data.each do |datum|
43
+ results[datum["sku"]] = datum["qty"]
44
+ end
45
+ end
46
+ results
47
+ end
48
+
49
+ def build_order(order)
50
+ request(order: order)
51
+ end
52
+
53
+ def build_order_status(reference_number)
54
+ request(orderstatus: reference_number)
55
+ end
56
+
57
+ def build_stock_check(*skus)
58
+ request(stockcheck: skus.map { |sku| { sku: sku } })
59
+ end
60
+
61
+ private
62
+
63
+ def post(xml)
64
+ self.class.post('', { query: { xmldata: xml }})
65
+ end
66
+
67
+ def login(username, password)
68
+ @builder = Honey::Builder.new(username, password)
69
+ end
70
+
71
+ def request(object)
72
+ @builder.build(object)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,53 @@
1
+ module Honey
2
+ class Order
3
+ def self.attr_accessor(*attributes)
4
+ @attributes ||= []
5
+ @attributes.concat attributes
6
+ super(*attributes)
7
+ end
8
+
9
+ def self.attributes
10
+ @attributes
11
+ end
12
+
13
+ def attributes
14
+ self.class.attributes
15
+ end
16
+
17
+ attr_accessor :reference, :shipby, :items, :last, :first, :address1, :address2, :city, :state, :zip, :country, :phone, :emailaddress, :instructions
18
+
19
+ def initialize(args = {})
20
+ update(args)
21
+ end
22
+
23
+ def update(args = {})
24
+ args.each_pair do |key, value|
25
+ self.send("#{key}=".to_sym, value)
26
+ end
27
+ end
28
+
29
+ def each_pair(&block)
30
+ present_attributes.each do |key|
31
+ yield(key, self.send(key), block)
32
+ end
33
+ end
34
+
35
+ def valid?
36
+ (required_attributes - present_attributes) == []
37
+ end
38
+
39
+ def invalid?
40
+ !valid?
41
+ end
42
+
43
+ def required_attributes
44
+ (attributes - [:address2, :reference, :instructions])
45
+ end
46
+
47
+ def present_attributes
48
+ attributes.collect do |attr|
49
+ attr unless self.send(attr).nil?
50
+ end.compact
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Honey
2
+ Version = "0.0.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Document < Nokogiri::XML::Node
4
+ def to_hash
5
+ children_to_hash(children)
6
+ end
7
+
8
+ private
9
+ def children_to_hash(children)
10
+ hash = {}
11
+ children.each do |child|
12
+ if child.children.any?
13
+ hash[child.name.to_sym] = children_to_hash(child.children)
14
+ else
15
+ hash = child.content.to_s
16
+ end
17
+ end
18
+ hash
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class BuilderTest < Minitest::Test
4
+ def setup
5
+ @builder = Honey::Builder.new("username", "password")
6
+ end
7
+
8
+ def test_builds_nested_hash_xml
9
+ assert_match_xpath "hello/there", @builder.build(hello: { there: "buddy" })
10
+ end
11
+
12
+ def test_builds_array_xml
13
+ assert_match_xpath "skus/sku", @builder.build(skus: [{sku: 12345}, {sku: 67890}])
14
+ end
15
+
16
+ def test_breaks_down_xml
17
+ assert_equal @builder.destruct("<hello><there>buddy</there></hello>"), { hello: { there: "buddy" } }
18
+ end
19
+
20
+ def test_builds_honey_order
21
+ order = Honey::Order.new(reference: "TEST")
22
+ assert_match_xpath "order/reference", @builder.build(order: order)
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class ClientTest < Minitest::Test
4
+ def setup
5
+ @client = Honey::Client.new
6
+ end
7
+
8
+ def test_data_feed_url
9
+ assert_equal Honey::Client.data_feed_url(:xml), "xml/honeysinventoryv2_0.xml"
10
+ assert_equal Honey::Client.data_feed_url(:txt), "xml/honeysinventory_v_1.0.txt"
11
+ end
12
+
13
+ def test_builds_order
14
+ assert_match_xpath "order/items/item", @client.build_order(valid_order)
15
+ end
16
+
17
+ def test_builds_stock_check
18
+ assert_match_xpath "stockcheck/sku", @client.build_stock_check(12345)
19
+ end
20
+
21
+ def test_builds_order_status
22
+ assert_match_xpath "orderstatus", @client.build_order_status(12345)
23
+ end
24
+
25
+ # These only work when supplied valid credentials
26
+ #
27
+ # def test_checks_stock_as_arguments
28
+ # results = @client.stock_check("BMS28515", "KL-468B")
29
+ # assert results.has_key?("BMS28515")
30
+ # assert results.has_key?("KL-468B")
31
+ # end
32
+
33
+ # def test_checks_stock_as_array
34
+ # results = @client.stock_check(["BMS28515", "KL-468B"])
35
+ # assert results.has_key?("BMS28515")
36
+ # assert results.has_key?("KL-468B")
37
+ # end
38
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class OrderTest < Minitest::Test
4
+ def setup
5
+ @order = Honey::Order.new
6
+ end
7
+
8
+ def teardown
9
+ @order = nil
10
+ end
11
+
12
+ def test_update
13
+ @order.update(last: "Shreve", items: [:item, :item])
14
+ assert_equal "Shreve", @order.last
15
+ assert_equal [:item, :item], @order.items
16
+ end
17
+
18
+ def test_validity
19
+ assert @order.invalid?
20
+ @order.required_attributes.each do |attr|
21
+ @order.send("#{attr}=".to_sym, "hello")
22
+ end
23
+ assert @order.valid?
24
+ end
25
+
26
+ def test_present_attributes
27
+ @order.update(last: "Shreve", first: "Jacob")
28
+ assert_equal @order.present_attributes, [:last, :first]
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class HoneyTest < Minitest::Test
4
+ def test_version_exists
5
+ assert defined? Honey::Version
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class DocumentToHashTest < Minitest::Test
4
+ def setup
5
+ @document = Nokogiri::XML("<hello><there>buddy</there></hello")
6
+ end
7
+
8
+ def test_document_to_hash
9
+ assert_equal @document.to_hash, { hello: { there: "buddy" } }
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..','lib'))
2
+ require 'honey'
3
+
4
+ def valid_order
5
+ Honey::Order.new({
6
+ items: [
7
+ { item: { sku: 1234, qty: 1 } },
8
+ { item: { sku: 5678, qty: 1 } }
9
+ ],
10
+ last: "Shreve",
11
+ first: "Jacob",
12
+ address1: "1234 East St.",
13
+ city: "Fowlerville",
14
+ state: "MI",
15
+ zip: 48836,
16
+ country: "US",
17
+ phone: "(123) 456-7890",
18
+ emailaddress: "jacob@example.com",
19
+ instructions: "Leave the package behind the wooden bear"
20
+ })
21
+ end
22
+
23
+ def assert_match_xpath(expected, actual, message = nil)
24
+ expected = "/HPEnvelope/#{expected}"
25
+ doc = Nokogiri::XML(actual)
26
+ nodes = doc.xpath(expected)
27
+ assert !nodes.empty?, message || "Expected to find xpath #{expected} in response:\n#{actual}"
28
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: honeys-place
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Evan Shreve
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.13.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.13'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.13.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: nokogiri
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.6'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.6.1
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.6'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.6.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: guard-minitest
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '2'
67
+ description: |2
68
+ Honey's Place is a wrapper for the Honey's Place API which easily enables
69
+ placing orders, checking order statuses, and stock checking.
70
+ email: jacob@sugutoys.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/honey.rb
76
+ - lib/honey/builder.rb
77
+ - lib/honey/client.rb
78
+ - lib/honey/order.rb
79
+ - lib/honey/version.rb
80
+ - lib/nokogiri/xml/document_to_hash.rb
81
+ - test/honey/builder_test.rb
82
+ - test/honey/client_test.rb
83
+ - test/honey/order_test.rb
84
+ - test/honey_test.rb
85
+ - test/nokogiri/xml/document_to_hash_test.rb
86
+ - test/test_helper.rb
87
+ homepage: http://github.com/SuguToys/honeys-place
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A wrapper for the Honey's Place API
111
+ test_files:
112
+ - test/honey/builder_test.rb
113
+ - test/honey/client_test.rb
114
+ - test/honey/order_test.rb
115
+ - test/honey_test.rb
116
+ - test/nokogiri/xml/document_to_hash_test.rb
117
+ - test/test_helper.rb