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
Instance Attribute Summary collapse
-
#authorize_path ⇒ Object
Returns the value of attribute authorize_path.
-
#config ⇒ Object
Returns the value of attribute config.
-
#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
- #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
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
19 20 21 22 23 24 25 26 |
# File 'lib/imgur/client.rb', line 19 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 end |
Instance Attribute Details
#authorize_path ⇒ Object
Returns the value of attribute authorize_path.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def @authorize_path end |
#config ⇒ Object
Returns the value of attribute config.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def config @config end |
#logger ⇒ Object
Returns the value of attribute logger.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def logger @logger end |
#parser ⇒ Object
Returns the value of attribute parser.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def parser @parser end |
#path ⇒ Object
Returns the value of attribute path.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def path @path end |
#token_path ⇒ Object
Returns the value of attribute token_path.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def token_path @token_path end |
#url ⇒ Object
Returns the value of attribute url.
17 18 19 |
# File 'lib/imgur/client.rb', line 17 def url @url end |
Instance Method Details
#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 |
# File 'lib/imgur/requests/get_albums.rb', line 3 def get_albums(params={}) 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 = "/#{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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/imgur/client.rb', line 33 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
49 50 51 52 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 79 80 81 82 83 84 85 86 87 |
# File 'lib/imgur/client.rb', line 49 def request(={}) method = ([:method] || :get).to_s.downcase path = @url.to_s + ("/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 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] json_body = parser.dump(body) headers = { "Content-Type" => "application/json, charset=utf-8", "Content-Length" => json_body.size.to_s, }.merge([:headers] || {}) json_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 = RestClient.send(method, path, headers) rescue RestClient::Forbidden 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
28 29 30 31 |
# File 'lib/imgur/client.rb', line 28 def reset! @config = nil @config = YAML.load_file(File.("~/.imgurrc")) end |