Sha256: e4f05b075a69d92ec25f1c3cfcae9eb754281d5fd4c557411556ea0f18c74b1f

Contents?: true

Size: 882 Bytes

Versions: 8

Compression:

Stored size: 882 Bytes

Contents

require 'json'
require 'typhoeus'
require 'wgif/exceptions'

module WGif
  class Uploader

    UPLOAD_ENDPOINT = 'https://api.imgur.com/3/image'

    def initialize(client_id)
      @client_id = client_id
    end

    def upload(filename)
      File.open(filename, 'r') do |file|
        response = Typhoeus.post(UPLOAD_ENDPOINT,
                                 body: { image: file },
                                 headers: auth_header)
        if response.success?
          image_url(response)
        else
          fail WGif::ImgurException, error_message(response)
        end
      end
    end

    private

    def image_url(response)
      JSON.parse(response.body)['data']['link']
    end

    def error_message(response)
      JSON.parse(response.body)['data']['error']
    end

    def auth_header
      { Authorization: "Client-ID #{@client_id}" }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wgif-0.5.4 lib/wgif/uploader.rb
wgif-0.5.3 lib/wgif/uploader.rb
wgif-0.5.2 lib/wgif/uploader.rb
wgif-0.5.1 lib/wgif/uploader.rb
wgif-0.5.0 lib/wgif/uploader.rb
wgif-0.4.0 lib/wgif/uploader.rb
wgif-0.3.1 lib/wgif/uploader.rb
wgif-0.3.0 lib/wgif/uploader.rb