Class: Oauthorizer::Token

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/oauthorizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_config_fileObject



98
99
100
# File 'lib/oauthorizer.rb', line 98

def self.get_config_file
  YAML.load_file(File.expand_path('config/oauthorizer_config.yml'))
end

Instance Method Details

#get_facebook_token_hashObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oauthorizer.rb', line 36

def get_facebook_token_hash
  config_file             = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
  oauthorizer_keys        = YAML.load_file(config_file)['facebook']
  if oauthorizer_keys['expires_at'].nil? || oauthorizer_keys['expires_at'] < Time.now
    Capybara.default_driver = :selenium
    Capybara.server_port    = oauthorizer_keys['server_port']
    self.visit "https://www.facebook.com/dialog/oauth?" +
      "client_id=#{oauthorizer_keys['client_id']}" +
      "&redirect_uri=#{oauthorizer_keys['redirect_uri']}" +
      "&scope=#{oauthorizer_keys['scope']}" +
      "&state=#{oauthorizer_keys['state']}"
   fill_in 'email', with: oauthorizer_keys['user_email']
   fill_in 'pass', with: oauthorizer_keys['user_password']
   find('#loginbutton').click
   find('#grant_required_clicked').click  if page.has_css?('#grant_required_clicked')
   find('#grant_clicked').click           if page.has_css?('#grant_clicked')

   parsed_response = JSON.parse page.text
   self.update_from_token_hash 'facebook', parsed_response, 'expires'

   parsed_response
  else
   oauthorizer_keys
  end
end

#get_foursquare_token_hashObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/oauthorizer.rb', line 62

def get_foursquare_token_hash
  config_file             = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
  oauthorizer_keys        = YAML.load_file(config_file)['foursquare']
  if oauthorizer_keys['access_token'].nil? 
    Capybara.default_driver = :selenium
    Capybara.server_port    = oauthorizer_keys['server_port']
    self.visit "https://www.foursquare.com/oauth2/authenticate?" +
      "client_id=#{oauthorizer_keys['client_id']}" +
      "&redirect_uri=#{oauthorizer_keys['redirect_uri']}" +
      "&response_type=code" 
    find('.newGreenButton').click
    fill_in 'username', with: oauthorizer_keys['user_email']
    fill_in 'password', with: oauthorizer_keys['user_password']
    find('.greenButton').click
    # find('.newGreenButton').click

    parsed_response = JSON.parse page.text
    self.update_from_token_hash 'foursquare', parsed_response

    parsed_response
  else
    oauthorizer_keys
  end
end

#get_google_token_hashObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/oauthorizer.rb', line 9

def get_google_token_hash
  config_file             = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
  oauthorizer_keys        = YAML.load_file(config_file)['google']
  if oauthorizer_keys['expires_at'].nil? || oauthorizer_keys['expires_at'] < Time.now
    Capybara.default_driver = :selenium
    Capybara.server_port    = oauthorizer_keys['server_port']
    self.visit "https://accounts.google.com/o/oauth2/auth?" +
      "scope=#{oauthorizer_keys['scope']}" + 
      "&redirect_uri=#{oauthorizer_keys['redirect_uri']}" +
      "&client_id=#{oauthorizer_keys['client_id']}" + 
      "&response_type=code" + 
      "&approval_prompt=force" + 
      "&access_type=offline"
    fill_in 'Email', with: oauthorizer_keys['user_email']
    fill_in 'Passwd', with: oauthorizer_keys['user_password']
    click_button 'signIn'
    click_button 'submit_approve_access'
   
    parsed_response = JSON.parse page.text 
    self.update_from_token_hash 'google', parsed_response, 'expires_in'
   
    parsed_response
  else
    oauthorizer_keys
  end
end

#update_from_token_hash(provider_type, token_hash, time_string = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/oauthorizer.rb', line 87

def update_from_token_hash provider_type, token_hash, time_string=nil
  config_file = File.join(Rails.root, 'config', 'oauthorizer_config.yml')
  credentials ||= YAML.load_file(config_file)
  credentials[provider_type].merge!(token_hash)
  credentials[provider_type].merge!('expires_at' => (Time.now + token_hash[time_string].to_i.seconds)) unless time_string.nil?

  new_file = File.open(config_file, 'w+')
  new_file.write(credentials.to_yaml.gsub("---\n", ''))
  new_file.close
end