jekyll-amazon 0.2.2 → 0.3.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: d26cfe071a4a29ceda2c35f3325a9ff459381d22
4
- data.tar.gz: c46e953d7f3bd946faaa56da74efb154d28d1ee3
3
+ metadata.gz: 7f66481069447c54da0a8c885fa980eb19ec03f7
4
+ data.tar.gz: 7345bc5b5ab2176870692f8d78f0da8dfb44f019
5
5
  SHA512:
6
- metadata.gz: 8db8af2c368a3d2883d85be286d16c38a81f3b2969e476bb0a34b310353d964a45b1227cc636a61921ab9da1942fba499510eec1a0265132d704455bbaa30678
7
- data.tar.gz: 37815a9067a508d78526ce91f7b2402e1d20395ff71e5ab83c57b646b152b2edaf01b46aabb25ee2fc328d8dfd62c5b20904ac27350d70e35112556916317d72
6
+ metadata.gz: 8e0f1df60df9f6f21fac5926c57d9a45a680dc941ff6c44c9b0b935af6d528f5bbb7cbb8ce41a048bcfee5ee519145019c813e79d7e705f73c40ca252bbd2c53
7
+ data.tar.gz: f4d3fe974de53b5b75d7e961a9ee23848d36933d39c128ab656a427dca950057f3651c43ad36822a1a5af539e22508f9036d0a5ff9f7d47720d10cd5d7c992c0
data/README.md CHANGED
@@ -26,12 +26,6 @@ And set the environment variables required by the Product Advertising API:
26
26
 
27
27
  (`ECS_ASSOCIATE_TAG` is the Tracking ID for your Affiliate account; `AWS_ACCESS_KEY_ID` and `AWS_SECRET_KEY` are your security credentials from your AWS account. An AWS account is required for the use of this API.)
28
28
 
29
- Users outside of Japan will also want to set
30
-
31
- $ export ECS_COUNTRY=...
32
-
33
- with the two character country code of their Amazon Affiliate account (e.g. `'us'` for United States).
34
-
35
29
  Finally, add the following to your site's `_config.yml`:
36
30
 
37
31
  ```
@@ -39,23 +33,34 @@ gems:
39
33
  - jekyll-amazon
40
34
  ```
41
35
 
42
- and if English labels are desired,
36
+ ## Configuration
37
+
38
+ Put the configuration in your _config.yml:
43
39
 
44
40
  ```
45
- amazon_locale : en
41
+ jekyll-amazon:
42
+ locale: 'ja' # label's language. default: 'en'
43
+ country: 'jp' # with the two character country code of their Amazon
44
+ # Affiliate account (e.g. `'us'` for United States).
45
+ # default: 'us'
46
+ # 'br'/'ca'/'cn'/'de'/'es'/'fr'/'in'/'it'/'jp'/'mx/'uk'/'us'
47
+ template_dir: '_templates' # original template directory
46
48
  ```
47
49
 
50
+
48
51
  ## Usage
49
52
 
50
53
  Use the tag as follows in your jekyll pages, posts and collections:
51
54
 
52
-
53
55
  ```liquid
54
56
  {% amazon 4083210443 detail %}
55
57
  ```
56
58
 
57
59
 
60
+ ## References
58
61
 
62
+ - [Amazon Product Advertising API](https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html).
63
+ - [jugend/amazon\-ecs: Ruby Amazon Product Advertising API](https://github.com/jugend/amazon-ecs)
59
64
 
60
65
  ## Contributing
61
66
 
@@ -2,6 +2,7 @@
2
2
  require 'amazon/ecs'
3
3
  require 'singleton'
4
4
  require 'i18n'
5
+ require 'erb'
5
6
 
6
7
  module Jekyll
7
8
  module Amazon
@@ -26,40 +27,20 @@ module Jekyll
26
27
  description: 'EditorialReviews/EditorialReview/Content'
27
28
  }.freeze
28
29
 
