Class: Imgur::Client
- Inherits:
-
Object
- Object
- Imgur::Client
- Defined in:
- lib/imgur.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
Instance Method Summary collapse
- #auth_header ⇒ Object
- #get(url, data = {}) ⇒ Object
- #get_account(username) ⇒ Object
- #get_album(id) ⇒ Object
- #get_image(id) ⇒ Object
-
#initialize(client_id) ⇒ Client
constructor
A new instance of Client.
- #me ⇒ Object
- #new_album(images = nil, options = {}) ⇒ Object
- #post(url, body = {}) ⇒ Object
- #upload(local_file) ⇒ Object
Constructor Details
#initialize(client_id) ⇒ Client
Returns a new instance of Client.
17 18 19 |
# File 'lib/imgur.rb', line 17 def initialize(client_id) @client_id = client_id end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
15 16 17 |
# File 'lib/imgur.rb', line 15 def client_id @client_id end |
Instance Method Details
#auth_header ⇒ Object
92 93 94 |
# File 'lib/imgur.rb', line 92 def auth_header {'Authorization' => 'Client-ID ' + @client_id} end |
#get(url, data = {}) ⇒ Object
25 26 27 |
# File 'lib/imgur.rb', line 25 def get(url, data={}) HTTParty.get(url, query: data, headers: auth_header) end |
#get_account(username) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/imgur.rb', line 41 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) ⇒ Object
35 36 37 38 39 |
# File 'lib/imgur.rb', line 35 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
29 30 31 32 33 |
# File 'lib/imgur.rb', line 29 def get_image(id) url = API_PATH + IMAGE_PATH + id resp = get(url).parsed_response Image.new(resp['data']) end |
#me ⇒ Object
48 49 50 |
# File 'lib/imgur.rb', line 48 def me get_account 'me' end |
#new_album(images = nil, options = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/imgur.rb', line 65 def new_album(images=nil, ={}) 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 [:cover] = [:cover].id if [:cover].is_a? Image body = {ids: image_ids}.merge 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
21 22 23 |
# File 'lib/imgur.rb', line 21 def post(url, body={}) HTTParty.post(url, body: body, headers: auth_header) end |
#upload(local_file) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/imgur.rb', line 52 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 |