Class: OpenAuth2::Client

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

Overview

Makes GET/POST requests to OAuth server.

Direct Known Subclasses

Token

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DelegateToConfig

delegate_to_config, extended

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:



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_urlObject

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

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:



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

def configure
  yield self if block_given?
end

#connection(&blk) ⇒ Object

Yields: Faraday object, so user can choose choose their own middleware.

Examples:

config = OpenAuth2::Config.new
client = OpenAuth2::Client.new(config)

client.connection do
  response :logger
end

Returns: Faraday 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

#tokenObject

Use this to get & refresh access/refresh tokens.

Returns: Token object.



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

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