Class: IGMarkets::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/session.rb

Overview

Manages a session with the IG Markets REST API, including signing in, signing out, and the sending of requests. In order to sign in, #username, #password, #api_key and #platform must be set. #platform must be either :demo or :production depending on which platform is being targeted.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyString

Returns The API key to use to authenticate this session.

Returns:

  • (String)

    The API key to use to authenticate this session.



13
14
15
# File 'lib/ig_markets/session.rb', line 13

def api_key
  @api_key
end

#cstString (readonly)

Returns The CST for the currently logged in session, or nil if there is no active session.

Returns:

  • (String)

    The CST for the currently logged in session, or nil if there is no active session.



19
20
21
# File 'lib/ig_markets/session.rb', line 19

def cst
  @cst
end

#passwordString

Returns The password to use to authenticate this session.

Returns:

  • (String)

    The password to use to authenticate this session.



10
11
12
# File 'lib/ig_markets/session.rb', line 10

def password
  @password
end

#platform:demo, :production

Returns The platform variant to log into for this session.

Returns:

  • (:demo, :production)

    The platform variant to log into for this session.



16
17
18
# File 'lib/ig_markets/session.rb', line 16

def platform
  @platform
end

#usernameString

Returns The username to use to authenticate this session.

Returns:

  • (String)

    The username to use to authenticate this session.



7
8
9
# File 'lib/ig_markets/session.rb', line 7

def username
  @username
end

#x_security_tokenString (readonly)

Returns The security token for the currently logged in session, or nil if there is no active session.

Returns:

  • (String)

    The security token for the currently logged in session, or nil if there is no active session.



22
23
24
# File 'lib/ig_markets/session.rb', line 22

def x_security_token
  @x_security_token
end

Instance Method Details

#alive?Boolean

Returns whether this session is currently alive and successfully signed in.

Returns:



51
52
53
# File 'lib/ig_markets/session.rb', line 51

def alive?
  !cst.nil? && !x_security_token.nil?
end

#delete(url, payload, api_version) ⇒ Hash

Sends a DELETE request to the IG Markets API. If an error occurs then RequestFailedError will be raised.

Parameters:

  • url (String)

    The URL to send the DELETE request to.

  • payload (nil, String, Hash)

    The payload to include with the DELETE request, this will be encoded as JSON.

  • api_version (Fixnum)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



94
95
96
# File 'lib/ig_markets/session.rb', line 94

def delete(url, payload, api_version)
  request(method: :delete, url: url, payload: payload, api_version: api_version).fetch :result
end

#get(url, api_version) ⇒ Hash

Sends a GET request to the IG Markets API. If an error occurs then RequestFailedError will be raised.

Parameters:

  • url (String)

    The URL to send the GET request to.

  • api_version (Fixnum)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



72
73
74
# File 'lib/ig_markets/session.rb', line 72

def get(url, api_version)
  request(method: :get, url: url, api_version: api_version).fetch :result
end

#inspectString

Returns a human-readable string containing this session’s details.

Returns:

  • (String)


101
102
103
# File 'lib/ig_markets/session.rb', line 101

def inspect
  "#<#{self.class.name} #{cst}, #{x_security_token}>"
end

#post(url, payload, api_version) ⇒ Hash

Sends a POST request to the IG Markets API. If an error occurs then RequestFailedError will be raised.

Parameters:

  • url (String)

    The URL to send the POST request to.

  • payload (nil, String, Hash)

    The payload to include with the POST request, this will be encoded as JSON.

  • api_version (Fixnum)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



62
63
64
# File 'lib/ig_markets/session.rb', line 62

def post(url, payload, api_version)
  request(method: :post, url: url, payload: payload, api_version: api_version).fetch :result
end

#put(url, payload, api_version) ⇒ Hash

Sends a PUT request to the IG Markets API. If an error occurs then RequestFailedError will be raised.

Parameters:

  • url (String)

    The URL to send the PUT request to.

  • payload (nil, String, Hash)

    The payload to include with the PUT request, this will be encoded as JSON.

  • api_version (Fixnum)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



83
84
85
# File 'lib/ig_markets/session.rb', line 83

def put(url, payload, api_version)
  request(method: :put, url: url, payload: payload, api_version: api_version).fetch :result
end

#sign_inObject

Signs in to IG Markets using the values of #username, #password, #api_key and #platform. If an error occurs then RequestFailedError will be raised.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ig_markets/session.rb', line 26

def 
  validate_authentication

  payload = { identifier: username, password: password_encryptor.encrypt(password), encryptedPassword: true }

   = request method: :post, url: 'session', payload: payload, api_version: API_V1

  headers = .fetch(:response).headers
  @cst = headers.fetch :cst
  @x_security_token = headers.fetch :x_security_token

  nil
end

#sign_outObject

Signs out of IG Markets, ending the current session (if any). If an error occurs then RequestFailedError will be raised.



42
43
44
45
46
# File 'lib/ig_markets/session.rb', line 42

def sign_out
  delete 'session', nil, API_V1 if alive?

  @cst = @x_security_token = nil
end