-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
61 lines (47 loc) · 1.26 KB
/
web.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
require 'sinatra'
require 'net/https'
require 'net/http'
require 'json'
require 'github/markup'
require 'redcarpet'
get '/' do
readme_path = 'README.md'
GitHub::Markup.render(readme_path, File.read(readme_path))
end
post '/' do
request.body.rewind
payload = JSON.parse(request.body.read)
images = payload['imgs']
status_code 400 if images.nil?
title = payload['title']
album = create_album(title)
upload_images(images, album['deletehash'])
{ 'id' => album['id'] }.to_json
end
def upload_images(images, deletehash)
images.each do |image|
imgur_request("/3/image?image=#{image}&album=#{deletehash}&type=URL", 'post')
end
end
def create_album(title)
path = '/3/album'
path += "?title=#{title}" unless title.nil?
response = imgur_request(path, 'post')
body = JSON.parse(response.body)
body['data']
end
def imgur_request(path, type)
headers = { 'Authorization' => "Client-ID #{ENV['IMGUR_API_KEY']}" }
uri = URI("https://api.imgur.com#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = create_request(path, type, headers)
http.request(request)
end
def create_request(path, type, headers)
if type == 'post'
Net::HTTP::Post.new(path, headers)
else
Net::HTTP::Get.new(path, headers)
end
end