From 37cf88f473f50d24fb58d261a549fed354b73906 Mon Sep 17 00:00:00 2001 From: tamoot Date: Wed, 26 Jan 2011 23:32:03 +0900 Subject: [PATCH] added size option. see http://www.tamoot.net/d/20110126.html#p01 --- plugin/instagr.rb | 66 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/plugin/instagr.rb b/plugin/instagr.rb index 94022547..57730b7c 100644 --- a/plugin/instagr.rb +++ b/plugin/instagr.rb @@ -7,30 +7,54 @@ # # usage: # <%= instagr 'short URL instag.ram' => -# +# <%= instagr 'short URL instag.ram', size} => +# +# available size option: +# :small => 150x150 pixel +# :medium => 306x306 pixel (default) +# :large => 612x612 pixel require 'cgi' require 'json' require 'open-uri' -def instagr( short_url, option = {}) - return %Q|

Argument is empty.. #{short_url}

| if !short_url or short_url.empty? - - query = "?url=#{CGI::escape(short_url)}" - begin - json_data = JSON::parse( open( "http://instagr.am/api/v1/oembed#{query}", &:read ) ) - width = option[:width] ? option[:width] : json_data["width"] - height = option[:height] ? option[:height] : json_data["height"] - - return <<-INSTAGR_DOM -
- - #{h @conf.to_native(json_data[ - -

Taken by #{h json_data["author_name"]}

-
- INSTAGR_DOM - rescue - return %Q|

Failed Open URL.. #{short_url}
#{h $!}

| - end +def instagr( short_url, size = :medium) + return %Q|

Argument is empty.. #{short_url}

| if !short_url or short_url.empty? + option = option.nil? ? {} : option + + # img size + maxwidth_data = {:small => 150, :medium => 306, :large => 612} + maxwidth = maxwidth_data[ size ] ? maxwidth_data[ size ] : maxwidth_data[:medium] + + # proxy + proxy = @conf['proxy'] ? "http://#{@conf['proxy']}" : nil + + # query + query = "?url=#{CGI::escape(short_url)}&maxwidth=#{maxwidth}" + + begin + json_data = JSON::parse( open( "http://instagr.am/api/v1/oembed#{query}", :proxy => proxy, &:read ) ) + width = option[:width] ? option[:width] : json_data["width"] + height = option[:height] ? option[:height] : json_data["height"] + + return <<-INSTAGR_DOM +
+ + #{h @conf.to_native(json_data[ + +

#{h json_data["author_name"]}'s photo.

+
+ INSTAGR_DOM + rescue + return %Q|

Failed Open URL.. #{short_url}
#{h $!}

| + end end + + +# Local Variables: +# mode: ruby +# indent-tabs-mode: t +# tab-width: 3 +# ruby-indent-level: 3 +# End: +# vim: ts=3