Class: GSimpleApi::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/g_simple_api/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, access_token = nil) ⇒ Manager

Returns a new instance of Manager.



19
20
21
22
23
# File 'lib/g_simple_api/manager.rb', line 19

def initialize(settings, access_token = nil)
    @settings = settings
    @authorizer = GSimpleApi::Authorizer.new(settings)
    @authorizer.access_token = access_token if access_token
end

Instance Attribute Details

#authorizerObject (readonly)

Returns the value of attribute authorizer.



17
18
19
# File 'lib/g_simple_api/manager.rb', line 17

def authorizer
  @authorizer
end

#settingsObject (readonly)

Returns the value of attribute settings.



17
18
19
# File 'lib/g_simple_api/manager.rb', line 17

def settings
  @settings
end

Instance Method Details

#authorize_url(callback_url = nil) ⇒ String

Builds an URL that will be used by users to allow access to the apis.

Parameters:

  • callback_url (String) (defaults to: nil)

    the URL that the user will be redirected back after approve or reject access to the api.

Returns:

  • (String)

    the complete URL which the users need to be send to.



29
30
31
32
# File 'lib/g_simple_api/manager.rb', line 29

def authorize_url(callback_url=nil)
    @authorizer.callback=(callback_url)
    @authorizer.authorization.authorization_uri.to_s
end

#execute(action, parameters = {}) ⇒ Object

Executes call for the loaded api.

Parameters:

  • action (String)

    the resource that will be called.

  • parameters (Hash) (defaults to: {})

    the parameters associated with the action.

Returns:

  • an object that contains the result of the call.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/g_simple_api/manager.rb', line 53

def execute(action, parameters = {})
    methods = split_method(action)
    discovered_api = settings.discovered_api
    api_client = settings.api_client

    params = {
        :api_method => chained_method_calls(methods, discovered_api),
        :parameters => parameters
    }

    api_client.execute(params).data
end

#get_token(code, refresh_token = false) ⇒ Hash

Processes code param and returns a valid access_token.

Parameters:

  • code (String)

    the code returned from #authorize_url or a previous stored refresh_token if refresh_token was true.

  • refresh_token (Boolean) (defaults to: false)

    indicates if code is a refresh_token or a code from first access.

Returns:

  • (Hash)

    with all required data for the api access.



39
40
41
42
43
44
45
46
47
# File 'lib/g_simple_api/manager.rb', line 39

def get_token(code, refresh_token=false)
    if refresh_token
        authorizer.refresh_token=(code)
    else
        authorizer.code= code
    end

    authorizer.authorization.fetch_access_token!
end