Class: Twurl::OAuthClient
- Inherits:
-
Object
- Object
- Twurl::OAuthClient
- Defined in:
- lib/twurl/oauth_client.rb
Constant Summary collapse
- OAUTH_CLIENT_OPTIONS =
%w[username consumer_key consumer_secret token secret]
- METHODS =
{ :post => Net::HTTP::Post, :get => Net::HTTP::Get, :put => Net::HTTP::Put, :delete => Net::HTTP::Delete, :options => Net::HTTP::Options, :head => Net::HTTP::Head, :copy => Net::HTTP::Copy }
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
- .load_client_for_username(username) ⇒ Object
- .load_client_for_username_and_consumer_key(username, consumer_key) ⇒ Object
- .load_default_client ⇒ Object
- .load_from_options(options) ⇒ Object
- .load_new_client_from_options(options) ⇒ Object
- .rcfile(reload = false) ⇒ Object
Instance Method Summary collapse
- #access_token ⇒ Object
- #authorized? ⇒ Boolean
- #client_auth_parameters ⇒ Object
- #configure_http! ⇒ Object
- #consumer ⇒ Object
- #exchange_credentials_for_access_token ⇒ Object
- #fetch_verify_credentials ⇒ Object
- #generate_authorize_url ⇒ Object
-
#initialize(options = {}) ⇒ OAuthClient
constructor
A new instance of OAuthClient.
- #needs_to_authorize? ⇒ Boolean
- #perform_pin_authorize_workflow ⇒ Object
- #perform_request_from_options(options, &block) ⇒ Object
- #pin_auth_parameters ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
- #verify_has_username ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ OAuthClient
Returns a new instance of OAuthClient.
55 56 57 58 59 60 61 62 63 |
# File 'lib/twurl/oauth_client.rb', line 55 def initialize( = {}) @username = ['username'] @password = ['password'] @consumer_key = ['consumer_key'] @consumer_secret = ['consumer_secret'] @token = ['token'] @secret = ['secret'] configure_http! end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
54 55 56 |
# File 'lib/twurl/oauth_client.rb', line 54 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
54 55 56 |
# File 'lib/twurl/oauth_client.rb', line 54 def username @username end |
Class Method Details
.load_client_for_username(username) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/twurl/oauth_client.rb', line 30 def load_client_for_username(username) if user_profiles = rcfile[username] if user_profiles.values.size == 1 new(user_profiles.values.first) else raise Exception, "There is more than one consumer key associated with #{username}. Please specify which consumer key you want as well." end else raise Exception, "No profile for #{username}" end end |
.load_client_for_username_and_consumer_key(username, consumer_key) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/twurl/oauth_client.rb', line 21 def load_client_for_username_and_consumer_key(username, consumer_key) user_profiles = rcfile[username] if user_profiles && attributes = user_profiles[consumer_key] new(attributes) else raise Exception, "No profile for #{username}" end end |
.load_default_client ⇒ Object
46 47 48 49 |
# File 'lib/twurl/oauth_client.rb', line 46 def load_default_client raise Exception, "You must authorize first" unless rcfile.default_profile load_client_for_username_and_consumer_key(*rcfile.default_profile) end |
.load_from_options(options) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/twurl/oauth_client.rb', line 11 def () if rcfile.has_oauth_profile_for_username_with_consumer_key?(.username, .consumer_key) load_client_for_username_and_consumer_key(.username, .consumer_key) elsif .username || (.command == 'authorize') () else load_default_client end end |
.load_new_client_from_options(options) ⇒ Object
42 43 44 |
# File 'lib/twurl/oauth_client.rb', line 42 def () new(..merge('password' => .password)) end |
Instance Method Details
#access_token ⇒ Object
172 173 174 |
# File 'lib/twurl/oauth_client.rb', line 172 def access_token @access_token ||= OAuth::AccessToken.new(consumer, token, secret) end |
#authorized? ⇒ Boolean
123 124 125 126 |
# File 'lib/twurl/oauth_client.rb', line 123 def oauth_response = fetch_verify_credentials oauth_response.class == Net::HTTPOK end |
#client_auth_parameters ⇒ Object
93 94 95 |
# File 'lib/twurl/oauth_client.rb', line 93 def client_auth_parameters {'x_auth_username' => username, 'x_auth_password' => password, 'x_auth_mode' => 'client_auth'} end |
#configure_http! ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/twurl/oauth_client.rb', line 154 def configure_http! consumer.http.set_debug_output(Twurl..debug_output_io) if Twurl..trace if Twurl..ssl? consumer.http.use_ssl = true consumer.http.verify_mode = OpenSSL::SSL::VERIFY_NONE end end |
#consumer ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/twurl/oauth_client.rb', line 162 def consumer @consumer ||= OAuth::Consumer.new( consumer_key, consumer_secret, :site => Twurl..base_url, :proxy => Twurl..proxy ) end |
#exchange_credentials_for_access_token ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/twurl/oauth_client.rb', line 83 def exchange_credentials_for_access_token response = begin consumer.token_request(:post, consumer.access_token_path, nil, {}, client_auth_parameters) rescue OAuth::Unauthorized end @token = response[:oauth_token] @secret = response[:oauth_token_secret] end |
#fetch_verify_credentials ⇒ Object
119 120 121 |
# File 'lib/twurl/oauth_client.rb', line 119 def fetch_verify_credentials access_token.get('/1.1/account/verify_credentials.json?include_entities=false&skip_status=true') end |
#generate_authorize_url ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/twurl/oauth_client.rb', line 105 def request = consumer.create_signed_request(:get, consumer., @request_token, pin_auth_parameters) params = request['Authorization'].sub(/^OAuth\s+/, '').split(/,\s+/).map { |p| k, v = p.split('=') v =~ /"(.*?)"/ "#{k}=#{CGI::escape($1)}" }.join('&') "#{Twurl..base_url}#{request.path}?#{params}" end |
#needs_to_authorize? ⇒ Boolean
128 129 130 |
# File 'lib/twurl/oauth_client.rb', line 128 def token.nil? || secret.nil? end |
#perform_pin_authorize_workflow ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/twurl/oauth_client.rb', line 97 def @request_token = consumer.get_request_token CLI.puts("Go to #{} and paste in the supplied PIN") pin = gets access_token = @request_token.get_access_token(:oauth_verifier => pin.chomp) {:oauth_token => access_token.token, :oauth_token_secret => access_token.secret} end |
#perform_request_from_options(options, &block) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/twurl/oauth_client.rb', line 75 def (, &block) request_class = METHODS.fetch(.request_method.to_sym) request = request_class.new(.path, .headers) request.set_form_data(.data) if .data request.oauth!(consumer.http, consumer, access_token) consumer.http.request(request, &block) end |
#pin_auth_parameters ⇒ Object
115 116 117 |
# File 'lib/twurl/oauth_client.rb', line 115 def pin_auth_parameters {'oauth_callback' => 'oob'} end |
#save ⇒ Object
132 133 134 135 |
# File 'lib/twurl/oauth_client.rb', line 132 def save verify_has_username self.class.rcfile << self end |
#to_hash ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/twurl/oauth_client.rb', line 145 def to_hash OAUTH_CLIENT_OPTIONS.inject({}) do |hash, attribute| if value = send(attribute) hash[attribute] = value end hash end end |
#verify_has_username ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/twurl/oauth_client.rb', line 137 def verify_has_username if username.nil? || username == '' oauth_response = fetch_verify_credentials oauth_response.body =~ /"screen_name"\s*:\s*"(.*?)"/ @username = $1 end end |