Class: Motion::Firebase::TwitterAuthHelper
- Inherits:
-
Object
- Object
- Motion::Firebase::TwitterAuthHelper
- Defined in:
- lib/firebase/firebase_twitter_helper.rb
Instance Method Summary collapse
-
#authenticate_account(account, &firebase_handler) ⇒ Object
Last public facing method.
-
#authenticate_with_twitter_credentials(response_data) ⇒ Object
Step 3 – authenticate with Firebase using Twitter credentials.
- #callback_if_exists_with_error(error) ⇒ Object
-
#create_credential_request_with_reverse_auth_payload(json) ⇒ Object
Step 1b Helper – creates request to Twitter.
-
#initialize(ref, api_key) ⇒ TwitterAuthHelper
constructor
A new instance of TwitterAuthHelper.
-
#make_reverse_request ⇒ Object
Step 1b – get request token from Twitter.
-
#parse_twitter_credentials(response_data) ⇒ Object
Step 3 Helper – parsers credentials into dictionary.
-
#request_twitter_credentials(request) ⇒ Object
Step 2 – request credentials from Twitter.
-
#select_twitter_account(&callback) ⇒ Object
Step 1a – get account.
Constructor Details
#initialize(ref, api_key) ⇒ TwitterAuthHelper
Returns a new instance of TwitterAuthHelper.
30 31 32 33 34 35 36 37 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 30 def initialize(ref, api_key) @store = ACAccountStore.new @ref = ref @api_key = api_key @account = nil @acounts = nil @firebase_callback = nil end |
Instance Method Details
#authenticate_account(account, &firebase_handler) ⇒ Object
Last public facing method
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 73 def authenticate_account(account, &firebase_handler) if !account error = NSError.alloc.initWithDomain('TwitterAuthHelper', code: AuthHelperErrorAccountAccessDenied, userInfo: { NSLocalizedDescriptionKey => 'No Twitter account to authenticate.' }) Dispatch::Queue.main.async do firebase_handler.call(error, nil) end if firebase_handler else @account = account @firebase_callback = firebase_handler make_reverse_request # kick off step 1b end end |
#authenticate_with_twitter_credentials(response_data) ⇒ Object
Step 3 – authenticate with Firebase using Twitter credentials
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 135 def authenticate_with_twitter_credentials(response_data) params = parse_twitter_credentials(response_data) if params['error'] error = NSError.alloc.initWithDomain('TwitterAuthHelper', code: AuthHelperErrorOAuthTokenRequestDenied, userInfo: { NSLocalizedDescriptionKey => 'OAuth token request was denied.', 'details' => params['error']}) callback_if_exists_with_error(error) else @ref.authWithOAuthProvider('twitter', parameters: params, withCompletionBlock: @firebase_callback) end end |
#callback_if_exists_with_error(error) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 88 def callback_if_exists_with_error(error) if @firebase_callback Dispatch::Queue.main.async do @firebase_callback.call(error, nil) end end end |
#create_credential_request_with_reverse_auth_payload(json) ⇒ Object
Step 1b Helper – creates request to Twitter
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 109 def create_credential_request_with_reverse_auth_payload(json) params = {} request_token = json['auth'] params['x_reverse_auth_parameters'] = request_token params['x_reverse_auth_target'] = @api_key url = NSURL.URLWithString('https://api.twitter.com/oauth/access_token') req = SLRequest.requestForServiceType(SLServiceTypeTwitter, requestMethod: SLRequestMethodPOST, URL: url, parameters: params) req.setAccount(@account) req end |
#make_reverse_request ⇒ Object
Step 1b – get request token from Twitter
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 97 def make_reverse_request @ref.makeReverseOAuthRequestTo('twitter', withCompletionBlock: -> (error, json) do if error callback_if_exists_with_error(error) else request = create_credential_request_with_reverse_auth_payload(json) request_twitter_credentials(request) end end) end |
#parse_twitter_credentials(response_data) ⇒ Object
Step 3 Helper – parsers credentials into dictionary
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 149 def parse_twitter_credentials(response_data) account_data = NSString.alloc.initWithData(response_data, encoding:NSUTF8StringEncoding) params = {} account_data.split('&').each do |param| key, value = param.split('=') params[key] = value end # This is super fragile error handling, but basically check that the token and token secret are there. # If not, return the result that Twitter returned. if params['oauth_token_secret'] && params['oauth_token'] params else { 'error' => account_data } end end |
#request_twitter_credentials(request) ⇒ Object
Step 2 – request credentials from Twitter
124 125 126 127 128 129 130 131 132 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 124 def request_twitter_credentials(request) request.performRequestWithHandler(-> (response_data, url_response, error) do if error callback_if_exists_with_error(error) else authenticate_with_twitter_credentials(response_data) end end) end |
#select_twitter_account(&callback) ⇒ Object
Step 1a – get account
40 41 42 43 44 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 |
# File 'lib/firebase/firebase_twitter_helper.rb', line 40 def select_twitter_account(&callback) account_type = @store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter) @store.requestAccessToAccountsWithType(account_type, options: nil, completion: -> (granted, error) do if granted @accounts = @store.accountsWithAccountType(account_type) if @accounts.length > 0 Dispatch::Queue.main.async do next_step = -> (account, &firebase_handler) do self.authenticate_account(account, &firebase_handler) end callback.call(nil, @accounts, next_step) end if callback else error = NSError.alloc.initWithDomain('TwitterAuthHelper', code: AuthHelperErrorAccountAccessDenied, userInfo: { NSLocalizedDescriptionKey => 'No Twitter accounts detected on phone. Please add one in the settings first.' }) Dispatch::Queue.main.async do callback.call(error, nil, nil) end if callback end else error = NSError.alloc.initWithDomain('TwitterAuthHelper', code: AuthHelperErrorAccountAccessDenied, userInfo: { NSLocalizedDescriptionKey => 'Access to twitter accounts denied.' }) Dispatch::Queue.main.async do callback.call(error, nil, nil) end if callback end end) end |