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 `:live` 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

#client_security_tokenString (readonly)

Returns the client session security access token for the currently logged in session, or ‘nil` if there is no active session.

Returns:

  • (String)

    the client session security access token for the currently logged in session, or ‘nil` if there is no active session.



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

def client_security_token
  @client_security_token
end

#log_sinksArray<#write>

Returns the array of streams to write log output to.

Returns:

  • (Array<#write>)

    the array of streams to write log output to.



27
28
29
# File 'lib/ig_markets/session.rb', line 27

def log_sinks
  @log_sinks
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, :live

Returns The platform variant to log into for this session.

Returns:

  • (:demo, :live)

    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 account session security access token for the currently logged in session, or ‘nil` if there is no active session.

Returns:

  • (String)

    the account session security access token for the currently logged in session, or ‘nil` if there is no active session.



24
25
26
# File 'lib/ig_markets/session.rb', line 24

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:



58
59
60
# File 'lib/ig_markets/session.rb', line 58

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

#delete(url, body = nil, api_version = API_V1) ⇒ Hash

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

Parameters:

  • url (String)

    The URL to send the DELETE request to.

  • body (Hash, nil) (defaults to: nil)

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

  • api_version (Integer) (defaults to: API_V1)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



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

def delete(url, body = nil, api_version = API_V1)
  request(method: :delete, url: url, body: body, api_version: api_version).fetch :body
end

#get(url, api_version = API_V1) ⇒ Hash

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

Parameters:

  • url (String)

    The URL to send the GET request to.

  • api_version (Integer) (defaults to: API_V1)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



79
80
81
# File 'lib/ig_markets/session.rb', line 79

def get(url, api_version = API_V1)
  request(method: :get, url: url, api_version: api_version).fetch :body
end

#post(url, body, api_version = API_V1) ⇒ Hash

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

Parameters:

  • url (String)

    The URL to send the POST request to.

  • body (Hash, nil)

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

  • api_version (Integer) (defaults to: API_V1)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



69
70
71
# File 'lib/ig_markets/session.rb', line 69

def post(url, body, api_version = API_V1)
  request(method: :post, url: url, body: body, api_version: api_version).fetch :body
end

#put(url, body = nil, api_version = API_V1) ⇒ Hash

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

Parameters:

  • url (String)

    The URL to send the PUT request to.

  • body (Hash, nil) (defaults to: nil)

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

  • api_version (Integer) (defaults to: API_V1)

    The API version to target.

Returns:

  • (Hash)

    The response from the IG Markets API.



90
91
92
# File 'lib/ig_markets/session.rb', line 90

def put(url, body = nil, api_version = API_V1)
  request(method: :put, url: url, body: body, api_version: api_version).fetch :body
end

#sign_inHash

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

Returns:

  • (Hash)

    The data returned in the body of the sign in request.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ig_markets/session.rb', line 33

def 
  @client_security_token = @x_security_token = nil

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

   = request method: :post, url: 'session', body: body, api_version: API_V2

  headers = .fetch(:response).headers
  @client_security_token = headers.fetch 'CST'
  @x_security_token = headers.fetch 'X-SECURITY-TOKEN'

  .fetch :body
end

#sign_outObject

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



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

def sign_out
  delete 'session' if alive?

  @client_security_token = @x_security_token = nil
end