Class: Imgur::Client::Real

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



29
30
31
32
33
34
35
36
37
# File 'lib/imgur/client.rb', line 29

def initialize(options={})
  @config                          = options[:config] || YAML.load_file(File.expand_path("~/.imgurrc")) || YAML.load_file("config/config.yml")
  @authorize_path                  = "/oauth2/authorize"
  @token_path                      = "/oauth2/token"
  @url                             = URI.parse(options[:url]  || "https://api.imgur.com")
  @logger                          = options[:logger]         || Logger.new(nil)
  @parser                          = begin; require 'json'; JSON; end
  @connection                      = RestClient::Resource.new(@url)
end

Instance Attribute Details

#authorize_pathObject

Returns the value of attribute authorize_path.



27
28
29
# File 'lib/imgur/client.rb', line 27

def authorize_path
  @authorize_path
end

#configObject

Returns the value of attribute config.



27
28
29
# File 'lib/imgur/client.rb', line 27

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



27
28
29
# File 'lib/imgur/client.rb', line 27

def connection
  @connection
end

#loggerObject

Returns the value of attribute logger.



27
28
29
# File 'lib/imgur/client.rb', line 27

def logger
  @logger
end

#parserObject

Returns the value of attribute parser.



27
28
29
# File 'lib/imgur/client.rb', line 27

def parser
  @parser
end

#pathObject

Returns the value of attribute path.



27
28
29
# File 'lib/imgur/client.rb', line 27

def path
  @path
end

#token_pathObject

Returns the value of attribute token_path.



27
28
29
# File 'lib/imgur/client.rb', line 27

def token_path
  @token_path
end

#urlObject

Returns the value of attribute url.



27
28
29
# File 'lib/imgur/client.rb', line 27

def url
  @url
end

Instance Method Details

#creditsObject



103
104
105
106
107
108
109
# File 'lib/imgur/client.rb', line 103

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 (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_tokenObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/imgur/client.rb', line 44

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.expand_path("~/.imgurrc"), "w") { |f| YAML.dump(@config, f) }
  self.reset!
  return true
end

#request(options = {}) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/imgur/client.rb', line 60

def request(options={})
  method  = (options[:method] || :get).to_s.downcase
  path    = "/3#{options[:path]}" || "/3"
  query   = options[: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.expand_path("~/.imgurrc"), 'w') { |f| YAML.dump(@config, f) }
  end
  headers = {
    "Accept"        => "application/json",
    "Authorization" => "Bearer #{@config[:access_token]}",
  }.merge(options[:headers] || {})

  request_body = if body = options[:body]
                   headers.merge!("Content-Type" => "application/json, charset=utf-8", "Content-Length" => body.to_s.size.to_s,)
                   body
                 end
  request_body ||= options[: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



39
40
41
42
# File 'lib/imgur/client.rb', line 39

def reset!
  @config     = nil
  @config     = YAML.load_file(File.expand_path("~/.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(options)
  path = "/upload"

  request(
    :method => :post,
    :path   => path,
    :body   => options,
  )
end