Class: Pickpocket::Authentication::Oauth

Inherits:
Object
  • Object
show all
Defined in:
lib/pickpocket/authentication/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOauth

Returns a new instance of Oauth.



10
11
12
# File 'lib/pickpocket/authentication/oauth.rb', line 10

def initialize
  @token_handler = TokenHandler.new
end

Instance Attribute Details

#token_handlerObject (readonly)

Returns the value of attribute token_handler.



8
9
10
# File 'lib/pickpocket/authentication/oauth.rb', line 8

def token_handler
  @token_handler
end

Instance Method Details

#authorizeObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pickpocket/authentication/oauth.rb', line 29

def authorize
  response_token = token_handler.read_oauth
  uri            = URI(Pickpocket.config.pocket_oauth_authorize_url)

  response = Net::HTTP.post_form(uri, {
      consumer_key: Pickpocket.config.consumer_key,
      code:         response_token
  })

  response_token = CGI::parse(response.body)['access_token'][0]
  token_handler.save_auth(response_token)
end

#request_authorizationObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pickpocket/authentication/oauth.rb', line 14

def request_authorization
  uri      = URI(Pickpocket.config.pocket_oauth_request_url)
  response = Net::HTTP.post_form(uri, {
      consumer_key: Pickpocket.config.consumer_key,
      redirect_uri: Pickpocket.config.pocket_homepage
  })

  response_token = CGI::parse(response.body)['code'][0]
  auth_url       = URI(Pickpocket.config.pocket_user_authorize_url)
  auth_url.query = "request_token=#{response_token}&redirect_uri=#{Pickpocket.config.pocket_homepage}"

  Launchy.open(auth_url.to_s)
  token_handler.save_oauth(response_token)
end