29
- ECS_ASSOCIATE_TAG = ENV['ECS_ASSOCIATE_TAG'] || ''
30
- AWS_ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY_ID'] || ''
31
- AWS_SECRET_KEY = ENV['AWS_SECRET_KEY'] || ''
32
-
33
- raise 'AWS_ACCESS_KEY_ID env variable is not set' if AWS_ACCESS_KEY_ID.empty?
34
- raise 'AWS_SECRET_KEY env variable is not set' if AWS_SECRET_KEY.empty?
35
- raise 'ECS_ASSOCIATE_TAG env variable is not set' if ECS_ASSOCIATE_TAG.empty?
36
-
37
30
  def initialize
38
31
  @result_cache = {}
39
32
  FileUtils.mkdir_p(CACHE_DIR)
40
33
  end
41
34
 
42
- def setup(context)
43
- site = context.registers[:site]
44
- # ::Amazon::Ecs.debug = true
35
+ def setup(country)
36
+ ::Amazon::Ecs.debug = debug?
45
37
  ::Amazon::Ecs.configure do |options|
46
- options[:associate_tag] = ECS_ASSOCIATE_TAG
47
- options[:AWS_access_key_id] = AWS_ACCESS_KEY_ID
48
- options[:AWS_secret_key] = AWS_SECRET_KEY
38
+ options[:associate_tag] = ENV.fetch('ECS_ASSOCIATE_TAG')
39
+ options[:AWS_access_key_id] = ENV.fetch('AWS_ACCESS_KEY_ID')
40
+ options[:AWS_secret_key] = ENV.fetch('AWS_SECRET_KEY')
49
41
  options[:response_group] = RESPONSE_GROUP
50
- options[:country] = ENV['ECS_COUNTRY'] || 'jp'
42
+ options[:country] = country
51
43
  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"]
63
44
  end
64
45
 
65
46
  def item_lookup(asin)
@@ -70,13 +51,21 @@ module Jekyll
70
51
  res.first_item
71
52
  end
72
53
  raise ArgumentError unless item
54
+ save(asin, item)
55
+ end
56
+
57
+ private
58
+
59
+ def save(asin, item)
73
60
  data = create_data(item)
74
61
  write_cache(asin, data)
75
62
  @result_cache[asin] = data
76
63
  @result_cache[asin]
77
64
  end
78
65
 
79
- private
66
+ def debug?
67
+ ENV.fetch('JEKYLL_AMAZON_DEBUG', 'false') == 'true'
68
+ end
80
69
 
81
70
  def read_cache(asin)
82
71
  path = File.join(CACHE_DIR, asin)
@@ -107,94 +96,55 @@ module Jekyll
107
96
  end
108
97
  end
109
98
 
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
-
132
99
  class AmazonTag < Liquid::Tag
133
- attr_accessor :asin
134
- attr_accessor :template_type
100
+ DEFAULT_LOCALE = 'en'.freeze
101
+ DEFAULT_COUNTRY = 'us'.freeze
135
102
 
136
103
  def initialize(tag_name, markup, tokens)
137
104
  super
138
105
  parse_options(markup)
139
- error "No ASIN given in #{tag_name} tag" if asin.nil? || asin.empty?
106
+ error "No ASIN given in #{tag_name} tag" if @asin.nil? || @asin.empty?
140
107
  end
141
108
 
142
109
  def render(context)
143
- type = template_type || :detail
144
- AmazonResultCache.instance.setup(context)
145
- item = AmazonResultCache.instance.item_lookup(asin.to_s)
110
+ setup(context)
111
+ setup_i18n
112
+ item = AmazonResultCache.instance.item_lookup(@asin.to_s)
146
113
  return unless item
147
- send(type, item)
114
+ render_from_file(@template_type, item)
148
115
  end
149
116
 
150
117
  private
151
118
 
119
+ def setup(context)
120
+ @site = context.registers[:site]
121
+ @config = @site.config['jekyll-amazon'] || {}
122
+ country = @config['country'] || DEFAULT_COUNTRY
123
+ @template_dir = @config['template_dir']
124
+ AmazonResultCache.instance.setup(country)
125
+ end
126
+
127
+ def setup_i18n
128
+ locale = @config['locale'] || DEFAULT_LOCALE
129
+ I18n.enforce_available_locales = false
130
+ I18n.locale = locale.to_sym
131
+ file = File.expand_path("../../locales/#{locale}.yml", __dir__)
132
+ I18n.load_path = [file]
133
+ end
134
+
152
135
  def parse_options(markup)
