jekyll-amazon 0.1.3 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c962cc8ef8e8ac44d9eb1c776b34f558b1d0ceae
4
- data.tar.gz: 17752eb9437ac70c3cf2c980e48bf28a0f6c5162
3
+ metadata.gz: da83b3be2c74bd71cdba9a72ab120695ba7f11b1
4
+ data.tar.gz: da0ba626962a763761bd7c7644339a856a30604f
5
5
  SHA512:
6
- metadata.gz: 795069dc05db49cb586084461b2df334df1fe3de749e6b99aebac4465b2f7a913bba5540a191e3f886ff1e6ced624ac5efdb6c4eb3bac7afc4b3097f0b296ec5
7
- data.tar.gz: ce5b15b49491eed261950faa98f7432a0d1bf7a638bc361e39594088baa81311fc067c3cdb3233734681b9cc876393200b367c8c16ed1eb7f8ab25dd6cdc8e59
6
+ metadata.gz: fd197cf4998c8a36d9872016e1e666ac53fdc02e2dd987d4c7e26457b491d50c2a0312cf45cc69907f4a41374c86336d09c6563619be2586539318d3dacaba4f
7
+ data.tar.gz: 1c77ad77a27cc3eb6436ebbe97e8c4e596338f3f0971ce885caa3df5bd5a21c6c79f83107d2aad05cd24ba7396bd5cabb58d3b52c7213b18c74f5e07536a02e9
@@ -0,0 +1,23 @@
1
+ .jk-amazon-item {
2
+ margin-bottom: 20px;
3
+ .jk-amazon-image {
4
+ float: left;
5
+ min-height: 1px;
6
+ width: 130px;
7
+ }
8
+ .jk-amazon-info {
9
+ margin-left: 130px;
10
+ min-height: 1px;
11
+ .jk-amazon-info-title {
12
+ padding-bottom: 10px;
13
+ }
14
+ }
15
+ &:before, &:after {
16
+ display: table;
17
+ content: " ";
18
+ }
19
+ &:after {
20
+ display: block;
21
+ clear: both;
22
+ }
23
+ }
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.license = "MIT"
20
20
 
21
21
  spec.add_runtime_dependency "amazon-ecs", "~> 2.4.0"
22
+ spec.add_runtime_dependency "i18n"
22
23
  spec.add_development_dependency "bundler", "~> 1.11"
23
24
  spec.add_development_dependency "rake"
24
25
  spec.add_development_dependency "rspec"
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
  require 'amazon/ecs'
3
3
  require 'singleton'
4
+ require 'i18n'
4
5
 
5
6
  module Jekyll
6
7
  module Amazon
@@ -8,6 +9,7 @@ module Jekyll
8
9
  include Singleton
9
10
 
10
11
  CACHE_DIR = '.amazon-cache/'.freeze
12
+ RESPONSE_GROUP = 'SalesRank,Images,ItemAttributes,EditorialReview'.freeze
11
13
 
12
14
  ITEM_HASH = {
13
15
  asin: 'ASIN',
@@ -20,11 +22,12 @@ module Jekyll
20
22
  detail_page_url: 'DetailPageURL',
21
23
  small_image_url: 'SmallImage/URL',
22
24
  medium_image_url: 'MediumImage/URL',
23
- large_image_url: 'LargeImage/URL'
25
+ large_image_url: 'LargeImage/URL',
26
+ description: 'EditorialReviews/EditorialReview/Content'
24
27
  }.freeze
25
28
 
26
29
  ECS_ASSOCIATE_TAG = ENV['ECS_ASSOCIATE_TAG'] || ''
27
- AWS_ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY'] || ''
30
+ AWS_ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY_ID'] || ''
28
31
  AWS_SECRET_KEY = ENV['AWS_SECRET_KEY'] || ''
29
32
 
30
33
  raise 'AWS_ACCESS_KEY_ID env variable is not set' if AWS_ACCESS_KEY_ID.empty?
@@ -37,29 +40,40 @@ module Jekyll
37
40
  end
38
41
 
39
42
  def setup(context)
40
- context.registers[:site]
43
+ site = context.registers[:site]
41
44
  # ::Amazon::Ecs.debug = true
42
45
  ::Amazon::Ecs.configure do |options|
43
46
  options[:associate_tag] = ECS_ASSOCIATE_TAG
44
47
  options[:AWS_access_key_id] = AWS_ACCESS_KEY_ID
45
48
  options[:AWS_secret_key] = AWS_SECRET_KEY
46
- options[:response_group] = 'SalesRank,Images,ItemAttributes'
49
+ options[:response_group] = RESPONSE_GROUP
47
50
  options[:country] = ENV['ECS_COUNTRY'] || 'jp'
48
51
  end
52
+
53
+ setup_i18n(site)
54
+ end
55
+
56
+ def setup_i18n(site)
57
+ locale = 'ja'
58
+ locale = site.config['amazon_locale'] if site.config['amazon_locale']
59
+ I18n.enforce_available_locales = false
60
+ I18n.locale = locale.to_sym
61
+ dir = File.expand_path(File.dirname(__FILE__))
62
+ I18n.load_path = [dir + "/../../locales/#{locale}.yml"]
49
63
  end
