rakuten_api 0.1.0 → 0.1.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.
- data/examples/ranking_csv.rb +54 -0
- data/examples/show_ranking.rb +1 -1
- data/lib/rakuten_api.rb +18 -11
- data/lib/rakuten_api/base/response.rb +3 -2
- data/lib/rakuten_api/version.rb +1 -1
- metadata +2 -1
@@ -0,0 +1,54 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# 楽天ランキングAPIを利用し、ランキングデータをCSVとして保存するサンプル
|
3
|
+
# config.item_ranking_module = 'MyCSV' の使い方を説明したかっただけ
|
4
|
+
|
5
|
+
require 'rakuten_api'
|
6
|
+
require 'faraday'
|
7
|
+
require 'csv'
|
8
|
+
|
9
|
+
module MyCSV
|
10
|
+
CSV_KEYS = {
|
11
|
+
rank: "ランク",
|
12
|
+
shop_name: "店舗名",
|
13
|
+
shop_url: "店舗URL",
|
14
|
+
item_name: "商品名",
|
15
|
+
catchcopy: "キャッチコピー",
|
16
|
+
item_price: "価格",
|
17
|
+
medium_image_urls: "画像",
|
18
|
+
tax_flag: "消費税",
|
19
|
+
postage_flag: "送料",
|
20
|
+
review_count: "レビュー数",
|
21
|
+
review_average: "平均レビュー",
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
def to_a
|
25
|
+
row = []
|
26
|
+
h = to_hash
|
27
|
+
CSV_KEYS.keys.each do |key|
|
28
|
+
row << h[key]
|
29
|
+
end
|
30
|
+
row.map do |o|
|
31
|
+
o.is_a?(Array) ? o.first : o
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
RakutenApi.configure do |config|
|
37
|
+
config.application_id = "[Your Application Id]"
|
38
|
+
config.item_ranking_module = 'MyCSV'
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
client = RakutenApi::ItemRanking::Client.new
|
43
|
+
response = client.request
|
44
|
+
|
45
|
+
CSV.open(response.last_build_date.strftime('%Y%m%d%H%M') + '.csv', "wb", { col_sep: ',', row_sep: "\n", force_quotes: true }) do |csv|
|
46
|
+
csv << RakutenApi::ItemRanking::Model::CSV_KEYS.values
|
47
|
+
begin
|
48
|
+
response.simple_mapping.each do |f|
|
49
|
+
csv << f.to_a
|
50
|
+
end
|
51
|
+
sleep 1
|
52
|
+
response = response.next_ranking
|
53
|
+
end while response.success?
|
54
|
+
end
|
data/examples/show_ranking.rb
CHANGED
data/lib/rakuten_api.rb
CHANGED
@@ -6,14 +6,7 @@ require "rakuten_api/base/client"
|
|
6
6
|
require "rakuten_api/base/params"
|
7
7
|
require "rakuten_api/base/response"
|
8
8
|
require "rakuten_api/base/item"
|
9
|
-
|
10
9
|
require "rakuten_api/error"
|
11
|
-
require "rakuten_api/item_search/client"
|
12
|
-
require "rakuten_api/item_search/response"
|
13
|
-
require "rakuten_api/genre_search/client"
|
14
|
-
require "rakuten_api/genre_search/response"
|
15
|
-
require "rakuten_api/item_ranking/client"
|
16
|
-
require "rakuten_api/item_ranking/response"
|
17
10
|
|
18
11
|
module RakutenApi
|
19
12
|
APPLICATION_END_POINT = "https://app.rakuten.co.jp/"
|
@@ -38,8 +31,22 @@ module RakutenApi
|
|
38
31
|
constant
|
39
32
|
end
|
40
33
|
end
|
41
|
-
end
|
42
34
|
|
43
|
-
|
44
|
-
|
45
|
-
|
35
|
+
module ItemSearch
|
36
|
+
autoload :Client, 'rakuten_api/item_search/client'
|
37
|
+
autoload :Response, 'rakuten_api/item_search/response'
|
38
|
+
autoload :Model, 'rakuten_api/item_search/model'
|
39
|
+
end
|
40
|
+
|
41
|
+
module ItemRanking
|
42
|
+
autoload :Client, 'rakuten_api/item_ranking/client'
|
43
|
+
autoload :Response, 'rakuten_api/item_ranking/response'
|
44
|
+
autoload :Model, 'rakuten_api/item_ranking/model'
|
45
|
+
end
|
46
|
+
|
47
|
+
module GenreSearch
|
48
|
+
autoload :Client, 'rakuten_api/genre_search/client'
|
49
|
+
autoload :Response, 'rakuten_api/genre_search/response'
|
50
|
+
autoload :Model, 'rakuten_api/genre_search/model'
|
51
|
+
end
|
52
|
+
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
module RakutenApi::Base
|
4
4
|
class Response
|
5
|
+
require 'json'
|
5
6
|
attr_reader :status
|
6
7
|
attr_reader :body
|
7
8
|
|
@@ -30,8 +31,8 @@ module RakutenApi::Base
|
|
30
31
|
protected
|
31
32
|
|
32
33
|
def json_parse(data)
|
33
|
-
JSON.parse(data)
|
34
|
-
rescue JSON::ParserError
|
34
|
+
::JSON.parse(data)
|
35
|
+
rescue ::JSON::ParserError
|
35
36
|
# @todo logger
|
36
37
|
{}
|
37
38
|
end
|
data/lib/rakuten_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rakuten_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- LICENSE.txt
|
89
89
|
- README.md
|
90
90
|
- Rakefile
|
91
|
+
- examples/ranking_csv.rb
|
91
92
|
- examples/show_ranking.rb
|
92
93
|
- lib/rakuten_api.rb
|
93
94
|
- lib/rakuten_api/base/client.rb
|