Class: Videoinfo::ImageHosts::Imgur

Inherits:
Videoinfo::ImageHost show all
Defined in:
lib/videoinfo/image_hosts/imgur.rb

Instance Method Summary collapse

Constructor Details

#initializeImgur

Returns a new instance of Imgur.



5
6
7
8
9
10
11
# File 'lib/videoinfo/image_hosts/imgur.rb', line 5

def initialize
  @boundary     = SecureRandom.hex(6)
  @header       = { 'Content-Type' => "multipart/form-data, boundary=#{@boundary}" }
  @uri          = URI.parse('https://imgur.com/upload')
  @http         = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = true
end

Instance Method Details

#upload(image) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/videoinfo/image_hosts/imgur.rb', line 13

def upload(image)
  begin
    request      = Net::HTTP::Post.new(@uri.request_uri, @header)
    request.body = post_body(image)
    response     = @http.request(request)
    img_name     = JSON.parse(response.body)['data']['hash']
    "https://i.imgur.com/#{img_name}.png"
  rescue => e
    raise Error, "could not upload image #{File.basename(image.path)}. #{e.message}"
  end
end