vacuum-parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 232232ef97ca3baf4de2c950796af593852228be
4
+ data.tar.gz: a25304ce2550b951d45848f1b591f0b837978dd1
5
+ SHA512:
6
+ metadata.gz: 130f548323d96b4f11a1adace59135c80018d5deb877cb8e9429d3eb904874440342e8fc24b005e21a03d76678d01b70faf70d2a9f06050bc853e6776df33478
7
+ data.tar.gz: af84bfbff06f247dfd003915d03ece2beaa32cdd79936075bd8f49b07c381ea3d3f42b04b982ab3f5d2f13dfb51b34bca46ce67f0fb7aedb2fef92d7879fb905
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1 @@
1
+ --markup markdown
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vacuum-parser.gemspec
4
+ gemspec
@@ -0,0 +1,42 @@
1
+ # Vacuum::Parser
2
+
3
+ Parser for [Vacuum](https://github.com/hakanensari/vacuum)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'vacuum-parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install vacuum-parser
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'vacuum/parser'
25
+ Vacuum::Response.parser = Vacuum::Parser
26
+ response = request.item_search(...)
27
+ result = response.parse
28
+ if result.is_valid?
29
+ puts result.items.to_a.first.item_attributes['Title']
30
+ else
31
+ e = result.error
32
+ raise "#{e['Code']}:#{e['Message']}"
33
+ end
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/davispuh/vacuum-parser/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'yard'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc 'Run specs'
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ end
11
+
12
+ YARD::Rake::YardocTask.new(:doc) do |t|
13
+ end
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,21 @@
1
+ require 'nokogiri'
2
+ require_relative 'parser/version'
3
+ require_relative 'parser/itemsearch_response'
4
+
5
+ module Vacuum
6
+ class Parser
7
+ class ParserError < RuntimeError
8
+ end
9
+
10
+ def self.parse(body)
11
+ document = Nokogiri::XML(body)
12
+ name = document.root.name
13
+ case name
14
+ when 'ItemSearchResponse'
15
+ ItemSearchResponse.new(document)
16
+ else
17
+ ParserError.new(name + ' is not implemented!')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,174 @@
1
+ module Vacuum
2
+ class Parser
3
+ class ItemSearchResponse
4
+ attr_accessor :Document
5
+
6
+ def initialize(document)
7
+ raise ParserError.new('Not a XML::Document') unless document.is_a?(Nokogiri::XML::Document)
8
+ @Document = document
9
+ raise ParserError.new('Not a ItemSearchResponse') if document.root.name != 'ItemSearchResponse'
10
+ end
11
+
12
+ def operationRequest
13
+ OperationRequest.new(@Document.at('/xmlns:ItemSearchResponse/xmlns:OperationRequest'))
14
+ end
15
+
16
+ def isValid?
17
+ (n = @Document.at('/xmlns:ItemSearchResponse/xmlns:Items/xmlns:Request/xmlns:IsValid')) &&
18
+ (n.content == 'True')
19
+ end
20
+
21
+ def request
22
+ @Document.at('/xmlns:ItemSearchResponse/xmlns:Items/xmlns:Request/xmlns:ItemSearchRequest')
23
+ end
24
+
25
+ def error
26
+ error = @Document.at('/xmlns:ItemSearchResponse/xmlns:Items/xmlns:Request/xmlns:Errors/xmlns:Error')
27
+ error.content if error
28
+ end
29
+
30
+ def items
31
+ return nil unless isValid?
32
+ @Items ||= Items.new(@Document.at('/xmlns:ItemSearchResponse/xmlns:Items'))
33
+ end
34
+
35
+ class OperationRequest
36
+ def initialize(operation_request)
37
+ raise ParserError.new('Not a Node') unless operation_request.is_a?(Nokogiri::XML::Node)
38
+ # TODO
39
+ end
40
+ end
41
+
42
+ class Items
43
+ attr_accessor :Items
44
+ attr_accessor :TotalResults
45
+ attr_accessor :TotalPages
46
+ attr_accessor :MoreSearchResultsUrl
47
+
48
+ def initialize(items)
49
+ raise ParserError.new('Not a Node') unless items.is_a?(Nokogiri::XML::Node)
50
+ @Items = items
51
+ @TotalResults = @Items.at('./xmlns:TotalResults').content.to_i
52
+ @TotalPages = @Items.at('./xmlns:TotalPages').content.to_i
53
+ @MoreSearchResultsUrl = @Items.at('./xmlns:MoreSearchResultsUrl').content
54
+ end
55
+
56
+ def to_a
57
+ @List ||= (@Items / './xmlns:Item').inject([]) { |lst, itm| lst << Entry.new(itm) }
58
+ @List
59
+ end
60
+
61
+ class Entry
62
+ attr_accessor :Item
63
+ attr_accessor :ASIN
64
+ attr_accessor :ParentASIN
65
+ attr_accessor :DetailPageURL
66
+ attr_accessor :ItemLinks
67
+ attr_accessor :ItemAttributes
68
+ attr_accessor :OfferSummary
69
+ attr_accessor :Offers
70
+ def initialize(item)
71
+ raise ParserError.new('Not a Node') unless item.is_a?(Nokogiri::XML::Node)
72
+ @Item = item
73
+ @ASIN = (n = @Item.at('./xmlns:ASIN')) && n.content
74
+ @ParentASIN = (n = @Item.at('./xmlns:ParentASIN')) && n.content
75
+ @DetailPageURL = (n = @Item.at('./xmlns:DetailPageURL')) && n.content
76
+ @ItemLinks = (@Item / './xmlns:ItemLinks/xmlns:ItemLink').inject([]) { |lst, itm| lst << ItemLink.new(itm) }
77
+ @ItemAttributes = (n = @Item.at('./xmlns:ItemAttributes')) && ItemAttributes.new(n)
78
+ @OfferSummary = (n = @Item.at('./xmlns:OfferSummary')) && OfferSummary.new(n)
79
+ @Offers = (n = @Item.at('./xmlns:Offers')) && Offers.new(n)
80
+ end
81
+
82
+ class ItemLink
83
+ attr_accessor :ItemLink
84
+ attr_accessor :Description
85
+ attr_accessor :URL
86
+ def initialize(item_link)
87
+ @ItemLink = item_link
88
+ @Description = (n = @ItemLink.at('./xmlns:Description')) && n.content
89
+ @URL = (n = @ItemLink.at('./xmlns:URL')) && n.content
90
+ end
91
+ end
92
+
93
+ class ItemAttributes
94
+ attr_accessor :ItemAttributes
95
+ attr_accessor :Brand
96
+ attr_accessor :Manufacturer
97
+ attr_accessor :Model
98
+ attr_accessor :PartNumber
99
+ attr_accessor :ProductGroup
100
+ attr_accessor :Size
101
+ attr_accessor :Title
102
+ def initialize(item_attributes)
103
+ @ItemAttributes = item_attributes
104
+ @Brand = (n = @ItemAttributes.at('./xmlns:Brand')) && n.content
105
+ @Manufacturer = (n = @ItemAttributes.at('./xmlns:Manufacturer')) && n.content
106
+ @Model = (n = @ItemAttributes.at('./xmlns:Model')) && n.content
107
+ @PartNumber = (n = @ItemAttributes.at('./xmlns:PartNumber')) && n.content
108
+ @ProductGroup = (n = @ItemAttributes.at('./xmlns:ProductGroup')) && n.content
109
+ @Size = (n = @ItemAttributes.at('./xmlns:Size')) && n.content
110
+ @Title = (n = @ItemAttributes.at('./xmlns:Title')) && n.content
111
+ end
112
+ end
113
+
114
+ class OfferSummary
115
+ attr_accessor :OfferSummary
116
+ attr_accessor :LowestNewPrice
117
+ attr_accessor :LowestUsedPrice
118
+ attr_accessor :LowestRefurbishedPrice
119
+ attr_accessor :TotalNew
120
+ attr_accessor :TotalUsed
121
+ attr_accessor :TotalCollectible
122
+ attr_accessor :TotalRefurbished
123
+ def initialize(offer_summary)
124
+ @OfferSummary = offer_summary
125
+ @LowestNewPrice = (n = @OfferSummary.at('./xmlns:LowestNewPrice')) && Price.new(n)
126
+ @LowestUsedPrice = (n = @OfferSummary.at('./xmlns:LowestUsedPrice')) && Price.new(n)
127
+ @LowestRefurbishedPrice = (n = @OfferSummary.at('./xmlns:LowestRefurbishedPrice')) && Price.new(n)
128
+ @TotalNew = (n = @OfferSummary.at('./xmlns:TotalNew')) && n.content.to_i
129
+ @TotalUsed = (n = @OfferSummary.at('./xmlns:TotalUsed')) && n.content.to_i
130
+ @TotalCollectible = (n = @OfferSummary.at('./xmlns:TotalCollectible')) && n.content.to_i
131
+ @TotalRefurbished = (n = @OfferSummary.at('./xmlns:TotalRefurbished')) && n.content.to_i
132
+ end
133
+ end
134
+
135
+ class Offers
136
+ attr_accessor :Offers
137
+ def initialize(offers)
138
+ raise ParserError.new('Not a Node') unless offers.is_a?(Nokogiri::XML::Node)
139
+ @Offers = offers
140
+ end
141
+
142
+ def to_a
143
+ @List ||= (@Offers / './xmlns:Offer').inject([]) { |lst, itm| lst << Offer.new(itm) }
144
+ @List
145
+ end
146
+ end
147
+
148
+ class Offer
149
+ def initialize(offer)
150
+ raise ParserError.new('Not a Node') unless offer.is_a?(Nokogiri::XML::Node)
151
+ @Offer = offer
152
+ # TODO
153
+ end
154
+ end
155
+
156
+ class Price
157
+ attr_accessor :Price
158
+ attr_accessor :Amount
159
+ attr_accessor :CurrencyCode
160
+ attr_accessor :FormattedPrice
161
+ def initialize(price)
162
+ raise ParserError.new('Not a Node') unless price.is_a?(Nokogiri::XML::Node)
163
+ @Price = price
164
+ @Amount = (n = @Price.at('./xmlns:Amount')) && n.content.to_i
165
+ @CurrencyCode = (n = @Price.at('./xmlns:CurrencyCode')) && n.content.to_sym
166
+ @FormattedPrice = (n = @Price.at('./xmlns:FormattedPrice')) && n.content
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,5 @@
1
+ module Vacuum
2
+ class Parser
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ require 'simplecov'
3
+
4
+ if ENV['CI']
5
+ require 'coveralls'
6
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
7
+ end
8
+
9
+ SimpleCov.start
10
+
11
+ RSpec.configure do |config|
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ end
16
+
17
+ require_relative '../lib/vacuum/parser.rb'
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vacuum/parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'vacuum-parser'
8
+ spec.version = Vacuum::Parser::VERSION
9
+ spec.authors = ['Dāvis']
10
+ spec.email = ['davispuh@gmail.com']
11
+ spec.summary = 'Parser for Vacuum'
12
+ spec.homepage = 'https://github.com/davispuh/vacuum-parser'
13
+ spec.license = 'UNLICENSE'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'nokogiri'
21
+ spec.add_development_dependency 'bundler', '>= 1.7'
22
+ spec.add_development_dependency 'rake', '>= 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'simplecov'
25
+ spec.add_development_dependency 'yard'
26
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vacuum-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dāvis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - davispuh@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".yardopts"
107
+ - Gemfile
108
+ - README.md
109
+ - Rakefile
110
+ - UNLICENSE
111
+ - lib/vacuum/parser.rb
112
+ - lib/vacuum/parser/itemsearch_response.rb
113
+ - lib/vacuum/parser/version.rb
114
+ - spec/spec_helper.rb
115
+ - vacuum-parser.gemspec
116
+ homepage: https://github.com/davispuh/vacuum-parser
117
+ licenses:
118
+ - UNLICENSE
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.6
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Parser for Vacuum
140
+ test_files:
141
+ - spec/spec_helper.rb
142
+ has_rdoc: