Module: ElvantoAPI

Defined in:
lib/elvanto.rb,
lib/elvanto/error.rb,
lib/elvanto/pager.rb,
lib/elvanto/utils.rb,
lib/elvanto/client.rb,
lib/elvanto/version.rb,
lib/elvanto/resources/resource.rb

Defined Under Namespace

Modules: Resource, Utils Classes: BadRequest, Client, Error, InternalError, NotFound, Pager, StandardError, Unauthorized

Constant Summary collapse

CLASS_MAPPING =
{}
VERSION =
"1.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

Returns the value of attribute client.



28
29
30
# File 'lib/elvanto.rb', line 28

def client
  @client
end

.configObject

Returns the value of attribute config.



29
30
31
# File 'lib/elvanto.rb', line 29

def config
  @config
end

.tokensObject

Returns the value of attribute tokens.



30
31
32
# File 'lib/elvanto.rb', line 30

def tokens
  @tokens
end

Class Method Details

.authorize_url(client_id, redirect_uri, scope = "AdministerAccount", state = nil) ⇒ String

Returns The authorization URL to which users of your application should be redirected.

Returns:

  • (String)

    The authorization URL to which users of your application should be redirected.



49
50
51
52
53
54
55
56
57
58
# File 'lib/elvanto.rb', line 49

def authorize_url(client_id, redirect_uri, scope="AdministerAccount", state=nil)
  
  scope = scope.join(",") if scope.class == Array

  params = {type: "web_server", client_id: client_id, redirect_uri: redirect_uri, scope: scope}
  params[:state] = state if state

  uri = Addressable::URI.new({:host => config[:host],:scheme => config[:scheme], :path => "oauth",:query_values => params})
  return uri.to_s
end

.call(endpoint, options = {}) ⇒ Object

Parameters:

  • enpoint (String)

    The name of endpoint, for example: “people/getAll” or “groups/GetInfo”

  • option (Hash)

    List of parametrs



91
92
93
94
# File 'lib/elvanto.rb', line 91

def call(endpoint, options={})
  response = post endpoint, options
  return response.body
end

.configure(options = {}) ⇒ Object

In order to authenticate with access token: options = “access_token”, … In order to authenticate with API key: options = “api_key”, …



36
37
38
39
40
# File 'lib/elvanto.rb', line 36

def configure(options={})
  @config = @config.merge(options)
  @config[:access_token] ||= tokens[:access_token]
  @client = ElvantoAPI::Client.new(@config)
end

.exchange_token(client_id, client_secret, code, redirect_uri) ⇒ Hash

Returns The hash with keys ‘access_token’, ‘expires_in’, and ‘refresh_token’.

Parameters:

  • client_secret (String)

    The Client Secret of your registered OAuth application.

  • code (String)

    The unique OAuth code to be exchanged for an access token.

  • redirect_url (String)

    The Redirect URI of your registered OAuth application.

Returns:

  • (Hash)

    The hash with keys ‘access_token’, ‘expires_in’, and ‘refresh_token’



65
66
67
68
69
70
# File 'lib/elvanto.rb', line 65

def exchange_token(client_id, client_secret, code, redirect_uri)
  params = {grant_type: 'authorization_code', client_id: client_id, client_secret: client_secret, code: code, redirect_uri: redirect_uri}
  response = Faraday.new(client.url).post "oauth/token", params
  @tokens = JSON.parse(response.body, :symbolize_keys => true)
  return @tokens
end

.post(href, options = {}) ⇒ Object



84
85
86
# File 'lib/elvanto.rb', line 84

def post(href, options={})
  self.client.post href, options
end

.refresh_token(token = nil) ⇒ Hash

Returns The hash with keys ‘access_token’, ‘expires_in’, and ‘refresh_token’.

Parameters:

  • refresh_token (String)

    Was included when the original token was granted to automatically retrieve a new access token.

Returns:

  • (Hash)

    The hash with keys ‘access_token’, ‘expires_in’, and ‘refresh_token’



74
75
76
77
78
79
80
81
82
# File 'lib/elvanto.rb', line 74

def refresh_token(token=nil)
  token ||= tokens[:refresh_token]
  raise "Error refreshing token. There is no refresh token set on this object" unless token

  params = {grant_type: "refresh_token", refresh_token: token}
  response = Faraday.new(client.url).post "oauth/token", params
  @tokens = JSON.parse(response.body,:symbolize_keys => true)
  return @tokens
end