middleman-amazon-link 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e3e2a7ef2e46dfb78641dda0fb2ce786eaf4a6b
4
+ data.tar.gz: 65e03037b190a4ea9b2d286570e8d8c404303b32
5
+ SHA512:
6
+ metadata.gz: cf5e0a284a0e998ab18862ad68ffc97f6c8b41634f732b7a9b9287ddd6e1a38f0c91ff3acaa5859afc5065fc20fc7cd1246765a50d1b6734fd536ac68787947a
7
+ data.tar.gz: 7470f9f64fb77a1ebf4fc40d4b6d2d9fdcac41a3516f9ef0fb5edbc28fb862810299cc8dc92bcb4b9eda8c58877d242f659acff831ecec99d5a1a6868374a971
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in middleman-amazon-link.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ataru Kodaka
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # middleman-amazon-link
2
+
3
+
4
+ **middleman-amazon-link** is a [Middleman](http://middlemanapp.com)
5
+ extension that generates links to amazon on products specified.
6
+
7
+
8
+ ## Installation
9
+
10
+
11
+ 1. Specify the dependency in your project's `Gemfile`:
12
+
13
+ ```ruby
14
+ gem "middleman-amazon-link"
15
+ ```
16
+
17
+ 2. Activate the extension in your project's `config.rb`:
18
+
19
+ ```ruby
20
+ # config.rb
21
+ activate :amazon_link do |amazon|
22
+ amazon.aws_access_key_id = "xxxx"
23
+ amazon.aws_secret_key = "xxx"
24
+ amazon.associate_tag = "xxxx-22"
25
+ amazon.country = "ja"
26
+ amazon.use_cache = true
27
+ amazon.cache_dir = ".cache/amazon"
28
+ end
29
+ ```
30
+
31
+ ## Configuration
32
+
33
+ ```ruby
34
+ # config.rb
35
+ activate :amazon_link do |amazon|
36
+ amazon.aws_access_key_id = "xxxx"
37
+ amazon.aws_secret_key = "xxx"
38
+
39
+
40
+ amazon.associate_tag = "xxxx-22"
41
+ amazon.country = "ja"
42
+ amazon.use_cache = true
43
+ amazon.cache_dir = ".cache/amazon"
44
+ end
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ Call 'amazon(asin)' function in your page file:
50
+
51
+ **ERB:**
52
+
53
+ ```erb
54
+ # Amazon Link
55
+ <%= amazon('ASINXXXXXXX', :detail) %>
56
+ ```
57
+
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new [Pull Request](../../pull/new/master)
66
+
67
+ ## License
68
+
69
+ Copyright (c) 2015 Ataru Kodaka. See [LICENSE](./LICENSE) for details.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,6 @@
1
+ require "middleman-amazon-link/version"
2
+
3
+ ::Middleman::Extensions.register(:amazon_link) do
4
+ require 'middleman-amazon-link/extension'
5
+ ::Middleman::AmazonLink::Extension
6
+ end
@@ -0,0 +1,72 @@
1
+ module Middleman
2
+ module AmazonLink
3
+ class EcsLookupWrapper
4
+ def initialize(ecs_opt, opt={})
5
+ ecs_opt[:country] ||= 'jp'
6
+ ecs_opt[:response_group] = 'Images,ItemAttributes'
7
+
8
+ Amazon::Ecs.options= ecs_opt
9
+ # required
10
+ # :AWS_access_key_id
11
+ # :AWS_secret_key
12
+ # optional
13
+ # :associate_tag
14
+ # :country
15
+
16
+ @use_cache = opt[:use_cache]
17
+ @cache_dir = opt[:cache_dir] || "./caches/amazon"
18
+
19
+ @result_cache = {}
20
+ end
21
+ def item_lookup(asin)
22
+ return @result_cache[asin] if @result_cache.has_key?(asin)
23
+ return @result_cache[asin] = load_cache(asin) if @use_cache && File.exist?(cache_filename(asin))
24
+
25
+ cnt = 0
26
+ begin
27
+ res = Amazon::Ecs.item_lookup(asin)
28
+ rescue Amazon::RequestError => err
29
+ if /503/ =~ err.message && cnt < 3
30
+ sleep 3
31
+ cnt += 1
32
+ $stderr.puts " retrying...#{asin}/#{cnt}"
33
+ $stderr.puts " options: #{Amazon::Ecs.options.inspect}"
34
+ retry
35
+ else
36
+ raise err
37
+ end
38
+ end
39
+
40
+ res.items.each do |item|
41
+ hash = {
42
+ asin: asin,
43
+ title: item.get('ItemAttributes/Title').to_s,
44
+ author: item.get('ItemAttributes/Author').to_s,
45
+ publisher: item.get('ItemAttributes/Manufacturer').to_s,
46
+ date: item.get('ItemAttributes/PublicationDate').to_s ||
47
+ item.get('ItemAttributes/ReleaseDate').to_s,
48
+ detail_url: item.get('DetailPageURL').to_s,
49
+
50
+ image_small: item.get('SmallImage/URL').to_s,
51
+ image_medium: item.get('MediumImage/URL').to_s,
52
+ image_large: item.get('LargeImage/URL').to_s,
53
+ }
54
+ @result_cache[asin] = hash
55
+ save_cache(asin, hash) if @use_cache
56
+ end
57
+ return @result_cache[asin]
58
+ end
59
+ ################
60
+ # caching
61
+ def cache_filename(asin)
62
+ File.join(@cache_dir, asin)
63
+ end
64
+ def load_cache(asin)
65
+ Marshal.load(File.read(cache_filename(asin)))
66
+ end
67
+ def save_cache(asin, hash)
68
+ File.open(cache_filename(asin), 'wb'){|f| f.write(Marshal.dump(hash))}
69
+ end
70
+ end
71
+ end ## class
72
+ end
@@ -0,0 +1,25 @@
1
+ require 'middleman-amazon-link/helpers'
2
+
3
+ module Middleman
4
+ module AmazonLink
5
+ class Extension < Middleman::Extension
6
+ option :aws_access_key_id, nil, 'AWS Access Key ID'
7
+ option :aws_secret_key, nil, 'AWS Secret key'
8
+ option :associate_tag, nil, 'tag: xxx-22'
9
+ option :country, 'jp', 'country'
10
+
11
+ option :use_cache, true, 'use cache or not'
12
+ option :cache_dir, './caches/amazon', 'directory for caches'
13
+
14
+ def initialize(app, options_hash = {}, &block)
15
+ super
16
+ app.set :amazon_link_settings, options
17
+ end
18
+ helpers do
19
+ include Middleman::AmazonLink::Helpers
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+
@@ -0,0 +1,45 @@
1
+ require 'middleman-amazon-link/ecs_wrapper'
2
+
3
+ module Middleman
4
+ module AmazonLink
5
+ module Helpers
6
+ @@templates = {
7
+ title: %Q{<span><a href="%{title}" target="_blank">%{title}</a></span>},
8
+ detail:
9
+ %(
10
+ <div class="amazon_item">
11
+ <a href="%{detail_url}" target="_blank"><img src="%{image_medium}"></a>
12
+ <a href="%{detail_url}" target="_blank">%{title}</a><br/>
13
+ <div class="item_detail">
14
+ %{author} / %{publisher} / %{date}
15
+ </div>
16
+ </div>)
17
+ }
18
+ def amazon(asin, type_or_string = :detail)
19
+ #binding.pry
20
+
21
+ amazon_opts = amazon_link_settings
22
+ ecs_opt = {
23
+ associate_tag: amazon_opts.associate_tag,
24
+ AWS_access_key_id: amazon_opts.aws_access_key_id,
25
+ AWS_secret_key: amazon_opts.aws_secret_key,
26
+ country: amazon_opts.country,
27
+ }
28
+ opt = {
29
+ use_cache: amazon_opts.use_cache,
30
+ cache_dir: amazon_opts.cache_dir
31
+ }
32
+ amazon_lookup = Middleman::AmazonLink::EcsLookupWrapper.new(ecs_opt, opt)
33
+ hash = amazon_lookup.item_lookup(asin)
34
+
35
+ if block_given?
36
+ yield(hash)
37
+ else
38
+ template = (type_or_string.class == Symbol) ? @@templates[type_or_string] : type_or_string
39
+ #or raise "no such template type: '#{type}'"
40
+ template % hash
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Middleman
2
+ module AmazonLink
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'middleman-amazon-link/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "middleman-amazon-link"
8
+ spec.version = Middleman::AmazonLink::VERSION
9
+ spec.authors = ["Ataru Kodaka"]
10
+ spec.email = ["ataru.kodaka@gmail.com"]
11
+ spec.summary = %q{Middleman Extention for amazon-link.}
12
+ spec.description = %q{helprs provided such as amazon(asin, :detail)}
13
+ spec.homepage = "http://github.com/atarukodaka/middleman-amazon-link"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-amazon-link
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ataru Kodaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: helprs provided such as amazon(asin, :detail)
42
+ email:
43
+ - ataru.kodaka@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/middleman-amazon-link.rb
54
+ - lib/middleman-amazon-link/ecs_wrapper.rb
55
+ - lib/middleman-amazon-link/extension.rb
56
+ - lib/middleman-amazon-link/helpers.rb
57
+ - lib/middleman-amazon-link/version.rb
58
+ - middleman-amazon-link.gemspec
59
+ homepage: http://github.com/atarukodaka/middleman-amazon-link
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Middleman Extention for amazon-link.
83
+ test_files: []