Class: IGMarkets::Session
- Inherits:
-
Object
- Object
- IGMarkets::Session
- Defined in:
- lib/ig_markets/session.rb
Overview
Instance Attribute Summary collapse
-
#api_key ⇒ String
The API key to use to authenticate this session.
-
#client_security_token ⇒ String
readonly
The client session security access token for the currently logged in session, or ‘nil` if there is no active session.
-
#log_sinks ⇒ Array<#write>
The array of streams to write log output to.
-
#password ⇒ String
The password to use to authenticate this session.
-
#platform ⇒ :demo, :live
The platform variant to log into for this session.
-
#username ⇒ String
The username to use to authenticate this session.
-
#x_security_token ⇒ String
readonly
The account session security access token for the currently logged in session, or ‘nil` if there is no active session.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Returns whether this session is currently alive and successfully signed in.
-
#delete(url, body = nil, api_version = API_V1) ⇒ Hash
Sends a DELETE request to the IG Markets API.
-
#get(url, api_version = API_V1) ⇒ Hash
Sends a GET request to the IG Markets API.
-
#post(url, body, api_version = API_V1) ⇒ Hash
Sends a POST request to the IG Markets API.
-
#put(url, body = nil, api_version = API_V1) ⇒ Hash
Sends a PUT request to the IG Markets API.
- #sign_in ⇒ Hash
-
#sign_out ⇒ Object
Signs out of IG Markets, ending the current session (if any).
Instance Attribute Details
#api_key ⇒ String
Returns 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_token ⇒ String (readonly)
Returns 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_sinks ⇒ Array<#write>
Returns 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 |
#password ⇒ String
Returns 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.
16 17 18 |
# File 'lib/ig_markets/session.rb', line 16 def platform @platform end |
#username ⇒ String
Returns 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_token ⇒ String (readonly)
Returns 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.
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.
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.
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.
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.
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_in ⇒ Hash
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.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ig_markets/session.rb', line 33 def sign_in @client_security_token = @x_security_token = nil body = { identifier: username, password: password_encryptor.encrypt(password), encryptedPassword: true } sign_in_result = request method: :post, url: 'session', body: body, api_version: API_V2 headers = sign_in_result.fetch(:response).headers @client_security_token = headers.fetch 'CST' @x_security_token = headers.fetch 'X-SECURITY-TOKEN' sign_in_result.fetch :body end |
#sign_out ⇒ Object
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 |