class Imgur::Client

Attributes

client_id[RW]

Public Class Methods

new(client_id) click to toggle source
# File lib/imgur.rb, line 18
def initialize(client_id)
  @client_id = client_id
end

Public Instance Methods

auth_header() click to toggle source
# File lib/imgur.rb, line 101
def auth_header
  {'Authorization' => 'Client-ID ' + @client_id}
end
get(url, query={}) click to toggle source
# File lib/imgur.rb, line 28
def get(url, query={})
  resp = HTTParty.get(url, query: query, headers: auth_header)
  raise NotFoundException.new if resp.response.is_a? Net::HTTPNotFound
  resp
end
get_account(username) click to toggle source
# File lib/imgur.rb, line 46
def get_account(username)
  url = API_PATH + ACCOUNT_PATH + username
  resp = get(url).parsed_response
  # The Imgur API doesn't send the username back
  Account.new resp['data'], username
end
get_album(id) click to toggle source
# File lib/imgur.rb, line 40
def get_album(id)
  url = API_PATH + ALBUM_GET_PATH + id
  resp = get(url).parsed_response
  Album.new resp['data']
end
get_image(id) click to toggle source
# File lib/imgur.rb, line 34
def get_image(id)
  url = API_PATH + IMAGE_PATH + id
  resp = get(url).parsed_response
  Image.new(resp['data'])
end
me() click to toggle source
# File lib/imgur.rb, line 57
def me
  get_account 'me'
end
new_album(images=nil, options={}) click to toggle source
# File lib/imgur.rb, line 74
def new_album(images=nil, options={})
  image_ids = []
  if images.is_a? Array
    if images[0].is_a? Image
      images.each do |img|
        image_ids << img.id
      end
    elsif images[0].is_a? String
      image_ids = images
    end
  elsif
    if images.is_a? Image
      image_ids << images.id
    elsif images.is_a? String
      image_ids << images
    end
  end
  options[:cover] = options[:cover].id if options[:cover].is_a? Image
  body = {ids: image_ids}.merge options
  url = API_PATH + ALBUM_CREATE_PATH
  resp = post(url, body).parsed_response
  id = resp['data']['id']
  album = get_album id
  album.deletehash = resp['data']['deletehash']
  album
end
post(url, body={}) click to toggle source
# File lib/imgur.rb, line 22
def post(url, body={})
  resp = HTTParty.post(url, body: body, headers: auth_header)
  raise NotFoundException.new if resp.response.is_a? Net::HTTPNotFound
  resp
end
update_album(album) click to toggle source
# File lib/imgur.rb, line 53
def update_album(album)
  album.update self
end
upload(local_file) click to toggle source
# File lib/imgur.rb, line 61
def upload(local_file)
  local_file.file.rewind
  image = local_file.file.read
  body = {image: image, type: 'file'}
  body[:title] = local_file.title if local_file.title
  body[:description] = local_file.description if local_file.description
  body[:album] = local_file.album_id if local_file.album_id
  resp = post(API_PATH + UPLOAD_PATH, body).parsed_response
  # the Imgur API doesn't send title or description back apparently.
  data = resp['data'].merge({'title' => body[:title], 'description' => body[:description]})
  Image.new(data)
end