Class: Imgurz::Client
- Inherits:
-
Struct
- Object
- Struct
- Imgurz::Client
- Defined in:
- lib/imgurz/client.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #as_anonymous ⇒ Object
- #as_regsitered ⇒ Object
- #delete(deletehash) ⇒ Object
- #into(album_id) ⇒ Object
- #upload(image) ⇒ Object
- #upload_file(file) ⇒ Object
- #upload_file_at(file_path) ⇒ Object
- #upload_image_content(file_content, type = nil) ⇒ Object
- #upload_url(url) ⇒ Object
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key
8 9 10 |
# File 'lib/imgurz/client.rb', line 8 def key @key end |
Instance Method Details
#as_anonymous ⇒ Object
10 11 12 13 |
# File 'lib/imgurz/client.rb', line 10 def as_anonymous @method = 'anonymous' self end |
#as_regsitered ⇒ Object
15 16 17 18 |
# File 'lib/imgurz/client.rb', line 15 def as_regsitered @method = 'registered' self end |
#delete(deletehash) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/imgurz/client.rb', line 65 def delete(deletehash) uri = URI.parse [Imgurz::IMAGE_ENDPOINT,deletehash].join('/') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Delete.new uri.path request['Authorization'] = "Bearer #{key}" if @method=='registered' response = http.request(request) JSON.parse response.body end |
#into(album_id) ⇒ Object
20 21 22 23 |
# File 'lib/imgurz/client.rb', line 20 def into(album_id) @album_id = album_id self end |
#upload(image) ⇒ Object
25 26 27 28 29 |
# File 'lib/imgurz/client.rb', line 25 def upload(image) return self.upload_file(image) if image.kind_of?(File) # already a file return self.upload_url(image) if image.kind_of?(String) and image.start_with?('http') # an URL self.upload_file_at(image) # we assume that a path is given end |
#upload_file(file) ⇒ Object
38 39 40 41 42 |
# File 'lib/imgurz/client.rb', line 38 def upload_file(file) return false unless file.kind_of?(File) content = [file.read].pack('m') self.upload_image_content(content) end |
#upload_file_at(file_path) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/imgurz/client.rb', line 31 def upload_file_at(file_path) return false unless file_path.kind_of?(String) File.open(file_path, 'rb') { |file| return self.upload_file(file) } end |
#upload_image_content(file_content, type = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/imgurz/client.rb', line 49 def upload_image_content(file_content, type=nil) return false unless file_content.kind_of?(String) uri = URI.parse Imgurz::IMAGE_ENDPOINT http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new uri.path self.as_anonymous unless @method request['Authorization'] = "Client-ID #{key}" if @method=='anonymous' request['Authorization'] = "Bearer #{key}" if @method=='registered' data = {image:file_content, type:type} data[:album] = @album_id if @album_id request.set_form_data(data) response = http.request(request) JSON.parse response.body end |
#upload_url(url) ⇒ Object
44 45 46 47 |
# File 'lib/imgurz/client.rb', line 44 def upload_url(url) return false unless url.kind_of?(String) and url.start_with?('http') self.upload_image_content(url,'url') end |