Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tamoot committed Jan 26, 2011
1 parent 11f0006 commit 37cf88f
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions plugin/instagr.rb
Expand Up @@ -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|<p>Argument is empty.. #{short_url}</p>| 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
<div class="instagr">
<a class="instagr" href="#{h short_url}">
<img src="#{h json_data["url"]}" width="#{h width}" height="#{h height}" alt="#{h @conf.to_native(json_data["title"])}">
</a>
<p>Taken by #{h json_data["author_name"]}</p>
</div>
INSTAGR_DOM
rescue
return %Q|<p>Failed Open URL.. #{short_url}<br>#{h $!}</p>|
end
def instagr( short_url, size = :medium)
return %Q|<p>Argument is empty.. #{short_url}</p>| 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
<div class="instagr">
<a class="instagr" href="#{h short_url}" title="#{h @conf.to_native(json_data["title"])}">
<img src="#{h json_data["url"]}" width="#{h width}" height="#{h height}" alt="#{h @conf.to_native(json_data["title"])}">
</a>
<p>#{h json_data["author_name"]}'s photo.</p>
</div>
INSTAGR_DOM
rescue
return %Q|<p>Failed Open URL.. #{short_url}<br>#{h $!}</p>|
end
end


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

0 comments on commit 37cf88f

Please sign in to comment.