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


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

def api_key
  @api_key
end

#client_security_tokenString (readonly)


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

def client_security_token
  @client_security_token
end

#passwordString


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

def password
  @password
end

#platform:demo, :live


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

def platform
  @platform
end

#usernameString


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

def username
  @username
end

#x_security_tokenString (readonly)


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.


55
56
57
# File 'lib/ig_markets/session.rb', line 55

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.


98
99
100
# File 'lib/ig_markets/session.rb', line 98

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.


76
77
78
# File 'lib/ig_markets/session.rb', line 76

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.


66
67
68
# File 'lib/ig_markets/session.rb', line 66

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.


87
88
89
# File 'lib/ig_markets/session.rb', line 87

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.


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ig_markets/session.rb', line 30

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.


46
47
48
49
50
# File 'lib/ig_markets/session.rb', line 46

def sign_out
  delete 'session' if alive?

  @client_security_token = @x_security_token = nil
end