Class: Oauthorizer::Token
- Inherits:
-
Object
- Object
- Oauthorizer::Token
- Includes:
- Capybara::DSL
- Defined in:
- lib/oauthorizer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get_facebook_token_hash ⇒ Object
- #get_foursquare_token_hash ⇒ Object
- #get_google_token_hash ⇒ Object
- #update_from_token_hash(provider_type, token_hash, time_string = nil) ⇒ Object
Class Method Details
.get_config_file ⇒ Object
98 99 100 |
# File 'lib/oauthorizer.rb', line 98 def self.get_config_file YAML.load_file(File.('config/oauthorizer_config.yml')) end |
Instance Method Details
#get_facebook_token_hash ⇒ Object
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') = YAML.load_file(config_file)['facebook'] if ['expires_at'].nil? || ['expires_at'] < Time.now Capybara.default_driver = :selenium Capybara.server_port = ['server_port'] self.visit "https://www.facebook.com/dialog/oauth?" + "client_id=#{['client_id']}" + "&redirect_uri=#{['redirect_uri']}" + "&scope=#{['scope']}" + "&state=#{['state']}" fill_in 'email', with: ['user_email'] fill_in 'pass', with: ['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 end end |
#get_foursquare_token_hash ⇒ Object
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') = YAML.load_file(config_file)['foursquare'] if ['access_token'].nil? Capybara.default_driver = :selenium Capybara.server_port = ['server_port'] self.visit "https://www.foursquare.com/oauth2/authenticate?" + "client_id=#{['client_id']}" + "&redirect_uri=#{['redirect_uri']}" + "&response_type=code" find('.newGreenButton').click fill_in 'username', with: ['user_email'] fill_in 'password', with: ['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 end end |
#get_google_token_hash ⇒ Object
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') = YAML.load_file(config_file)['google'] if ['expires_at'].nil? || ['expires_at'] < Time.now Capybara.default_driver = :selenium Capybara.server_port = ['server_port'] self.visit "https://accounts.google.com/o/oauth2/auth?" + "scope=#{['scope']}" + "&redirect_uri=#{['redirect_uri']}" + "&client_id=#{['client_id']}" + "&response_type=code" + "&approval_prompt=force" + "&access_type=offline" fill_in 'Email', with: ['user_email'] fill_in 'Passwd', with: ['user_password'] 'signIn' 'submit_approve_access' parsed_response = JSON.parse page.text self.update_from_token_hash 'google', parsed_response, 'expires_in' parsed_response else 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 |