Class: Imgur::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/imgur.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id) ⇒ Client

Returns a new instance of Client.



16
17
18
# File 'lib/imgur.rb', line 16

def initialize(client_id)
  @client_id = client_id
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



14
15
16
# File 'lib/imgur.rb', line 14

def client_id
  @client_id
end

Instance Method Details

#auth_headerObject



80
81
82
# File 'lib/imgur.rb', line 80

def auth_header
  {'Authorization' => 'Client-ID ' + @client_id}
end

#get(url, data = {}) ⇒ Object



24
25
26
# File 'lib/imgur.rb', line 24

def get(url, data={})
  HTTParty.get(url, query: data, headers: auth_header)
end

#get_album(id) ⇒ Object



34
35
36
37
38
# File 'lib/imgur.rb', line 34

def get_album(id)
  url = API_PATH + ALBUM_GET_PATH + id
  resp = get(url).parsed_response
  Album.new resp['data']
end

#get_image(id) ⇒ Object



28
29
30
31
32
# File 'lib/imgur.rb', line 28

def get_image(id)
  url = API_PATH + IMAGE_PATH + id
  resp = get(url).parsed_response
  Image.new(resp['data'])
end

#new_album(images = nil, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/imgur.rb', line 53

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 = {}) ⇒ Object



20
21
22
# File 'lib/imgur.rb', line 20

def post(url, body={})
  HTTParty.post(url, body: body, headers: auth_header)
end

#upload(local_file) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/imgur.rb', line 40

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