This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path500px_tag.rb
52 lines (44 loc) · 1.57 KB
/
500px_tag.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
# jekyll-500px-embed
# ------------------
# A Liquid tag for Jekyll for embedding 500px photos
# Author: Luke Korth (github.com/lkorth)
#
# Example: {% 500px 89255597 %}
# Embeds 500px photo with id 89255597
require 'net/https'
require 'uri'
require 'json'
module Jekyll
class FiveHundredPxTag < Liquid::Tag
CONSUMER_KEY_FILE = "500px_consumer_key_file"
def initialize(tag_name, photo_id, token)
super
@photo_id = photo_id.strip
end
def render(context)
if context.registers[:site].config[CONSUMER_KEY_FILE] == nil
""
else
puts "No 500px consumer key found" unless File.exists?(context.registers[:site].config[CONSUMER_KEY_FILE])
consumer_key = File.read(context.registers[:site].config[CONSUMER_KEY_FILE]).strip
photo = fetch_photo(@photo_id, consumer_key)
<<-EOF
<div class="embeded-five-hundred-px-photo">
<div class="pixels-photo">
<p><img src="#{photo['image_url']}" alt="#{photo['name']} by #{photo['user']['fullname']} on 500px"></p>
<a href="https://500px.com/#{photo['url']}">#{photo['name']} by #{photo['user']['fullname']} on 500px</a>
</div>
</div>
EOF
end
end
def fetch_photo(photo_id, consumer_key)
uri = URI.parse("https://api.500px.com/v1/photos/#{photo_id}?image_size=4&consumer_key=#{consumer_key}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
photo = JSON.parse(http.request(Net::HTTP::Get.new(uri.request_uri)).body)
photo['photo']
end
end
end
Liquid::Template.register_tag('500px', Jekyll::FiveHundredPxTag)