Class: ImgurUp::Imgur

Inherits:
Object
  • Object
show all
Defined in:
lib/imgur_up/imgur.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, refresh_token = nil) ⇒ Imgur

Returns a new instance of Imgur.



6
7
8
9
10
# File 'lib/imgur_up/imgur.rb', line 6

def initialize(client_id, client_secret, refresh_token = nil)
  @client_id     = client_id
  @client_secret = client_secret
  @refresh_token = refresh_token
end

Instance Method Details

#access_tokenObject



42
43
44
45
46
47
48
# File 'lib/imgur_up/imgur.rb', line 42

def access_token
  return @access_token if @access_token

  url = "https://api.imgur.com/oauth2/token"
  response = RestClient.post(url, client_id: @client_id, client_secret: @client_secret, grant_type: "refresh_token", refresh_token: @refresh_token)
  @access_token = JSON.parse(response)["access_token"]
end

#albumsObject



27
28
29
30
31
32
# File 'lib/imgur_up/imgur.rb', line 27

def albums
  url = "https://api.imgur.com/3/account/me/albums"
  response = RestClient.get(url, Authorization: "Bearer #{access_token}")

  JSON.parse(response)["data"]
end

#authorize(pin) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/imgur_up/imgur.rb', line 16

def authorize(pin)
  url = "https://api.imgur.com/oauth2/token"
  response = RestClient.post(url, client_id: @client_id, client_secret: @client_secret, grant_type: "pin", pin: pin)
  output = JSON.parse(response)

  @access_token  = output["access_token"]
  @refresh_token = output["refresh_token"]

  output
end

#pin_request_urlObject



12
13
14
# File 'lib/imgur_up/imgur.rb', line 12

def pin_request_url
  "https://api.imgur.com/oauth2/authorize?client_id=#{@client_id}&response_type=pin"
end

#upload(image, album) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/imgur_up/imgur.rb', line 34

def upload(image, album)
  path = "https://api.imgur.com/3/image"

  ::ImgurUp.logger.info "Uploading #{image}"
  response = RestClient.post(path, {image: File.open(image, "rb"), album: album}, Authorization: "Bearer #{access_token}")
  JSON.parse(response)["data"]
end