Skip to content

Instantly share code, notes, and snippets.

@suwa320
Created October 8, 2012 23:45
Show Gist options
  • Select an option

  • Save suwa320/3855664 to your computer and use it in GitHub Desktop.

Select an option

Save suwa320/3855664 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'amazon/ecs'
module Amazon
class Element
def to_hash
item_attr = self.get_element('ItemAttributes')
authors = item_attr.get_array('Authors')
authors = [item_attr.get('Author')] if authors.empty?
{
:asin => self.get('ASIN'),
:title => item_attr.get('Title'),
:authors => authors,
:formatted_price => ((self/'ListPrice/FormattedPrice').text rescue ''),
:small_image_url => self.get_hash('SmallImage')['URL'],
:small_image_width => self.get_hash('SmallImage')['Width'],
:small_image_height => self.get_hash('SmallImage')['Height'],
:medium_image_url => self.get_hash('MediumImage')['URL'],
:medium_image_width => self.get_hash('MediumImage')['Width'],
:medium_image_height => self.get_hash('MediumImage')['Height']
}
end
end
end
module Jekyll
module AmazonPlugin
def setup(config)
if not defined? @@setup
Amazon::Ecs.options = {
:associate_tag => config['associate_tag'],
:AWS_access_key_id => config['access_key_id'],
:AWS_secret_key => config['secret_key'],
:country => config['country']
}
@@associate_tag = config['associate_tag']
@@setup = true
end
end
def lookup(asin)
Amazon::Ecs.item_lookup(
asin.strip, {:response_group => 'Large'}).items.first.to_hash
end
def search(query)
sort = @sort || 'salesrank'
Amazon::Ecs.item_search(query,
{:response_group => 'Large', :sort => sort}).items.map{|v| v.to_hash}
end
end
class AmazonTag < Liquid::Tag
include AmazonPlugin
def initialize(tag_name, text, tokens)
super
@asin = text
end
def render(context)
config = context.registers[:site].config['amazon'].dup
setup(config)
item = lookup(@asin)
<<-html.strip
<div class="amazon_product clearfix">
<a href="http://www.amazon.jp/o/ASIN/#{item[:asin]}/#{config['associate_tag']}">
<img class="align-left" src="#{item[:small_image_url]}" width="#{item[:small_image_width]}" height="#{item[:small_image_height]}" />
</a>
<a href="http://www.amazon.jp/o/ASIN/#{item[:asin]}/#{config['associate_tag']}" style="color:blue;text-decoration:none">
<strong class="title">#{item[:title]}</strong>
</a>
<br />
<span class="label_price">価格</span>
<span class="price">#{item[:formatted_price]}</span>
<span class="authors">#{item[:authors].join(', ')}</span>
</div>
html
end
end
class AmazonBlock < Liquid::Block
include AmazonPlugin
def initialize(tag_name, text, tokens)
super
@limit = 3
parse_args(text)
end
def render(context)
config = context.registers[:site].config['amazon'].dup
context['amazon'] = config
setup(config)
if @query
items = search(@query)
else
items = @asin.each_with_object([]){|v, arr| arr << lookup(v)}
end
items[0, @limit].each_with_object('') do |item, str|
str << render_item(item, context)
end
end
private
def parse_args(text)
args = text.split(/\s+/).select{|v|
if v =~ /^limit:(\d+)$/
@limit = $~[1].to_i; nil
nil
elsif v =~ /^sort:(\w+)$/
@sort = $~[1].to_s; nil
else
v
end
}
if args.all?{|v| v =~ /(\d{9}[\dX])/}
@asin = args
else
@query = args.join(' ')
end
end
def render_item(item, context)
@nodelist.each_with_object('') do |node, str|
item[:url] =
"http://www.amazon.jp/o/ASIN/%s/%s/ref=nosim" %
[item[:asin], context['amazon.associate_tag']]
if node.kind_of?(Liquid::Variable)
if item[node.name.to_sym]
str << item[node.name.to_sym]
else
str << node.render(context)
end
else
str << node.to_s
end
end
end
end
end
Liquid::Template.register_tag('amazon', Jekyll::AmazonTag)
Liquid::Template.register_tag('amazonb', Jekyll::AmazonBlock)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment