Class: OpenAuth2::Client

Inherits:
Object
  • Object
show all
Extended by:
DelegateToConfig
Includes:
Connection
Defined in:
lib/open_auth2/client.rb

Overview

Makes GET/POST requests to OAuth server.

Instance Method Summary collapse

Methods included from DelegateToConfig

delegate_to_config, extended

Methods included from Connection

#connection, included

Constructor Details

#initialize(config = nil) {|_self| ... } ⇒ Client

Use to set config.

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)

Yields:

  • (_self)

Yield Parameters:



26
27
28
29
30
# File 'lib/open_auth2/client.rb', line 26

def initialize(config=nil)
  @config = config
  yield self if block_given?
  @faraday_url = endpoint
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).



69
70
71
# File 'lib/open_auth2/client.rb', line 69

def build_code_url(params={})
  token.build_code_url(params)
end

#configure {|_self| ... } ⇒ Object

Use to set/change config after #initialize.

Examples:

client = OpenAuth2::Client.new

client.configure do |c|
  c.access_token  = :access_token
  c.refresh_token = :refresh_token
end

Returns: self.

Yields:

  • (_self)

Yield Parameters:



44
45
46
# File 'lib/open_auth2/client.rb', line 44

def configure
  yield self if block_given?
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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/open_auth2/client.rb', line 86

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.



120
121
122
123
124
125
126
127
128
129
# File 'lib/open_auth2/client.rb', line 120

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.



155
156
157
158
# File 'lib/open_auth2/client.rb', line 155

def run_request(hash)
  connection.run_request(hash[:verb], hash[:path], hash[:body],
                         hash[:header])
end

#tokenObject

Use this to get & refresh access/refresh tokens.

Returns: Token object.



52
53
54
# File 'lib/open_auth2/client.rb', line 52

def token
  @token ||= Token.new(config)
end