rakuten_api 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -8,7 +8,7 @@ require 'rakuten_api'
8
8
  require 'faraday'
9
9
 
10
10
  RakutenApi.configure do |config|
11
- config.application_id = "[Your application_id]"
11
+ config.application_id = "[Your Application Id]"
12
12
  end
13
13
 
14
14
  client = RakutenApi::ItemRanking::Client.new
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
- require "rakuten_api/genre_search/model"
44
- require "rakuten_api/item_search/model"
45
- require "rakuten_api/item_ranking/model"
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
@@ -1,3 +1,3 @@
1
1
  module RakutenApi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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