153
136
  options = (markup || '').split(' ').map(&:strip)
154
- self.asin = options.shift
155
- self.template_type = options.shift || :title
156
- end
157
-
158
- def title(item)
159
- url = item[:detail_page_url]
160
- title = item[:title]
161
- format(
162
- %(<a class="jk-amazon-info-title" href="%s" target="_blank">%s</a>),
163
- url, title
164
- )
165
- end
166
-
167
- def image(item)
168
- url = item[:detail_page_url]
169
- title = item[:title]
170
- image_url = item[:medium_image_url]
171
- str = <<-"EOS"
172
- <a class="jk-amazon-image" href="#{url}" target="_blank">
173
- <img src="#{image_url}" alt="#{title}" />
174
- </a>
175
- EOS
176
- str.to_s
177
- end
178
-
179
- def detail(item)
180
- attrs = {
181
- author: item[:author],
182
- publisher: item[:publisher],
183
- date: item[:publication_date] || item[:release_date],
184
- salesrank: item[:salesrank],
185
- description: br2nl(item[:description])
186
- }.map { |k, v| ItemAttribute.new(k, v).to_html }.join("\n")
187
-
188
- str = <<-"EOS"
189
- <div class="jk-amazon-item">
190
- #{image(item)}
191
- <div class="jk-amazon-info">
192
- #{title(item)}
193
- #{attrs}
194
- </div>
195
- </div>
196
- EOS
197
- str.to_s
137
+ @asin = options.shift
138
+ @template_type = options.shift || :title
139
+ end
140
+
141
+ def render_from_file(type, item)
142
+ if @template_dir
143
+ template_file = File.expand_path("#{type}.erb", @template_dir)
144
+ file = template_file if File.exist? template_file
145
+ end
146
+ file ||= File.expand_path("../../templates/#{type}.erb", __dir__)
147
+ ERB.new(open(file).read).result(binding)
198
148
  end
199
149
 
200
150
  def br2nl(text)
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Amazon
3
- VERSION = '0.2.2'.freeze
3
+ VERSION = '0.3.0'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,27 @@
1
+ <div class="jk-amazon-item">
2
+ <a class="jk-amazon-image" href="<%= item[:detail_page_url] %>" target="_blank">
3
+ <img src="<%= item[:medium_image_url] %>" alt="<%= item[:title] %>" />
4
+ </a>
5
+ <div class="jk-amazon-info">
6
+ <a class="jk-amazon-info-title" href="<%= item[:detail_page_url] %>" target="_blank">
7
+ <%= item[:title] %>
8
+ </a>
9
+ <%
10
+ {
11
+ author: item[:author],
12
+ publisher: item[:publisher],
13
+ date: item[:publication_date] || item[:release_date],
14
+ salesrank: item[:salesrank],
15
+ description: br2nl(item[:description])
16
+ }.each do |key, value|
17
+ next if value.nil? || value.empty?
18
+ %>
19
+ <div class="jk-amazon-info-<%= key %>">
20
+ <span class="jk-amazon-info-label">
21
+ <%= I18n.t(key) %>
22
+ </span>
23
+ <%= value %>
24
+ </div>
25
+ <% end %>
26
+ </div>
27
+ </div>
@@ -0,0 +1,3 @@
1
+ <a class="jk-amazon-image" href="<%= item[:detail_page_url] %>" target="_blank">
2
+ <img src="<%= item[:medium_image_url] %>" alt="<%= item[:title] %>" />
3
+ </a>
@@ -0,0 +1,3 @@
1
+ <a class="jk-amazon-info-title" href="<%= item[:detail_page_url] %>" target="_blank">
2
+ <%= item[:title] %>
3
+ </a>
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.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tokzk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-01 00:00:00.000000000 Z
11
+ date: 2017-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazon-ecs
@@ -116,6 +116,9 @@ files:
116
116
  - lib/jekyll-amazon/version.rb
117
117
  - locales/en.yml
118
118
  - locales/ja.yml
119
+ - templates/detail.erb
120
+ - templates/image.erb
121
+ - templates/title.erb
119
122
  homepage: https://github.com/tokzk/jekyll-amazon
120
123
  licenses:
121
124
  - MIT