Module: TwitterOAuthToken

Defined in:
lib/twitter_oauth_token.rb,
lib/twitter_oauth_token/cli.rb,
lib/twitter_oauth_token/get.rb,
lib/twitter_oauth_token/version.rb

Overview

Project constants

Constant Summary collapse

RESULT_ACCESS_TOKEN =
'access_token'
RESULT_ACCESS_TOKEN_SECRET =
'access_token_secret'
RESULT_ERROR =
'error'
PROMPT_CONSUMER_KEY =

prompt keys

'consumer_key'
PROMPT_CONSUMER_SECRET =
'consumer_secret'
PROMPT_PIN =
'pin'
PROMPT_OPEN_URL =
'open_url'
PROMPT_CONSUMER_KEY_DEFAULT =

prompt defaults

'[Step 1 of 4] consumer key: '
PROMPT_CONSUMER_SECRET_DEFAULT =
'[Step 2 of 4] consumer secret: '
PROMPT_OPEN_URL_DEFAULT =
'[Step 3 of 4] open this link in a browser (sign in and authorize): '
PROMPT_PIN_DEFAULT =
'[Step 4 of 4] enter pin: '
VERSION =
'0.1.0'
PROJECT =
'twitter_oauth_token'

Class Method Summary collapse

Class Method Details

.access_token(request_token, pin) ⇒ Object



38
39
40
41
42
43
# File 'lib/twitter_oauth_token/get.rb', line 38

def access_token(request_token, pin)
  request_token.get_access_token(
    :oauth_token => request_token.token,
    :oauth_verifier => pin
  )
end

.authorize_url(request_token) ⇒ Object



34
35
36
# File 'lib/twitter_oauth_token/get.rb', line 34

def authorize_url(request_token)
  request_token.authorize_url
end

.cliObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twitter_oauth_token/cli.rb', line 7

def cli
  puts "#{PROJECT} #{VERSION}"
  puts 'Let\'s get that token in 4 easy steps!'
  results = get()

  error = results[RESULT_ERROR]

  if error.nil?
    access_token = results[RESULT_ACCESS_TOKEN]
    access_token_secret = results[RESULT_ACCESS_TOKEN_SECRET]

    puts "Token: #{access_token}"
    puts "Secret: #{access_token_secret}"
    puts '🎉'
  else
    puts "Error: #{error}"
  end
end

.consumer(consumer_key, consumer_secret) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/twitter_oauth_token/get.rb', line 22

def consumer(consumer_key, consumer_secret)
  OAuth::Consumer.new(
    consumer_key,
    consumer_secret,
    :site => 'https://api.twitter.com'
  )
end

.get(prompts = nil) ⇒ Object



45
46
47
48
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
# File 'lib/twitter_oauth_token/get.rb', line 45

def get(prompts=nil)
  if prompts.nil?
    prompts = {
      PROMPT_CONSUMER_KEY => PROMPT_CONSUMER_KEY_DEFAULT,
      PROMPT_CONSUMER_SECRET => PROMPT_CONSUMER_SECRET_DEFAULT,
      PROMPT_OPEN_URL => PROMPT_OPEN_URL_DEFAULT,
      PROMPT_PIN => PROMPT_PIN_DEFAULT
    }
  end

  begin
    print prompts[PROMPT_CONSUMER_KEY]
    consumer_key = STDIN.gets.strip

    print prompts[PROMPT_CONSUMER_SECRET]
    consumer_secret = STDIN.gets.strip

    c = consumer(consumer_key, consumer_secret)

    request_token = request_token(c)

    url = authorize_url(request_token)
    puts "#{prompts[PROMPT_OPEN_URL]}#{url}"

    print prompts[PROMPT_PIN]
    pin = STDIN.gets.strip

    access_token = access_token(request_token, pin)

    {
      RESULT_ACCESS_TOKEN => access_token.token,
      RESULT_ACCESS_TOKEN_SECRET => access_token.secret,
      RESULT_ERROR => nil
    }
  rescue => e
    {
      RESULT_ACCESS_TOKEN => nil,
      RESULT_ACCESS_TOKEN_SECRET => nil,
      RESULT_ERROR => e
    }
  end # begin
end

.request_token(consumer) ⇒ Object



30
31
32
# File 'lib/twitter_oauth_token/get.rb', line 30

def request_token(consumer)
  consumer.get_request_token
end