Class: Imgur::Client::Real
- Inherits:
-
Object
- Object
- Imgur::Client::Real
- Defined in:
- lib/imgur/client.rb,
lib/imgur/requests/get_album.rb,
lib/imgur/requests/get_image.rb,
lib/imgur/requests/get_albums.rb,
lib/imgur/requests/get_images.rb,
lib/imgur/requests/get_account.rb,
lib/imgur/requests/delete_image.rb,
lib/imgur/requests/get_accounts.rb,
lib/imgur/requests/upload_image.rb
Instance Attribute Summary collapse
-
#authorize_path ⇒ Object
Returns the value of attribute authorize_path.
-
#config ⇒ Object
Returns the value of attribute config.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#path ⇒ Object
Returns the value of attribute path.
-
#token_path ⇒ Object
Returns the value of attribute token_path.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #credits ⇒ Object
- #delete_image(deletehash) ⇒ Object
- #get_account(id) ⇒ Object
- #get_accounts(params = {}) ⇒ Object
- #get_album(id) ⇒ Object
- #get_albums(params = {}) ⇒ Object
- #get_image(id) ⇒ Object
- #get_images(params = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Real
constructor
A new instance of Real.
- #refresh_token ⇒ Object
- #request(options = {}) ⇒ Object
- #reset! ⇒ Object
- #upload_image(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
34 35 36 37 38 39 40 41 42 |
# File 'lib/imgur/client.rb', line 34 def initialize(={}) @config = [:config] || YAML.load_file(File.("~/.imgurrc")) || YAML.load_file("config/config.yml") @authorize_path = "/oauth2/authorize" @token_path = "/oauth2/token" @url = URI.parse([:url] || "https://api.imgur.com") @logger = [:logger] || Logger.new(nil) @parser = begin; require 'json'; JSON; end @connection = RestClient::Resource.new(@url) end |
Instance Attribute Details
#authorize_path ⇒ Object
Returns the value of attribute authorize_path.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def @authorize_path end |
#config ⇒ Object
Returns the value of attribute config.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def config @config end |
#connection ⇒ Object
Returns the value of attribute connection.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def connection @connection end |
#logger ⇒ Object
Returns the value of attribute logger.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def logger @logger end |
#parser ⇒ Object
Returns the value of attribute parser.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def parser @parser end |
#path ⇒ Object
Returns the value of attribute path.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def path @path end |
#token_path ⇒ Object
Returns the value of attribute token_path.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def token_path @token_path end |
#url ⇒ Object
Returns the value of attribute url.
32 33 34 |
# File 'lib/imgur/client.rb', line 32 def url @url end |
Instance Method Details
#credits ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/imgur/client.rb', line 108 def credits response = request( method: :get, path: "/credits" ).body["data"] "#{response["UserRemaining"]}/#{response["UserLimit"]}" end |
#delete_image(deletehash) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/imgur/requests/delete_image.rb', line 3 def delete_image(deletehash) path = "/image/#{deletehash}" request( :method => :delete, :path => path, ) end |
#get_account(id) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/imgur/requests/get_account.rb', line 3 def get_account(id) path = "/#{id}" request( :method => :get, :path => path, ) end |
#get_accounts(params = {}) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/imgur/requests/get_accounts.rb', line 3 def get_accounts(params={}) path = params[:path] request( :method => :get, :path => path, ) end |
#get_album(id) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/imgur/requests/get_album.rb', line 3 def get_album(id) path = "/album/#{id}" request( :method => :get, :path => path, ) end |
#get_albums(params = {}) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/imgur/requests/get_albums.rb', line 3 def get_albums(params={}) path = params[:path] request( method: :get, path: path, ) end |
#get_image(id) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/imgur/requests/get_image.rb', line 3 def get_image(id) path = "/image/#{id["id"]}" request( :method => :get, :path => path, ) end |
#get_images(params = {}) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/imgur/requests/get_images.rb', line 3 def get_images(params={}) path = params[:path] request( :method => :get, :path => path, ) end |
#refresh_token ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/imgur/client.rb', line 49 def refresh_token response = RestClient.post( @url.to_s + @token_path, :client_id => @config[:client_id], :client_secret => @config[:client_secret], :refresh_token => @config[:refresh_token], :grant_type => "refresh_token", ) new_params = @parser.load(response) @config[:access_token] = new_params["access_token"] @config[:refresh_token] = new_params["refresh_token"] File.open(File.("~/.imgurrc"), "w") { |f| YAML.dump(@config, f) } self.reset! return true end |
#request(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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/imgur/client.rb', line 65 def request(={}) method = ([:method] || :get).to_s.downcase path = "/3#{[:path]}" || "/3" query = [:query] || {} unless @config[:access_token] Launchy.open(@url.to_s + @authorize_path + "?client_id=#{@config[:client_id]}&response_type=token") puts "Copy and paste access_token from URL here" verifier = $stdin.gets.strip puts "Copy and paste refresh_token from URL here" refresh_token = $stdin.gets.strip @config[:access_token] = verifier @config[:refresh_token] = refresh_token File.open(File.("~/.imgurrc"), 'w') { |f| YAML.dump(@config, f) } end headers = { "Accept" => "application/json", "Authorization" => "Bearer #{@config[:access_token]}", }.merge([:headers] || {}) request_body = if body = [:body] headers.merge!("Content-Type" => "application/json, charset=utf-8", "Content-Length" => body.to_s.size.to_s,) body end request_body ||= [:params] || {} path = "#{path}?#{query.map{|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&")}" unless query.empty? begin response = case method when "get" @connection[path].get(headers) when "post" @connection[path].post(request_body, headers) when "delete" @connection[path].delete(headers) end rescue RestClient::Forbidden => e self.refresh_token retry end parsed_body = response.strip.empty? ? {} : parser.load(response) status = parsed_body.delete("status") Imgur::Response.new(status, {}, parsed_body).raise! end |
#reset! ⇒ Object
44 45 46 47 |
# File 'lib/imgur/client.rb', line 44 def reset! @config = nil @config = YAML.load_file(File.("~/.imgurrc")) end |
#upload_image(options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/imgur/requests/upload_image.rb', line 3 def upload_image(={}) path = "/upload" request( :method => :post, :path => path, :body => , ) end |