Class: FellowshipOneAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/f1api/client.rb

Overview

The Fellowship One API client class

Takes an HTTP request and passes it through the OAuth library which added the approperiate headers and querystring parameters.

Examples

Simple client using OAuth and default YAML config values

client = FellowshipOneAPI::Client.new

client.authorize!

client.request(:get, ‘/v1/People/123.xml’)

Using credentials based authentication (2nd party)

client = FellowshipOneAPI::Client.new(=> :credentials)

client.authorize!(“username”, “password”)

client.request(:get, ‘/v1/People/123.xml’)

Authenticating against weblink passing credentials

client = FellowshipOneAPI::Client.new(=> :credentials, :auth_against => :weblink)

client.authorize(“weblinkuser”, “weblinkpassword”)

client.request(:get, ‘/v1/People/123.xml’)

Loading a client with an existing access token

client = FellowshipOneAPI::Client.new(=> “123456”, :oauth_token_secret => “987654”)

client.request(:get, ‘/v1/People/123.xml’)

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Creates a new instance of a client used to connect with the Fellowship One API The client can be configured with the following symbols:

:auth_type
  • Can be :credentials or :oauth (:oauth is the default)

:auth_against
  • Can be :portal or :weblink (:portal is the default)

:oauth_token
  • The access token

:oauth_token_secret
  • The access token secret



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/f1api/client.rb', line 40

def initialize(args = {})
  args[:auth_type] ||= Configuration.authentication_type.to_sym
  if args[:auth_type] == :credentials
    require "#{File.dirname(__FILE__)}/oauth/credentials_authentication"
    extend OAuth::CredentialsAuthentication
  else
    require "#{File.dirname(__FILE__)}/oauth/oauth_authentication"
    extend OAuth::OAuthAuthentication
  end

  if(args[:auth_against])
    load_consumer_config args[:auth_against], args[:site_url]
  else
    load_consumer_config :portal, args[:site_url]
  end

  if(args[:oauth_token] and args[:oauth_token_secret])
    @oauth_access_token = ::OAuth::AccessToken.from_hash(@oauth_consumer, args[:oauth_token], args[:oauth_token_secret])
  end

  if(args[:auth_username] and args[:auth_password])
    authorize! args[:auth_username], args[:auth_password]
  end
end

Instance Method Details

#current_userObject

Lazy loads the user record you’ve logged in as



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/f1api/client.rb', line 66

def current_user
  if @current_user.nil?
    req = request(:get, "#{@authenticated_user_uri}.json")
    if req.code.to_i == 200
      @current_user = JSON.parse(req.body)["person"]
    else
      raise "Non HTTP 200 on current_user load"
    end
  end
  @current_user
end

#request(*args) ⇒ Object

Passes through the request to the OAuth library to be signed and set out HTTP



79
80
81
# File 'lib/f1api/client.rb', line 79

def request(*args)
  @oauth_access_token.request(*args)
end