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, query = {}) ⇒ 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
- #update_album(album) ⇒ Object
- #upload(local_file) ⇒ Object
Constructor Details
#initialize(client_id) ⇒ Client
Returns a new instance of Client.
18 19 20 |
# File 'lib/imgur.rb', line 18 def initialize(client_id) @client_id = client_id end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
16 17 18 |
# File 'lib/imgur.rb', line 16 def client_id @client_id end |
Instance Method Details
#auth_header ⇒ Object
101 102 103 |
# File 'lib/imgur.rb', line 101 def auth_header {'Authorization' => 'Client-ID ' + @client_id} end |
#get(url, query = {}) ⇒ Object
28 29 30 31 32 |
# 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) ⇒ Object
46 47 48 49 50 51 |
# 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) ⇒ Object
40 41 42 43 44 |
# 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) ⇒ Object
34 35 36 37 38 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/imgur.rb', line 57 def me get_account 'me' end |
#new_album(images = nil, options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/imgur.rb', line 74 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
22 23 24 25 26 |
# 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) ⇒ Object
53 54 55 |
# File 'lib/imgur.rb', line 53 def update_album(album) album.update self end |
#upload(local_file) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# 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 |