Class: Twinfield::Api::OAuthSession

Inherits:
Object
  • Object
show all
Defined in:
lib/twinfield/api/o_auth_session.rb

Constant Summary collapse

HEADER_TEMPLATE =
{
  "Header" => {},
  :attributes! => {
    "Header" => {xmlns: "http://www.twinfield.com/"}
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOAuthSession

sets up a new savon client which will be used for current Session



14
15
16
17
# File 'lib/twinfield/api/o_auth_session.rb', line 14

def initialize
  @cluster = Twinfield.configuration.cluster
  @access_token = Twinfield.configuration.access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



11
12
13
# File 'lib/twinfield/api/o_auth_session.rb', line 11

def access_token
  @access_token
end

#clusterObject

Returns the value of attribute cluster.



11
12
13
# File 'lib/twinfield/api/o_auth_session.rb', line 11

def cluster
  @cluster
end

Instance Method Details

#connected?Boolean

Returns true or false if current session has a session_id and cluster from twinfield

Returns:

  • (Boolean)


43
44
45
# File 'lib/twinfield/api/o_auth_session.rb', line 43

def connected?
  !!@access_token && !!@cluster
end

#headerObject



47
48
49
50
51
52
53
54
55
# File 'lib/twinfield/api/o_auth_session.rb', line 47

def header
  soap_header = HEADER_TEMPLATE

  header_contents = {}
  header_contents["AccessToken"] = access_token if access_token
  header_contents["CompanyCode"] = Twinfield.configuration.company if Twinfield.configuration.company

  soap_header.merge({"Header" => header_contents})
end

#logon(relog = false) ⇒ Object

retrieve a session_id and cluster from twinfield relog is by default set to false so when logon is called on your current session, you wont lose your session_id



22
23
24
# File 'lib/twinfield/api/o_auth_session.rb', line 22

def logon(relog = false)
  # no need to logon with OAuth
end

#relogObject

call logon method with relog set to true this wil destroy the current session and cluster



28
29
30
# File 'lib/twinfield/api/o_auth_session.rb', line 28

def relog
  logon(true)
end

#statusObject

after a logon try you can ask the current status



33
34
35
36
37
38
39
# File 'lib/twinfield/api/o_auth_session.rb', line 33

def status
  if connected?
    "Ok"
  else
    "No access token"
  end
end