letsshop_mapper 0.8.6beta

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = Let's Shop Mapper
2
+
3
+ == About
4
+
5
+ Let's Shop Mapper is a Ruby library that allows to search and parse on Let's Shop Service
6
+
7
+ == Install
8
+
9
+ gem install letsshop_mapper
10
+
11
+ == Examples
12
+
13
+ lshop = LetsShopMapper::Connection::Base::new("hostname", "key")
14
+ lshop.find({:q => "robe", :start => 10, :nhits => 5, :f => "refine:'universe:mode',refine:'gender:femme'"})
15
+ lshop.feed.entries.each { |e|
16
+ puts "Id: #{e.id}\n"
17
+ puts "Link: #{e.link}\n"
18
+ puts "Title: #{e.title}\n"
19
+ puts "Description: #{e.description}\n"
20
+ puts "Thumbnail: #{e.thumb}\n"
21
+ puts "Price: #{e.price}\n"
22
+ puts "Supplier: #{e.supplier}\n"
23
+ puts "------"
24
+ puts "facets"
25
+ puts "------"
26
+ e.facets.each { |f|
27
+ puts "Title: #{f.title}\n"
28
+ puts "Type: #{f.type}\n"
29
+ puts "Filter: #{f.filter.str_value}\n"
30
+ puts "Role: #{f.role}\n"
31
+ puts "Selected: #{f.selected}\n"
32
+ }
33
+ puts "-----------------------"
34
+ }
35
+
36
+ See also the test code, especially letsshop_mapper_test.rb
37
+
38
+ == License
39
+
40
+ Copyright (C) 2010 Happun.
41
+
42
+ Licensed under the Apache License, Version 2.0 (the "LICENSE");
43
+ you may not use this file except in compliance with the License.
44
+ You may obtain a copy of the License at
45
+
46
+ http://www.apache.org/licenses/LICENSE-2.0
47
+
48
+ Unless required by applicable law or agreed to in writing, software
49
+ distributed under the License is distributed on an "AS IS" BASIS,
50
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51
+ See the License for the specific language governing permissions and
52
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,90 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'rake/testtask'
5
+ require 'rake/gempackagetask'
6
+ require 'find'
7
+
8
+ require File.expand_path('../lib/letsshop_mapper/version', __FILE__)
9
+
10
+ PKGNAME = 'letsshop_mapper'
11
+ PKGVERSION = version = File.read("LETSSHOP_MAPPER_VERSION").strip
12
+
13
+ PKGFILES = [ 'README.rdoc', 'LICENSE', 'Rakefile' ]
14
+ Find.find('init.rb', 'lib/', 'test/', 'rails/') do |f|
15
+ if FileTest.directory?(f) and f =~ /\.svn/
16
+ Find.prune
17
+ else
18
+ PKGFILES << f
19
+ end
20
+ end
21
+
22
+ PKGFILES.reject! { |f| f =~ /^test\/(fixtures|.*_output)\// }
23
+
24
+ desc 'Default: run unit tests'
25
+ task :default => [:test]
26
+
27
+ desc 'Create gemspec file'
28
+ task :gemspec do
29
+ spec = Gem::Specification.new do |s|
30
+ s.platform = Gem::Platform::RUBY
31
+ s.author = 'happun'
32
+ s.email = 'dev@happun.com'
33
+ s.homepage = 'http://github.com/rgaidot/letsshop-mapper'
34
+ s.summary = "Let's Shop Mapper is a Ruby library that allows to search and parse on Let's Shop Service"
35
+ s.name = PKGNAME
36
+ s.rubyforge_project = PKGNAME
37
+ s.version = PKGVERSION
38
+ s.require_path = 'lib'
39
+ s.has_rdoc = true
40
+ s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
41
+ s.rdoc_options << '--line-numbers' << '--inline-source' << 'README.rdoc'
42
+ s.files = PKGFILES
43
+ s.description = "Let's Shop Mapper is a Ruby library that allows to search and parse on Let's Shop Service."
44
+ end
45
+ File.open("#{spec.name}.gemspec", "w") do |f|
46
+ f.write spec.to_ruby
47
+ end
48
+ end
49
+
50
+ desc 'Build the gem'
51
+ task :install => :gemspec do
52
+ system "gem build letsshop_mapper.gemspec"
53
+ end
54
+
55
+ desc 'Build the gem'
56
+ task :release => :install do
57
+ system "gem push letsshop_mapper-#{PKGVERSION}.gem"
58
+ end
59
+
60
+ desc 'Clean Up'
61
+ task :clean do |t|
62
+ FileUtils.rm_rf "doc"
63
+ FileUtils.rm_rf "pkg"
64
+ end
65
+
66
+ desc 'Generate documentation for the letsshop_mapper plugin.'
67
+ Rake::RDocTask.new(:rdoc) do |rdoc|
68
+ rdoc.rdoc_dir = "doc"
69
+ rdoc.title = "Let's Shop Mapper"
70
+ rdoc.options << '--line-numbers' << '--inline-source'
71
+ rdoc.options << '--charset' << 'utf-8'
72
+ rdoc.options << '--line-numbers' << '--inline-source' << 'README.rdoc'
73
+ rdoc.rdoc_files.include('README*')
74
+ rdoc.rdoc_files.include('lib/**/*.rb')
75
+ end
76
+
77
+ desc 'Package letsshop_mapper plugin.'
78
+ Rake::PackageTask.new(PKGNAME, PKGVERSION) do |p|
79
+ p.need_tar = true
80
+ p.need_zip = true
81
+ p.package_files = PKGFILES
82
+ end
83
+
84
+ desc 'Test the letsshop_mapper plugin.'
85
+ Rake::TestTask.new(:test) do |t|
86
+ t.libs << 'lib'
87
+ t.libs << 'test'
88
+ t.pattern = 'test/**/*_test.rb'
89
+ t.verbose = true
90
+ end
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'letsshop_mapper'
2
+ letsshop_config = YAML.load_file("#{RAILS_ROOT}/config/letsshop.yml")[Rails.env]
3
+ LSHOP = LetsShopMapper::Connection::Base::new(letsshop_config["server"], letsshop_config["key"])
@@ -0,0 +1,63 @@
1
+ require 'uri'
2
+ require "cgi"
3
+ require 'net/http'
4
+
5
+ module LetsShopMapper
6
+ module Connection
7
+ class Base
8
+ attr_reader :server
9
+ attr_reader :key
10
+ attr_reader :response
11
+
12
+ attr_reader :feed
13
+ attr_reader :tree
14
+
15
+ def initialize(server = nil, key = nil)
16
+ @server = server
17
+ @key = key
18
+ end
19
+ def connect_and_get()
20
+ uri = "http://#{@server}/search/#{@key}"
21
+ begin
22
+ @response = Net::HTTP.get_response(URI.parse(uri))
23
+ @feed = @response.body
24
+ rescue Exception
25
+ raise LetsShopMapper::RequestBaseSearchException::new
26
+ end
27
+ end
28
+ def find(conditions = {})
29
+ uri = "http://#{@server}/search/#{@key}"
30
+ query = "?"
31
+ query << "q=#{CGI.escape(conditions[:q])}" if conditions[:q] != nil
32
+ query << "&products=#{conditions[:products]}" if conditions[:products] != nil
33
+ query << "&start=#{conditions[:start]}" if conditions[:start] != nil
34
+ query << "&nhits=#{conditions[:nhits]}" if conditions[:nhits] != nil
35
+ if conditions[:f] != nil
36
+ refine = ""
37
+ conditions[:f].split(',').each do |f|
38
+ refine << f.gsub(/(refine:)+(\047)+(.+)(\047)/, '\1"\3",')
39
+ end
40
+ query << "&f=#{CGI.escape(refine)}"
41
+ end
42
+ query << "&c=#{CGI.escape(conditions[:c])}" if conditions[:c] != nil
43
+ query << "&sort=#{CGI.escape(conditions[:sort])}" if conditions[:sort] != nil
44
+ begin
45
+ @response = Net::HTTP.get_response(URI.parse("#{uri}/#{query}"))
46
+ @feed = LetsShopMapper::Model::OpenSearch::Feed::new(@response.body)
47
+ rescue Exception
48
+ raise LetsShopMapper::Error::RequestBaseSearchException::new
49
+ end
50
+ end
51
+ def get_tree(category = nil)
52
+ uri = "http://#{@server}/tree/#{@key}"
53
+ id = "#{category}" if category != nil
54
+ begin
55
+ @response = Net::HTTP.get_response(URI.parse("#{uri}/#{id}"))
56
+ @tree = LetsShopMapper::Model::Tree::Tree::new(@response.body)
57
+ rescue Exception
58
+ raise LetsShopMapper::Error::RequestBaseSearchException::new
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module LetsShopMapper
2
+ class Error < StandardError; end
3
+ class UnknownFeedTypeException < Error; end
4
+ class RequestBaseSearchException < Error; end
5
+ end
@@ -0,0 +1,42 @@
1
+ include REXML
2
+ module LetsShopMapper
3
+ module Model
4
+ module Base
5
+ class Category
6
+ attr_accessor :id
7
+ attr_accessor :name
8
+ attr_reader :children
9
+ attr_reader :filters
10
+
11
+ def initialize()
12
+ @id, @name = nil
13
+ @children = []
14
+ @filters = []
15
+ end
16
+ def recurse(category, par = nil)
17
+ category.each_child_element do |child|
18
+ if child.name == "category"
19
+ chl = add_category(child)
20
+ par.children << chl
21
+ elsif child.name == "filter"
22
+ par.filters << Filter::new(child.attributes.get_attribute("value"))
23
+ end
24
+ recurse(child, chl)
25
+ end
26
+ end
27
+ def add_category(category)
28
+ c = Category::new
29
+ c.name = category.elements['name'].text
30
+ c.id = category.elements['name'].attributes.get_attribute("id").value
31
+ return c
32
+ end
33
+ def to_map
34
+ {@name => @children.empty? ? nil : @children.map {|child| child.to_map}}
35
+ end
36
+ def to_s(localtime = true)
37
+ self.to_map.to_yaml
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ module LetsShopMapper
2
+ module Model
3
+ module Base
4
+ class Facet
5
+ attr_reader :title
6
+ attr_reader :type
7
+ attr_reader :filter
8
+ attr_reader :role
9
+ attr_reader :selected
10
+ attr_reader :nhits
11
+
12
+ attr_reader :feed
13
+ attr_reader :xml
14
+
15
+ def initialize(facet = nil, feed = nil)
16
+ @feed = feed
17
+ @xml = facet
18
+ @title, @type, @filter, @role, @selected, @nhits = nil
19
+ @selected = false
20
+ parse(facet) if facet
21
+ end
22
+
23
+ def parse(facet)
24
+ @title = facet.attributes.get_attribute("title").value.split(":")[1]
25
+ @type = facet.attributes.get_attribute("title").value.split(":")[0]
26
+ @filter = Filter::new(facet.attributes.get_attribute("title"))
27
+ @role = facet.attributes.get_attribute("role").value
28
+ if facet.attributes.get_attribute("nhits")
29
+ @nhits = facet.attributes.get_attribute("nhits").value
30
+ end
31
+ if facet.attributes.get_attribute("selected")
32
+ @selected = LetsShopMapper.Boolean(facet.attributes.get_attribute("selected").value)
33
+ end
34
+ end
35
+
36
+ def to_s(localtime = true)
37
+ s = ''
38
+ s += "Title: #{@title}\n"
39
+ s += "Type: #{@type}\n"
40
+ s += "Filter: #{@filter.str_value}\n"
41
+ s += "Role: #{@role}\n"
42
+ s += "Selected: #{@selected}\n"
43
+ s += "nhits: #{@nhits}\n"
44
+ s += "\n"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,30 @@
1
+ module LetsShopMapper
2
+ module Model
3
+ module Base
4
+ class Filter
5
+ attr_reader :key
6
+ attr_reader :value
7
+ attr_reader :str_value
8
+
9
+ def initialize(filtr = nil)
10
+ @key, @value, @str_value = nil
11
+ @position = 0
12
+ @depend = false
13
+ parse(filtr) if filtr
14
+ end
15
+ def parse(filtr)
16
+ @str_value = filtr.value
17
+ @key = filtr.value.split(":")[0]
18
+ @value = filtr.value.split(":")[1]
19
+ end
20
+ def to_s(localtime = true)
21
+ s = ''
22
+ s += "Key: #{@key}\n"
23
+ s += "Value: #{@value}\n"
24
+ s += "Filter: #{@str_value}\n"
25
+ s += "\n"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,85 @@
1
+ module LetsShopMapper
2
+ module Model
3
+ module OpenSearch
4
+ class Entry
5
+ attr_reader :id
6
+ attr_reader :link
7
+ attr_reader :title
8
+ attr_reader :description
9
+ attr_reader :price
10
+ attr_reader :currency
11
+ attr_reader :discount
12
+ attr_reader :older_price
13
+ attr_reader :thumb
14
+ attr_reader :supplier
15
+ attr_reader :facets
16
+
17
+ attr_reader :feed
18
+ attr_reader :xml
19
+
20
+ def initialize(entry = nil, feed = nil)
21
+ @feed = feed
22
+ @xml = entry
23
+ @id, @link, @title, @description, @price, @thumb, @supplier = nil
24
+ @facets = []
25
+ parse(entry) if entry
26
+ end
27
+
28
+ # parse entry
29
+ def parse(entry)
30
+ @id = entry.elements["id"].text
31
+ if (e = entry.elements['link'])
32
+ @link = e.attribute('href').value
33
+ end
34
+ if (e = entry.elements['title']) && e.text
35
+ @title = e.text
36
+ end
37
+ if (e = entry.elements['description']) && e.text
38
+ @description = e.text
39
+ end
40
+ if (e = entry.elements['letsshop/thumb'])
41
+ @thumb = e.text
42
+ end
43
+ if (e = entry.elements['letsshop/price']) && e.text
44
+ @price = e.text
45
+ @currency = e.attributes.get_attribute("currency").value
46
+ @discount = e.attributes.get_attribute("discount").value
47
+ @older_price = e.attributes.get_attribute("older").value
48
+ end
49
+ entry.each_element('Query') do |f|
50
+ if !f.attributes.get_attribute("title").value.index("supplier:").nil?
51
+ @supplier = f.attributes.get_attribute("title").value.gsub("supplier:","")
52
+ end
53
+ end
54
+ entry.each_element('Query') do |e|
55
+ @facets << Base::Facet::new(e, self)
56
+ end
57
+ end
58
+
59
+ def get_facets_by(scope)
60
+ results = []
61
+ @facets.each { |f|
62
+ if f.type == scope
63
+ then results << f end }
64
+ return results
65
+ end
66
+
67
+ def to_s(localtime = true)
68
+ s = ''
69
+ s += "Id: #{@id}\n"
70
+ s += "Link: #{@link}\n"
71
+ s += "Title: #{@title}\n"
72
+ s += "Description: #{@description}\n"
73
+ s += "Thumbnail: #{@thumb}\n"
74
+ s += "Price: #{@price} #{@currency}\n"
75
+ s += "Discount: #{@older_price} (#{@discount})\n"
76
+ s += "Supplier: #{@supplier}\n"
77
+ s += "----------------------------------\n"
78
+ @facets.each { |i| s += i.to_s(localtime) }
79
+ s += "\n"
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
@@ -0,0 +1,78 @@
1
+ module LetsShopMapper
2
+ module Model
3
+ module OpenSearch
4
+ class Feed
5
+ attr_reader :title
6
+ attr_reader :link
7
+ attr_reader :entries
8
+ attr_reader :startindex
9
+ attr_reader :itemsperpage
10
+ attr_reader :totalresults
11
+
12
+ attr_reader :facets
13
+
14
+ attr_reader :xml
15
+ attr_reader :encoding
16
+
17
+ def initialize(str = nil)
18
+ @title, @link, @encoding = nil
19
+ @entries = []
20
+ @facets = []
21
+ parse(str) if str
22
+ end
23
+
24
+ def parse(str)
25
+ doc = REXML::Document.new(str)
26
+ @xml = doc.root
27
+ @encoding = doc.encoding
28
+ if doc.root.elements['/feed']
29
+ if (e = doc.root.elements['/feed/title']) && e.text
30
+ @title = e.text
31
+ end
32
+ if (e = doc.root.elements['/feed/link'])
33
+ @link = e.attribute('href').value
34
+ end
35
+ if (e = doc.root.elements['/feed/startIndex']) && e.text
36
+ @startindex = e.text
37
+ end
38
+ if (e = doc.root.elements['/feed/itemsPerPage']) && e.text
39
+ @itemsperpage = e.text
40
+ end
41
+ if (e = doc.root.elements['/feed/totalResults']) && e.text
42
+ @totalresults = e.text
43
+ end
44
+ # Facets
45
+ doc.root.each_element_with_attribute('role', 'subset') do |f|
46
+ @facets << Base::Facet::new(f, self)
47
+ end
48
+ # Entries
49
+ doc.root.each_element('/feed/entry') do |e|
50
+ @entries << Entry::new(e, self)
51
+ end
52
+ else
53
+ raise LetsShopMapper::Error::UnknownFeedTypeException::new
54
+ end
55
+ end
56
+
57
+ def get_facets_by(scope)
58
+ results = []
59
+ @facets.each { |f| if f.type == scope then results << f end }
60
+ return results
61
+ end
62
+
63
+ def to_s(localtime = true)
64
+ s = ''
65
+ s += "Encoding: #{@encoding}\n"
66
+ s += "Title: #{@title}\n"
67
+ s += "Link: #{@link}\n"
68
+ s += "Entries:\n"
69
+ @entries.each { |i| s += i.to_s(localtime) }
70
+ s += "Facets\n"
71
+ @facets.each { |i| s += i.to_s(localtime) }
72
+ s += "\n"
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
@@ -0,0 +1,35 @@
1
+ module LetsShopMapper
2
+ module Model
3
+ module Tree
4
+ class Tree
5
+ attr_reader :categories
6
+
7
+ attr_reader :xml
8
+
9
+ def initialize(str = nil)
10
+ parse(str) if str
11
+ end
12
+ def parse(str)
13
+ doc = REXML::Document.new(str)
14
+ @xml = doc.root
15
+ if doc.root.elements['/categories']
16
+ doc.root.each_element('/categories/category') do |c|
17
+ @categories = Base::Category::new
18
+ @categories.recurse(c, @categories)
19
+ end
20
+ elsif doc.root.elements['/category']
21
+ doc.root.each_element('/category') do |c|
22
+ @categories = Base::Category::new
23
+ @categories.recurse(c, @categories)
24
+ end
25
+ else
26
+ raise LetsShopMapper::Error::UnknownFeedTypeException::new
27
+ end
28
+ end
29
+ def to_s(localtime = true)
30
+ s = @categories.to_s(localtime)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module REXML
2
+ def each_child_element(&block)
3
+ self.elements.each { |node|
4
+ block.call(node)
5
+ }
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module LetsShopMapper
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 8
5
+ TINY = 6
6
+ BUILD = "beta"
7
+ STRING = [MAJOR, MINOR, TINY, BUILD].join('.')
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'tempfile'
3
+ require 'rexml/document'
4
+
5
+ module LetsShopMapper
6
+ autoload :REXML, "letsshop_mapper/patch/rexml"
7
+ autoload :Version, "letsshop_mapper/version"
8
+ autoload :Connection, "letsshop_mapper/connection"
9
+ autoload :Error, "letsshop_mapper/exceptions"
10
+ module Model
11
+ module Base
12
+ autoload :Facet, "letsshop_mapper/model/base/facet"
13
+ autoload :Category, "letsshop_mapper/model/base/category"
14
+ autoload :Filter, "letsshop_mapper/model/base/filter"
15
+ end
16
+ module OpenSearch
17
+ autoload :Feed, "letsshop_mapper/model/opensearch/feed"
18
+ autoload :Entry, "letsshop_mapper/model/opensearch/entry"
19
+ end
20
+ module Tree
21
+ autoload :Tree, "letsshop_mapper/model/tree/tree"
22
+ end
23
+ end
24
+ def self.Boolean(string)
25
+ return true if string == true || string =~ /^true$/i
26
+ return false if string == false || string.nil? || string =~ /^false$/i
27
+ raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
28
+ end
29
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__),'../init.rb')
data/test/letsshop.yml ADDED
@@ -0,0 +1,11 @@
1
+ development:
2
+ server: letsshop.dev.happun.com
3
+ key: 82842d494583280b940b208664f34014
4
+
5
+ test:
6
+ server:
7
+ key:
8
+
9
+ production:
10
+ server:
11
+ key:
@@ -0,0 +1,159 @@
1
+ require 'test_helper'
2
+
3
+ FIXTURESDIR = "test/fixtures"
4
+
5
+ module LetsShopMapper
6
+ module Test
7
+ class LetsShopMapperTest < ::Test::Unit::TestCase
8
+ def test_search_parse_file
9
+ Dir.foreach(FIXTURESDIR) do |f|
10
+ next if f !~ /letsshop*.xml$/
11
+ puts "Checking #{f}"
12
+ str = File::read(FIXTURESDIR + '/' + f)
13
+ lshopFeed = LetsShopMapper::Model::OpenSearch::Feed::new(str)
14
+ assert_equal "Search: ", lshopFeed.title
15
+ assert_equal "http://letsshop.dev.happun.com/search/82842d494583280b940b208664f34014", lshopFeed.link
16
+ assert_equal "Search: ", lshopFeed.title
17
+ assert_equal "0", lshopFeed.startindex
18
+ assert_equal "2", lshopFeed.itemsperpage
19
+ assert_equal "228903", lshopFeed.totalresults
20
+ f = lshopFeed.get_facets_by("universe")
21
+ assert_equal "mode", f[0].title
22
+ assert_equal "universe", f[0].type
23
+ assert_equal "subset", f[0].role
24
+ assert_equal "225709", f[0].nhits
25
+ assert_equal "b67ab565e1e0f2661e08845111a2106f", lshopFeed.entries[0].id
26
+ assert_equal "6e0512b7f44299b3e1acb38939cf7d3e", lshopFeed.entries[1].id
27
+ assert_equal "Flirt - Collier en or 750 jaune et diamants", lshopFeed.entries[0].title
28
+ assert_equal "Fauteuil style Louis Philippe en cuir reptile noir", lshopFeed.entries[1].title
29
+ assert_equal "18900.0", lshopFeed.entries[0].price
30
+ assert_equal "15800.0", lshopFeed.entries[1].price
31
+ f = lshopFeed.entries[0].get_facets_by("universe")
32
+ assert_equal "mode", f[0].title
33
+ assert_equal "universe", f[0].type
34
+ assert_equal "subset", f[0].role
35
+ assert_equal "universe:mode", f[0].filter.str_value
36
+ assert_equal false, f[0].selected
37
+ end
38
+ end
39
+ def test_search_with_query
40
+ lshop = LetsShopMapper::Connection::Base::new("letsshop.dev.happun.com", "82842d494583280b940b208664f34014")
41
+ lshop.find({:q => "robe", :start => 10, :nhits => 5})
42
+ assert_equal "UTF-8", lshop.feed.encoding
43
+ assert_equal "Search: robe", lshop.feed.title
44
+ assert_equal "10", lshop.feed.startindex
45
+ assert_equal "5", lshop.feed.itemsperpage
46
+ assert_equal "http://letsshop.dev.happun.com/search/82842d494583280b940b208664f34014", lshop.feed.link
47
+ f = lshop.feed.entries[0].get_facets_by("universe")
48
+ assert_equal "mode", f[0].title
49
+ assert_equal "subset", f[0].role
50
+ f = lshop.feed.entries[0].get_facets_by("gender")
51
+ assert_equal "femme", f[0].title
52
+ end
53
+ def test_search_with_query_and_yml
54
+ config = YAML.load_file('test/letsshop.yml')['development']
55
+ lshop = LetsShopMapper::Connection::Base::new(config["server"], config["key"])
56
+ lshop.find({:q => "jeans"})
57
+ f = lshop.feed.entries[0].get_facets_by("universe")
58
+ assert_equal "mode", f[0].title
59
+ lshop.find({:q => "robe noir"})
60
+ f = lshop.feed.entries[0].get_facets_by("universe")
61
+ assert_equal "mode", f[0].title
62
+ assert_equal "UTF-8", lshop.feed.encoding
63
+ assert_equal "Search: robe AND noir", lshop.feed.title
64
+ assert_equal "0", lshop.feed.startindex
65
+ end
66
+ def test_search_facets_with_query
67
+ config = YAML.load_file('test/letsshop.yml')['development']
68
+ lshop = LetsShopMapper::Connection::Base::new(config["server"], config["key"])
69
+ lshop.find({:q => "robe"})
70
+ f = lshop.feed.get_facets_by("category")
71
+ assert_equal "robe", f[0].title
72
+ assert_equal "robe courte", f[1].title
73
+ end
74
+ def test_search_by_category
75
+ lshop = LetsShopMapper::Connection::Base::new("letsshop.dev.happun.com", "82842d494583280b940b208664f34014")
76
+ lshop.find({:c => "68446949-6-39989294", :start => 0, :nhits => 2})
77
+ assert_equal "UTF-8", lshop.feed.encoding
78
+ assert_equal "Search: ", lshop.feed.title
79
+ assert_equal "http://letsshop.dev.happun.com/search/82842d494583280b940b208664f34014", lshop.feed.link
80
+ f = lshop.feed.entries[0].get_facets_by("category")
81
+ assert_equal "chaussure", f[0].title
82
+ assert_equal "subset", f[0].role
83
+ f = lshop.feed.entries[0].get_facets_by("universe")
84
+ assert_equal "mode", f[0].title
85
+ end
86
+ def test_search_by_category_with_query
87
+ lshop = LetsShopMapper::Connection::Base::new("letsshop.dev.happun.com", "82842d494583280b940b208664f34014")
88
+ lshop.find({:c => "68446949-6-39989294", :q => "escarpin noir",:start => 0, :nhits => 2})
89
+ assert_equal "UTF-8", lshop.feed.encoding
90
+ assert_equal "Search: escarpin AND noir", lshop.feed.title
91
+ assert_equal "http://letsshop.dev.happun.com/search/82842d494583280b940b208664f34014", lshop.feed.link
92
+ f = lshop.feed.entries[0].get_facets_by("category")
93
+ assert_equal "chaussure", f[0].title
94
+ assert_equal "subset", f[0].role
95
+ f = lshop.feed.entries[0].get_facets_by("universe")
96
+ assert_equal "mode", f[0].title
97
+ end
98
+ def test_tree_parse_file
99
+ Dir.foreach(FIXTURESDIR) do |f|
100
+ next if f !~ /tree*.xml$/
101
+ puts "Checking #{f}"
102
+ str = File::read(FIXTURESDIR + '/' + f)
103
+ lshopTree = LetsShopMapper::Model::Tree::Tree::new(str)
104
+ assert_equal "Femme", lshopTree.categories.children[0].children[0].name
105
+ assert_equal "2", lshopTree.categories.children[0].children[0].id.split('-')[1]
106
+ assert_equal "Enfant", lshopTree.categories.children[0].children[2].name
107
+ assert_equal "4", lshopTree.categories.children[0].children[2].id.split('-')[1]
108
+ assert_equal "Manteaux", lshopTree.categories.children[0].children[2].children[5].name
109
+ assert_equal "4084", lshopTree.categories.children[0].children[2].children[5].id.split('-')[1]
110
+ assert_equal "Accessoires", lshopTree.categories.children[0].children[0].children[4].name
111
+ assert_equal "11", lshopTree.categories.children[0].children[0].children[4].id.split('-')[1]
112
+ end
113
+ end
114
+ def test_tree
115
+ config = YAML.load_file('test/letsshop.yml')['development']
116
+ lshop = LetsShopMapper::Connection::Base::new(config["server"], config["key"])
117
+ lshop.get_tree("68446949-6-39989294")
118
+ assert_equal "Escarpins", lshop.tree.categories.children[0].name
119
+ assert_equal "52", lshop.tree.categories.children[0].id.split('-')[1]
120
+ assert_equal "Sandales", lshop.tree.categories.children[1].name
121
+ assert_equal "53", lshop.tree.categories.children[1].id.split('-')[1]
122
+ lshop.get_tree(lshop.tree.categories.children[1].id)
123
+ assert_equal "Sandales a talon", lshop.tree.categories.children[1].name
124
+ assert_equal "3605", lshop.tree.categories.children[1].id.split('-')[1]
125
+ end
126
+ def test_search_special_character
127
+ lshop = LetsShopMapper::Connection::Base::new("letsshop.dev.happun.com", "82842d494583280b940b208664f34014")
128
+ lshop.find({:q => "levi's", :f => "refine:'universe:mode',refine:'gender:enfant',refine:'brand:levi's'", :start => 0, :nhits => 5})
129
+ assert_equal "UTF-8", lshop.feed.encoding
130
+ ff = lshop.feed.get_facets_by("brand")
131
+ assert_equal true, lshop.feed.facets[0].selected
132
+ f = lshop.feed.entries[0].get_facets_by("brand")
133
+ assert_equal "levi's", f[0].title
134
+ lshop.find({:f => "refine:'brand:levi's'", :start => 0, :nhits => 5})
135
+ f = lshop.feed.entries[0].get_facets_by("brand")
136
+ assert_equal "levi's", f[0].title
137
+ end
138
+ def test_search_product
139
+ lshop = LetsShopMapper::Connection::Base::new("letsshop.dev.happun.com", "82842d494583280b940b208664f34014")
140
+ lshop.find({:producs => "b67ab565e1e0f2661e08845111a2106f"})
141
+ assert_equal "UTF-8", lshop.feed.encoding
142
+ assert_equal "b67ab565e1e0f2661e08845111a2106f", lshop.feed.entries[0].id
143
+ assert_equal "Flirt - Collier en or 750 jaune et diamants", lshop.feed.entries[0].title
144
+ end
145
+ def test_tree_filters
146
+ Dir.foreach(FIXTURESDIR) do |f|
147
+ next if f !~ /tree*.xml$/
148
+ puts "Checking #{f}"
149
+ str = File::read(FIXTURESDIR + '/' + f)
150
+ lshopTree = LetsShopMapper::Model::Tree::Tree::new(str)
151
+ assert_equal "Femme", lshopTree.categories.children[0].children[0].name
152
+ assert_equal "2", lshopTree.categories.children[0].children[0].id.split('-')[1]
153
+ assert_equal "gender", lshopTree.categories.children[0].children[0].filters[0].key
154
+ assert_equal "femme", lshopTree.categories.children[0].children[0].filters[0].value
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'pp'
3
+ require 'letsshop_mapper'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letsshop_mapper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1481994153
5
+ prerelease: true
6
+ segments:
7
+ - 0
8
+ - 8
9
+ - 6beta
10
+ version: 0.8.6beta
11
+ platform: ruby
12
+ authors:
13
+ - happun
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-27 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Let's Shop Mapper is a Ruby library that allows to search and parse on Let's Shop Service.
23
+ email: dev@happun.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - LICENSE
31
+ files:
32
+ - README.rdoc
33
+ - LICENSE
34
+ - Rakefile
35
+ - init.rb
36
+ - lib/letsshop_mapper.rb
37
+ - lib/letsshop_mapper/version.rb
38
+ - lib/letsshop_mapper/patch/rexml.rb
39
+ - lib/letsshop_mapper/model/tree/tree.rb
40
+ - lib/letsshop_mapper/model/opensearch/feed.rb
41
+ - lib/letsshop_mapper/model/opensearch/entry.rb
42
+ - lib/letsshop_mapper/model/base/filter.rb
43
+ - lib/letsshop_mapper/model/base/facet.rb
44
+ - lib/letsshop_mapper/model/base/category.rb
45
+ - lib/letsshop_mapper/exceptions.rb
46
+ - lib/letsshop_mapper/connection.rb
47
+ - test/test_helper.rb
48
+ - test/letsshop_mapper_test.rb
49
+ - test/letsshop.yml
50
+ - rails/init.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/rgaidot/letsshop-mapper
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --line-numbers
58
+ - --inline-source
59
+ - README.rdoc
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">"
75
+ - !ruby/object:Gem::Version
76
+ hash: 25
77
+ segments:
78
+ - 1
79
+ - 3
80
+ - 1
81
+ version: 1.3.1
82
+ requirements: []
83
+
84
+ rubyforge_project: letsshop_mapper
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Let's Shop Mapper is a Ruby library that allows to search and parse on Let's Shop Service
89
+ test_files: []
90
+