jekyll-amazon 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jekyll-amazon.gemspec +26 -0
- data/lib/jekyll-amazon/amazon_tag.rb +174 -0
- data/lib/jekyll-amazon/version.rb +5 -0
- data/lib/jekyll-amazon.rb +7 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 72c45367933abdb3238152c3a39b71731365c8e4
|
4
|
+
data.tar.gz: 1641e8404d810292230f42c81145f8a10fa88b8b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3b7c025dba826e7e76546bf636640c34bd39a30f8d42c4854cd93baf35e0b2bbc9f2afcf1004cf0c0ef312b4ed9c4dd2eaba06acfadb681dbf0337682abcfd1
|
7
|
+
data.tar.gz: cbc1e9fa948d35dd162ca3bb6d0b0ac1f1086c3e5e4c376ab0d7a0695fc830a2d1718de5aead19f5478ba921df0880924c69114a64d0525b87b42dcf477794a3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Jekyll::Amazon
|
2
|
+
|
3
|
+
Liquid tag for display Amazon Associate Links in Jekyll sites: `{% amazon %}`.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'jekyll-amazon'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install jekyll-amazon
|
20
|
+
|
21
|
+
And set the environment varables of the amazon ecs:
|
22
|
+
|
23
|
+
$ export ECS_ASSOCIATE_TAG=...
|
24
|
+
$ export AWS_ACCESS_KEY_ID=...
|
25
|
+
$ export AWS_SECRET_KEY...
|
26
|
+
|
27
|
+
|
28
|
+
Finally, add the following to your site's `_config.yml`:
|
29
|
+
|
30
|
+
```
|
31
|
+
gems:
|
32
|
+
- jekyll-amazon
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
Use the tag as follows in your jekyll pages, posts and collections:
|
38
|
+
|
39
|
+
|
40
|
+
```liquid
|
41
|
+
{% amazon 4083210443 detail %}
|
42
|
+
```
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tokzk/jekyll-amazon.
|
47
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jekyll/amazon"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jekyll-amazon/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jekyll-amazon"
|
8
|
+
spec.version = Jekyll::Amazon::VERSION
|
9
|
+
spec.authors = ["tokzk"]
|
10
|
+
spec.email = ["t@okzk.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Liquid tag for display Amazon associate links in Jekyll sites.}
|
13
|
+
spec.homepage = "https://github.com/tokzk/jekyll-amazon"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "amazon-ecs", "~> 2.4.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "jekyll", ">= 3.0"
|
26
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'amazon/ecs'
|
3
|
+
require 'singleton'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
module Jekyll
|
7
|
+
module Amazon
|
8
|
+
class AmazonResultCache
|
9
|
+
include Singleton
|
10
|
+
|
11
|
+
CACHE_DIR = '.amazon-cache/'.freeze
|
12
|
+
|
13
|
+
ITEM_HASH = {
|
14
|
+
asin: 'ASIN',
|
15
|
+
title: 'ItemAttributes/Title',
|
16
|
+
author: 'ItemAttributes/Author',
|
17
|
+
publisher: 'ItemAttributes/Manufacturer',
|
18
|
+
publication_date: 'ItemAttributes/PublicationDate',
|
19
|
+
release_date: 'ItemAttributes/ReleaseDate',
|
20
|
+
detail_page_url: 'DetailPageURL',
|
21
|
+
small_image_url: 'SmallImage/URL',
|
22
|
+
medium_image_url: 'MediumImage/URL',
|
23
|
+
large_image_url: 'LargeImage/URL'
|
24
|
+
}.freeze
|
25
|
+
|
26
|
+
ECS_ASSOCIATE_TAG = ENV['ECS_ASSOCIATE_TAG'] || ''
|
27
|
+
AWS_ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY'] || ''
|
28
|
+
AWS_SECRET_KEY = ENV['AWS_SECRET_KEY'] || ''
|
29
|
+
|
30
|
+
raise 'AWS_ACCESS_KEY_ID env variable is not set' if AWS_ACCESS_KEY_ID.empty?
|
31
|
+
raise 'AWS_SECRET_KEY env variable is not set' if AWS_SECRET_KEY.empty?
|
32
|
+
raise 'ECS_ASSOCIATE_TAG env variable is not set' if ECS_ASSOCIATE_TAG.empty?
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
@result_cache = {}
|
36
|
+
@cache_dir = Pathname.new(CACHE_DIR)
|
37
|
+
@cache_dir.mkdir_p
|
38
|
+
end
|
39
|
+
|
40
|
+
def setup(context)
|
41
|
+
context.registers[:site]
|
42
|
+
::Amazon::Ecs.debug = true
|
43
|
+
::Amazon::Ecs.configure do |options|
|
44
|
+
options[:associate_tag] = ECS_ASSOCIATE_TAG
|
45
|
+
options[:AWS_access_key_id] = AWS_ACCESS_KEY_ID
|
46
|
+
options[:AWS_secret_key] = AWS_SECRET_KEY
|
47
|
+
options[:response_group] = 'Images,ItemAttributes'
|
48
|
+
options[:country] = ENV['ECS_COUNTRY'] || 'jp'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def item_lookup(asin)
|
53
|
+
return @result_cache[asin] if @result_cache.key?(asin)
|
54
|
+
return read_cache(asin) if read_cache(asin)
|
55
|
+
|
56
|
+
retry_api do
|
57
|
+
res = ::Amazon::Ecs.item_lookup(asin)
|
58
|
+
item = res.first_item
|
59
|
+
data = create_data(item)
|
60
|
+
write_cache(asin, data)
|
61
|
+
@result_cache[asin] = data
|
62
|
+
@result_cache[asin]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def read_cache(asin)
|
69
|
+
path = @cache_dir.join(asin)
|
70
|
+
return nil unless path.file?
|
71
|
+
Marshal.load(path.read)
|
72
|
+
end
|
73
|
+
|
74
|
+
def write_cache(asin, obj)
|
75
|
+
path = @cache_dir.join(asin)
|
76
|
+
path.open { |f| f.write(Marshal.dump(obj)) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def retry_api
|
80
|
+
yield
|
81
|
+
rescue
|
82
|
+
retry_count ||= 0
|
83
|
+
retry_count += 1
|
84
|
+
sleep retry_count
|
85
|
+
retry if retry_count <= 5
|
86
|
+
raise
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_data(item)
|
90
|
+
ITEM_HASH.each_with_object({}) do |(key, value), hash|
|
91
|
+
hash[key] = item.get(value).to_s
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class AmazonTag < Liquid::Tag
|
97
|
+
attr_accessor :asin
|
98
|
+
attr_accessor :template_type
|
99
|
+
|
100
|
+
def initialize(tag_name, markup, tokens)
|
101
|
+
super
|
102
|
+
parse_options(markup)
|
103
|
+
if asin.nil? || asin.empty?
|
104
|
+
raise SyntaxError, "No ASIN given in #{tag_name} tag"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def render(context)
|
109
|
+
type = template_type || :detail
|
110
|
+
AmazonResultCache.instance.setup(context)
|
111
|
+
item = AmazonResultCache.instance.item_lookup(asin.to_s)
|
112
|
+
return unless item
|
113
|
+
send(type, item)
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def parse_options(markup)
|
119
|
+
options = (markup || '').split(' ').map(&:strip)
|
120
|
+
self.asin = options.shift
|
121
|
+
self.template_type = options.shift || :title
|
122
|
+
end
|
123
|
+
|
124
|
+
def title(item)
|
125
|
+
url = item[:detail_page_url]
|
126
|
+
title = item[:title]
|
127
|
+
format(%(<a href="%s" target="_blank">%s</a>), url, title)
|
128
|
+
end
|
129
|
+
|
130
|
+
def image(item)
|
131
|
+
url = item[:detail_page_url]
|
132
|
+
title = item[:title]
|
133
|
+
image_url = item[:medium_image_url]
|
134
|
+
str = <<-"EOS"
|
135
|
+
<a href="#{url}" target="_blank">
|
136
|
+
<img src="#{image_url}" alt="#{title}" />
|
137
|
+
</a>
|
138
|
+
EOS
|
139
|
+
str.to_s
|
140
|
+
end
|
141
|
+
|
142
|
+
def detail(item)
|
143
|
+
url = item[:detail_page_url]
|
144
|
+
image_url = item[:medium_image_url]
|
145
|
+
title = item[:title]
|
146
|
+
author = item[:author]
|
147
|
+
publisher = item[:publisher]
|
148
|
+
date = item[:publication_date] || item[:release_date]
|
149
|
+
str = <<-"EOS"
|
150
|
+
<div class="amazon_item">
|
151
|
+
<div class="amazon_image">
|
152
|
+
<a href="#{url}" target="_blank">
|
153
|
+
<img src="#{image_url}" alt="#{title}" />
|
154
|
+
</a>
|
155
|
+
</div>
|
156
|
+
<div class="amazon_info">
|
157
|
+
<div class="amazon_info_title">
|
158
|
+
<a href="#{url}" target="_blank">#{title}</a>
|
159
|
+
</div>
|
160
|
+
<div class="amazon_info_detail">
|
161
|
+
<span class="amazon_info_author">#{author} </span>
|
162
|
+
<span class="amazon_info_publisher">#{publisher} </span>
|
163
|
+
<span class="amazon_info_date">#{date} </span>
|
164
|
+
</div>
|
165
|
+
</div>
|
166
|
+
</div>
|
167
|
+
EOS
|
168
|
+
str.to_s
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll-amazon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tokzk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: amazon-ecs
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jekyll
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- t@okzk.org
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- Gemfile
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- jekyll-amazon.gemspec
|
98
|
+
- lib/jekyll-amazon.rb
|
99
|
+
- lib/jekyll-amazon/amazon_tag.rb
|
100
|
+
- lib/jekyll-amazon/version.rb
|
101
|
+
homepage: https://github.com/tokzk/jekyll-amazon
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.4.8
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Liquid tag for display Amazon associate links in Jekyll sites.
|
125
|
+
test_files: []
|