50
64
 
51
65
  def item_lookup(asin)
52
66
  return @result_cache[asin] if @result_cache.key?(asin)
53
67
  return read_cache(asin) if read_cache(asin)
54
-
55
- retry_api do
68
+ item = retry_api do
56
69
  res = ::Amazon::Ecs.item_lookup(asin)
57
- item = res.first_item
58
- data = create_data(item)
59
- write_cache(asin, data)
60
- @result_cache[asin] = data
61
- @result_cache[asin]
70
+ res.first_item
62
71
  end
72
+ raise ArgumentError unless item
73
+ data = create_data(item)
74
+ write_cache(asin, data)
75
+ @result_cache[asin] = data
76
+ @result_cache[asin]
63
77
  end
64
78
 
65
79
  private
@@ -86,12 +100,35 @@ module Jekyll
86
100
  end
87
101
 
88
102
  def create_data(item)
103
+ return unless item
89
104
  ITEM_HASH.each_with_object({}) do |(key, value), hash|
90
105
  hash[key] = item.get(value).to_s
91
106
  end
92
107
  end
93
108
  end
94
109
 
110
+ class ItemAttribute
111
+ attr_accessor :key
112
+ attr_accessor :value
113
+
114
+ def initialize(key, value)
115
+ self.key = key
116
+ self.value = value
117
+ end
118
+
119
+ def to_html
120
+ str = "<div class=\"jk-amazon-info-#{key}\">"
121
+ str += labeled
122
+ str += '</div>'
123
+ str
124
+ end
125
+
126
+ def labeled
127
+ return '' if value.nil? || value.empty?
128
+ "<span class=\"jk-amazon-info-label\">#{I18n.t(key)} </span>#{value}"
129
+ end
130
+ end
131
+
95
132
  class AmazonTag < Liquid::Tag
96
133
  attr_accessor :asin
97
134
  attr_accessor :template_type
@@ -139,10 +176,14 @@ module Jekyll
139
176
  end
140
177
 
141
178
  def detail(item)
142
- author = item[:author]
143
- publisher = item[:publisher]
144
- date = item[:publication_date] || item[:release_date]
145
- salesrank = item[:salesrank]
179
+ attrs = {
180
+ author: item[:author],
181
+ publisher: item[:publisher],
182
+ date: item[:publication_date] || item[:release_date],
183
+ salesrank: item[:salesrank],
184
+ description: br2nl(item[:description])
185
+ }.map { |k, v| ItemAttribute.new(k, v).to_html }.join("\n")
186
+
146
187
  str = <<-"EOS"
147
188
  <div class="jk-amazon-item">
148
189
  <div class="jk-amazon-image">
@@ -152,27 +193,15 @@ module Jekyll
152
193
  <div class="jk-amazon-info-title">
153
194
  #{title(item)}
154
195
  </div>
155
- <div class="jk-amazon-info-author">
156
- #{labeled("Author: ", author)}
157
- </div>
158
- <div class="jk-amazon-info-publisher">
159
- #{labeled("Publisher: ", publisher)}
160
- </div>
161
- <div class="jk-amazon-info-date">
162
- #{labeled("Date: ", date)}
163
- </div>
164
- <div class="jk-amazon-info-salesrank">
165
- #{labeled("Sales Rank: ", salesrank)}
166
- </div>
196
+ #{attrs}
167
197
  </div>
168
198
  </div>
169
- EOS
199
+ EOS
170
200
  str.to_s
171
201
  end
172
202
 
173
- def labeled(label, value)
174
- return "" if value.nil? || value.empty?
175
- "<span class=\"amazon-info-label\">#{label} </span>#{value}"
203
+ def br2nl(text)
204
+ text.gsub(%r{&lt;br\s*/?&gt;}, "\n") unless text.nil?
176
205
  end
177
206
  end
178
207
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Amazon
3
- VERSION = '0.1.3'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
data/locales/en.yml ADDED
@@ -0,0 +1,6 @@
1
+ en:
2
+ author: 'Author: '
3
+ publisher: 'Publisher: '
4
+ date: 'Date: '
5
+ salesrank: 'Sales Rank: '
6
+ description: 'Description: '
data/locales/ja.yml ADDED
@@ -0,0 +1,6 @@
1
+ ja:
2
+ author: '作者: '
3
+ publisher: '出版社/メーカー: '
4
+ date: '発売日: '
5
+ salesrank: '売上順位: '
6
+ description: '概要: '
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-amazon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tokzk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-19 00:00:00.000000000 Z
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazon-ecs
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -93,12 +107,15 @@ files:
93
107
  - Gemfile
94
108
  - README.md
95
109
  - Rakefile
110
+ - _sass/jk_amazon.scss
96
111
  - bin/console
97
112
  - bin/setup
98
113
  - jekyll-amazon.gemspec
99
114
  - lib/jekyll-amazon.rb
100
115
  - lib/jekyll-amazon/amazon_tag.rb
101
116
  - lib/jekyll-amazon/version.rb
117
+ - locales/en.yml
118
+ - locales/ja.yml
102
119
  homepage: https://github.com/tokzk/jekyll-amazon
103
120
  licenses:
104
121
  - MIT