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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#authorize_pathObject

Returns the value of attribute authorize_path.



17
18
19
# File 'lib/imgur/client.rb', line 17

def authorize_path
  @authorize_path
end

#configObject

Returns the value of attribute config.



17
18
19
# File 'lib/imgur/client.rb', line 17

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/imgur/client.rb', line 17

def logger
  @logger
end

#parserObject

Returns the value of attribute parser.



17
18
19
# File 'lib/imgur/client.rb', line 17

def parser
  @parser
end

#pathObject

Returns the value of attribute path.



17
18
19
# File 'lib/imgur/client.rb', line 17

def path
  @path
end

#token_pathObject

Returns the value of attribute token_path.



17
18
19
# File 'lib/imgur/client.rb', line 17

def token_path
  @token_path
end

#urlObject

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_tokenObject



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.expand_path("~/.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(options={})
  method  = (options[:method] || :get).to_s.downcase
  path    =  @url.to_s + ("/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
    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]
                   json_body    = parser.dump(body)
                   headers      = {
                     "Content-Type"   => "application/json, charset=utf-8",
                     "Content-Length" => json_body.size.to_s,
                   }.merge(options[:headers] || {})

                   json_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 = 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.expand_path("~/.imgurrc"))
end