forked from tdiary/tdiary-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagr.rb
64 lines (55 loc) · 1.79 KB
/
instagr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
#
# instagr.rb - plugin to insert images on instagr.am
#
# Copyright (C) 2011, tamoot <tamoot+tdiary@gmail.com>
# You can redistribute it and/or modify it under GPL2.
#
# 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 'net/http'
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
px_host, px_port = (@conf['proxy'] || '').split( /:/ )
px_port = 80 if px_host and !px_port
# query
query = "?url=#{CGI::escape(short_url)}&maxwidth=#{maxwidth}"
begin
Net::HTTP.version_1_2
res = Net::HTTP::Proxy(px_host, px_port).get('instagram.com', "/api/v1/oembed/#{query}")
json_data = JSON::parse( res, &: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
alias :instagram :instagr
# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
# vim: ts=3