coneco 0.0.1

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.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-09-22
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 FIXME full name
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README.txt
6
+ Rakefile
7
+ config/hoe.rb
8
+ config/requirements.rb
9
+ lib/coneco.rb
10
+ lib/coneco/version.rb
11
+ lib/coneco/client.rb
12
+ script/console
13
+ script/destroy
14
+ script/generate
15
+ script/txt2html
16
+ setup.rb
17
+ tasks/deployment.rake
18
+ tasks/environment.rake
19
+ tasks/website.rake
20
+ test/test_coneco.rb
21
+ test/test_helper.rb
22
+ website/index.html
23
+ website/index.txt
24
+ website/javascripts/rounded_corners_lite.inc.js
25
+ website/stylesheets/screen.css
26
+ website/template.html.erb
@@ -0,0 +1,7 @@
1
+
2
+ For more information on coneco, see http://coneco.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
@@ -0,0 +1,48 @@
1
+ = coneco
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2008 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,73 @@
1
+ require 'coneco/version'
2
+
3
+ AUTHOR = 'kilynn' # can also be an array of Authors
4
+ EMAIL = "kilynn atmark gmail.com"
5
+ DESCRIPTION = "Library for coneco.net webservice API"
6
+ GEM_NAME = 'coneco' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'coneco' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+ EXTRA_DEPENDENCIES = [
11
+ # ['activesupport', '>= 1.3.1']
12
+ ] # An array of rubygem dependencies [name, version]
13
+
14
+ @config_file = "~/.rubyforge/user-config.yml"
15
+ @config = nil
16
+ RUBYFORGE_USERNAME = "unknown"
17
+ def rubyforge_username
18
+ unless @config
19
+ begin
20
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
21
+ rescue
22
+ puts <<-EOS
23
+ ERROR: No rubyforge config file found: #{@config_file}
24
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
25
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
26
+ EOS
27
+ exit
28
+ end
29
+ end
30
+ RUBYFORGE_USERNAME.replace @config["username"]
31
+ end
32
+
33
+
34
+ REV = nil
35
+ # UNCOMMENT IF REQUIRED:
36
+ # REV = YAML.load(`svn info`)['Revision']
37
+ VERS = Coneco::VERSION::STRING + (REV ? ".#{REV}" : "")
38
+ RDOC_OPTS = ['--quiet', '--title', 'coneco documentation',
39
+ "--opname", "index.html",
40
+ "--line-numbers",
41
+ "--main", "README",
42
+ "--inline-source"]
43
+
44
+ class Hoe
45
+ def extra_deps
46
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
47
+ @extra_deps
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.developer(AUTHOR, EMAIL)
55
+ p.description = DESCRIPTION
56
+ p.summary = DESCRIPTION
57
+ p.url = HOMEPATH
58
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
59
+ p.test_globs = ["test/**/test_*.rb"]
60
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
61
+
62
+ # == Optional
63
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
64
+ #p.extra_deps = EXTRA_DEPENDENCIES
65
+
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ $hoe.rsync_args = '-av --delete --ignore-errors'
73
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'coneco/client.rb'
5
+
6
+ module Coneco
7
+
8
+ end
@@ -0,0 +1,268 @@
1
+ #
2
+ # = coneco_api_client.rb
3
+ #
4
+ # Ruby client for {coneco.net Web Services}[http://apidoc.coneco.net] API.
5
+ #
6
+ # This file is in the public domain.
7
+ # Original Created by Satoshi Nakagawa in 2007.9.11.
8
+ #
9
+ # == Overview
10
+ #
11
+ # coneco_api_client.rb is a library to access to Coneco.net Web Services.
12
+ #
13
+ # == How to use
14
+ #
15
+ # * apikey = 'xxxx'
16
+ # * c = Coneco::Client.new(apikey)
17
+ # * c.find_categories
18
+ # * c.find_products(nil, 'pc')
19
+ # * c.get_reviews(1040908007)
20
+ #
21
+
22
+ $KCODE = 'u'
23
+ require 'rubygems'
24
+ require 'net/http'
25
+ require 'cgi'
26
+ require 'json'
27
+
28
+ module Coneco
29
+
30
+ module InitHelper
31
+ def populate(h)
32
+ h.each do |k,v|
33
+ v = CGI.unescapeHTML(v) if String === v
34
+ instance_variable_set('@' + _to_ivar_name(k), v)
35
+ end
36
+ end
37
+
38
+ def _to_ivar_name(s)
39
+ s = s.gsub(/.[A-Z]/) {|c| c[0..0] + '_' + c[1..1].downcase }
40
+ s[0..0] = s[0..0].downcase
41
+ s
42
+ end
43
+ end
44
+
45
+ class Response
46
+ attr_reader :header, :body
47
+ def initialize(h, body)
48
+ @header = ResponseHeader.new(h['Header'])
49
+ @body = body
50
+ end
51
+ end
52
+
53
+ class ResponseHeader
54
+ include InitHelper
55
+ attr_reader :version, :count, :page, :page_count, :status
56
+ def initialize(h)
57
+ populate(h)
58
+ @page = Page.new(@page) if @page
59
+ end
60
+
61
+ def success?
62
+ @status == 'Success'
63
+ end
64
+ end
65
+
66
+ class Page
67
+ include InitHelper
68
+ attr_reader :size, :current, :count
69
+ def initialize(h)
70
+ populate(h)
71
+ end
72
+ end
73
+
74
+ class Category
75
+ include InitHelper
76
+ attr_accessor :level, :id, :name, :url
77
+ def initialize(h)
78
+ populate(h)
79
+ end
80
+ end
81
+
82
+ class Product
83
+ include InitHelper
84
+ attr_accessor :com_id, :name, :top_category, :middle_category, :category, :url, :image_url, :small_image_url,
85
+ :lowest_price, :highest_price, :average_price, :seller_count, :ranking, :manufacturer, :brand, :release_date,
86
+ :specifications, :description, :review,
87
+ :avg_ratings
88
+ def initialize(h)
89
+ populate(h)
90
+ @top_category = Category.new(@top_category) if @top_category
91
+ @middle_category = Category.new(@middle_category) if @middle_category
92
+ @category = Category.new(@category) if @category
93
+ @specifications.map! {|i| Specification.new(i) } if @specifications
94
+ @review = ReviewItem.new(@review) if @review
95
+ @avg_ratings = ReviewItem.new(@avg_ratings) if @avg_ratings
96
+ end
97
+ end
98
+
99
+ class Specification
100
+ include InitHelper
101
+ attr_accessor :title, :spec
102
+ def initialize(h)
103
+ populate(h)
104
+ end
105
+ end
106
+
107
+ class Review
108
+ include InitHelper
109
+ attr_accessor :item, :review
110
+ def initialize(h) #(item, review)
111
+ populate(h)
112
+ @item = Product.new(@item) if @item
113
+ @review = ReviewItem.new(@review) if @review
114
+ end
115
+ end
116
+
117
+ class ReviewItem
118
+ include InitHelper
119
+ attr_accessor :review_count, :review_list_url, :bookmark_count, :bookmark_user_list_url,
120
+ :overall_rating, :ratings,
121
+ :id, :user_id, :summary, :url, :author_url, :pros, :cons, :description,
122
+ :purchase, :created
123
+ def initialize(h)
124
+ populate(h)
125
+ @ratings.map! {|i| Rating.new(i) } if @ratings
126
+ @purchase = Purchase.new(@purchase) if @purchase
127
+ end
128
+ end
129
+
130
+ class Rating
131
+ include InitHelper
132
+ attr_accessor :title, :rating
133
+ def initialize(h)
134
+ populate(h)
135
+ end
136
+ end
137
+
138
+ class Purchase
139
+ include InitHelper
140
+ attr_accessor :price, :date
141
+ def initialize(h)
142
+ populate(h)
143
+ end
144
+ end
145
+
146
+ class Client
147
+
148
+ FIND_CATEGORIES_URL = 'http://api.coneco.net/cws/v1/SearchCategories_json'
149
+ FIND_PRODUCTS_URL = 'http://api.coneco.net/cws/v1/SearchProducts_json'
150
+ FIND_REVIEWS_URL = 'http://api.coneco.net/cws/v1/SearchReviews_json'
151
+
152
+ # coneco.net Web Services用のAPIキーをセットします。
153
+ #
154
+ def initialize(key, timeout=30)
155
+ @key = key
156
+ @timeout = timeout
157
+ end
158
+
159
+ # カテゴリ検索APIを実行。parent_categoryを入力することで、さらに下層のカテゴリが検索できます。
160
+ #
161
+ def find_categories(parent_category=0)
162
+ s = get(FIND_CATEGORIES_URL, { :apikey => @key, :categoryId => parent_category })
163
+ Response.new(s, parse_categories(s))
164
+ end
165
+
166
+ # 商品検索APIを実行。Hash形式で検索条件の指定して絞込みを行う。
167
+ #
168
+ def find_products_option(option)
169
+ option[:apikey] = @key
170
+ s = get(FIND_PRODUCTS_URL, option)
171
+ Response.new(s, parse_products(s))
172
+ end
173
+
174
+ # 商品検索APIを実行。引数に検索条件を指定して絞込みを行う。
175
+ #
176
+ def find_products(categoryId, comId=nil, keyword=nil, orFlag=nil, maker=nil, brand=nil, available=nil,
177
+ highestPrice=nil, lowestPrice=nil, page=nil, count=nil, sort=nil)
178
+ h = { :apikey => @key }
179
+ h[:categoryId] = categoryId if categoryId
180
+ h[:comId] = comId if comId
181
+ h[:keyword] = keyword if keyword
182
+ h[:orFlag] = orFlag if orFlag
183
+ h[:maker] = maker if maker
184
+ h[:brand] = brand if brand
185
+ h[:available] = available if available
186
+ h[:highestPrice] = highestPrice if highestPrice
187
+ h[:lowestPrice] = lowestPrice if lowestPrice
188
+ h[:page] = page if page
189
+ h[:count] = count if count
190
+ h[:sort] = sort if sort
191
+ s = get(FIND_PRODUCTS_URL, h)
192
+ Response.new(s, parse_products(s))
193
+ end
194
+
195
+ # レビュー検索APIを実行。Hash形式で検索条件の指定を行う。
196
+ #
197
+ def find_reviews_option(option)
198
+ option[:apikey] = @key
199
+ s = get(FIND_REVIEWS_URL, h)
200
+ Respose.new(s, parse_reviews(s))
201
+ end
202
+
203
+ # レビュー検索APIを実行。引数に検索条件を指定して絞り込みを行う。
204
+ #
205
+ def find_reviews(category, comId=nil, keyword=nil, orFlag=nil, userId=nil, page=nil, count=nil, sort=nil)
206
+ h = { :apikey => @key }
207
+ h[:comId] = comId if comId
208
+ h[:categoryId] = category if category
209
+ h[:keyword] = keyword if keyword
210
+ h[:orFlag] = orFlag if orFlag
211
+ h[:userId] = userId if userId
212
+ h[:page] = page if page
213
+ h[:count] = count if count
214
+ h[:sort] = sort if sort
215
+ s = get(FIND_REVIEWS_URL, h)
216
+ Response.new(s, parse_reviews(s))
217
+ end
218
+
219
+
220
+ private
221
+
222
+ def parse_categories(s)
223
+ ary = s['Category']
224
+ if ary
225
+ ary.map {|i| Category.new(i) }
226
+ else
227
+ nil
228
+ end
229
+ end
230
+
231
+ def parse_products(s)
232
+ ary = s['Item']
233
+ if ary
234
+ ary.map {|i| Product.new(i) }
235
+ else
236
+ nil
237
+ end
238
+ end
239
+
240
+ def parse_reviews(s)
241
+ ary = s['ItemInfo']
242
+ if ary
243
+ ary.map {|i| Review.new(i)}
244
+ else
245
+ nil
246
+ end
247
+ end
248
+
249
+ def get(url, params)
250
+ uri = URI.parse(url)
251
+ path = uri.path
252
+ q = params.inject("?") {|s,i| s << "#{i[0].to_s}=#{CGI.escape(i[1].to_s)}&" }.chop
253
+ path << q if q.size > 0
254
+
255
+ Net::HTTP.start(uri.host, uri.port) do |c|
256
+ c.read_timeout = @timeout
257
+ req = Net::HTTP::Get.new(path)
258
+ res = c.request(req)
259
+ parse_result(res)
260
+ end
261
+ end
262
+
263
+ def parse_result(res)
264
+ JSON.parse(res.body)
265
+ end
266
+ end
267
+
268
+ end