Class: OpenAuth2::Client
- Inherits:
-
Object
- Object
- OpenAuth2::Client
- Extended by:
- DelegateToConfig
- Includes:
- Connection
- Defined in:
- lib/open_auth2/client.rb
Overview
Used to make GET/POST requests to OAuth server.
Instance Method Summary collapse
-
#build_code_url(params = {}) ⇒ Object
Examples: client.build_code_url #=> ‘http://…’.
-
#configure {|_self| ... } ⇒ Object
Yields: self, use it to set/change config after #initialize.
-
#get(hash) ⇒ Object
Makes GET request to OAuth server via Faraday.
-
#initialize(config = nil) {|_self| ... } ⇒ Client
constructor
Use it to set @config.
-
#post(hash) ⇒ Object
Makes POST request to OAuth server via Faraday.
-
#run_request(hash) ⇒ Object
Makes request to OAuth server via Faraday#run_request.
-
#token ⇒ Object
We use this to get & refresh access/refresh tokens.
Methods included from DelegateToConfig
Methods included from Connection
Constructor Details
#initialize(config = nil) {|_self| ... } ⇒ Client
Use it to set @config. Will raise error if no @config or wrong it is set right.
Yields: self.
Accepts:
config: (optional) OpenAuth2::Config object
Examples:
config = OpenAuth2::Config.new do |c|
c.provider = :facebook
end
# set via block
OpenAuth2::Client.new do |c|
c.config = config
end
# or pass it as an argument
OpenAuth2::Client.new(config)
Returns: self.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/open_auth2/client.rb', line 32 def initialize(config=nil) @config = config yield self if block_given? raise_config_setter_errors # endpoint is where the api requests are made @faraday_url = endpoint self end |
Instance Method Details
#build_code_url(params = {}) ⇒ Object
Examples:
client.build_code_url
#=> 'http://...'
# or
client.build_code_url(:scope => 'publish_stream')
Accepts:
params: (optional) Hash of additional config to be bundled into
the url.
Returns: String (url).
86 87 88 |
# File 'lib/open_auth2/client.rb', line 86 def build_code_url(params={}) token.build_code_url(params) end |
#configure {|_self| ... } ⇒ Object
Yields: self, use it to set/change config after #initialize. Mainly for setting access_token and refresh_token. Will raise Config related errors same as #initialize.
Examples:
client = OpenAuth2::Client.new
client.configure do |c|
c.access_token = :access_token
c.refresh_token = :refresh_token
end
Returns: self.
58 59 60 61 62 63 |
# File 'lib/open_auth2/client.rb', line 58 def configure yield self raise_config_setter_errors self end |
#get(hash) ⇒ Object
Makes GET request to OAuth server via Faraday. If access_token is available, we pass that along to identify ourselves.
Accepts:
hash
:path - (required)
Examples:
client.get(:path => '/cocacola')
client.get(:path => '/cocacola', :limit => 1)
Returns: Faraday response object.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/open_auth2/client.rb', line 103 def get(hash) connection.get do |conn| path = hash.delete(:path) if path_prefix path = "#{path_prefix}#{path}" end hash.merge!(:access_token => access_token) if access_token conn.url(path, hash) end end |
#post(hash) ⇒ Object
Makes POST request to OAuth server via Faraday.
Accepts:
hash
:path - (required)
:content_type - (optional)
:body - (optional)
Examples:
# using query params (fb uses this)
client.post(:path => "/me/feed?message='From OpenAuth2'")
# using body (google uses this)
body = JSON.dump(:message => "From OpenAuth2)
client.post(:path => "/me/feed,
:body => body,
:content_type => 'application/json')
Returns: Faraday response object.
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/open_auth2/client.rb', line 137 def post(hash) connection.post do |conn| if hash[:content_type] conn.headers["Content-Type"] = hash[:content_type] end conn.url(hash[:path], :access_token => access_token) conn.body = hash[:body] end end |
#run_request(hash) ⇒ Object
Makes request to OAuth server via Faraday#run_request. It takes Hash since I can never remember the order in which to pass the arguments.
Accepts:
hash
:verb - (required) GET/POST etc.
:path - (required)
:body - (optional)
:header - (optional)
Examples:
# public GET request
path = "https://graph.facebook.com/cocacola"
client.run_request(verb: :get, path: path, body: nil, header: nil)
# private GET request
path = "/me/likes?access_token=..."
client.run_request(verb: :get, path: path, body: nil, header: nil)
Returns: Faraday response object.
170 171 172 173 |
# File 'lib/open_auth2/client.rb', line 170 def run_request(hash) connection.run_request(hash[:verb], hash[:path], hash[:body], hash[:header]) end |