Sha256: 14c0a3ee248f84127f2bfb9c5a57767d1b58915275d720cd2418abf9e3beb03f

Contents?: true

Size: 846 Bytes

Versions: 1

Compression:

Stored size: 846 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)
        raise WGif::ImgurException, error_message(response) unless response.success?
        image_url(response)
      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

1 entries across 1 versions & 1 rubygems

Version Path
wgif-0.2.0 lib/wgif/uploader.rb