Module: IDeleteMyTweets::Auth

Included in:
CommandConfig
Defined in:
lib/i_delete_my_tweets/auth.rb

Instance Method Summary collapse

Instance Method Details

#build_headers(request) ⇒ Object



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

def build_headers(request)
  request["Authorization"].sub(/^OAuth\s+/, "").split(/,\s+/).map { |p|
    k, v = p.split("=")
    v =~ /"(.*?)"/
    "#{k}=#{CGI.escape($1)}"
  }.join("&")
end

#build_path(request, params) ⇒ Object



38
39
40
# File 'lib/i_delete_my_tweets/auth.rb', line 38

def build_path(request, params)
  "#{Twitter::REST::Request::BASE_URL}#{request.path}?#{params}"
end

#consumer(client) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/i_delete_my_tweets/auth.rb', line 5

def consumer(client)
  OAuth::Consumer.new(
    client.consumer_key,
    client.consumer_secret,
    site: Twitter::REST::Request::BASE_URL,
  )
end

#generate_authorize_url(client, request_token) ⇒ Object



32
33
34
35
36
# File 'lib/i_delete_my_tweets/auth.rb', line 32

def generate_authorize_url(client, request_token)
  oauth = consumer(client)
  request = oauth.create_signed_request(:get, oauth.authorize_path, request_token, pin_auth_parameters)
  build_path(request, build_headers(request))
end

#get_access_credentials(request_token, pin) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/i_delete_my_tweets/auth.rb', line 23

def get_access_credentials(request_token, pin)
  access_token = request_token.get_access_token(oauth_verifier: pin.chomp)
  {oauth_token: access_token.token,
   oauth_token_secret: access_token.secret,
   screen_name: access_token.params[:screen_name]}
rescue StandardError => e
  say_error set_color " 🚫 Oops, something bad happened: #{e.message} ", :white, :on_red, :bold
end

#get_request_token(client) ⇒ Object



17
18
19
20
21
# File 'lib/i_delete_my_tweets/auth.rb', line 17

def get_request_token(client)
  consumer(client).get_request_token
rescue StandardError => e
  say_error set_color " 🚫 Oops, something bad happened: #{e.message} ", :white, :on_red, :bold
end

#pin_auth_parametersObject



13
14
15
# File 'lib/i_delete_my_tweets/auth.rb', line 13

def pin_auth_parameters
  {oauth_callback: "oob"}
end