Class: OpenAuth2::Client
- Inherits:
-
Object
- Object
- OpenAuth2::Client
- Extended by:
- DelegateToConfig
- Defined in:
- lib/open_auth2/client.rb
Overview
Makes GET/POST requests to OAuth server.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#faraday_url ⇒ Object
Returns the value of attribute faraday_url.
Instance Method Summary collapse
-
#build_code_url(params = {}) ⇒ Object
Examples: client.build_code_url #=> ‘http://…’.
-
#configure {|_self| ... } ⇒ Object
Use to set/change config after #initialize.
-
#connection(&blk) ⇒ Object
Yields: Faraday object, so user can choose choose their own middleware.
-
#get(hash) ⇒ Object
Makes GET request to OAuth server.
-
#initialize(config = nil) {|_self| ... } ⇒ Client
constructor
Use to set config.
-
#post(hash) ⇒ Object
Makes POST request to OAuth server.
-
#run_request(hash) ⇒ Object
Makes request to OAuth server via Faraday#run_request.
-
#token ⇒ Object
Use this to get & refresh access/refresh tokens.
Methods included from DelegateToConfig
Constructor Details
#initialize(config = nil) {|_self| ... } ⇒ Client
27 28 29 30 31 |
# File 'lib/open_auth2/client.rb', line 27 def initialize(config=nil) @config = config yield self if block_given? @faraday_url = endpoint end |
Instance Attribute Details
#faraday_url ⇒ Object
Returns the value of attribute faraday_url.
7 8 9 |
# File 'lib/open_auth2/client.rb', line 7 def faraday_url @faraday_url 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).
70 71 72 |
# File 'lib/open_auth2/client.rb', line 70 def build_code_url(params={}) token.build_code_url(params) end |
#configure {|_self| ... } ⇒ Object
45 46 47 |
# File 'lib/open_auth2/client.rb', line 45 def configure yield self if block_given? end |
#connection(&blk) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/open_auth2/client.rb', line 174 def connection(&blk) @connection ||= Faraday.new(:url => @faraday_url) do |builder| builder.request :url_encoded builder.adapter :net_http builder.instance_eval(&blk) if block_given? end @connection end |
#get(hash) ⇒ Object
Makes GET request to OAuth server. 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.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/open_auth2/client.rb', line 87 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.
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.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/open_auth2/client.rb', line 121 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.
156 157 158 159 |
# File 'lib/open_auth2/client.rb', line 156 def run_request(hash) connection.run_request(hash[:verb], hash[:path], hash[:body], hash[:header]) end |