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.
-
#cst ⇒ String
readonly
The CST for the currently logged in session, or nil if there is no active session.
-
#password ⇒ String
The password to use to authenticate this session.
-
#platform ⇒ :demo, :production
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 security 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, payload, api_version) ⇒ Hash
Sends a DELETE request to the IG Markets API.
-
#get(url, api_version) ⇒ Hash
Sends a GET request to the IG Markets API.
-
#inspect ⇒ String
Returns a human-readable string containing this session’s details.
-
#post(url, payload, api_version) ⇒ Hash
Sends a POST request to the IG Markets API.
-
#put(url, payload, api_version) ⇒ Hash
Sends a PUT request to the IG Markets API.
- #sign_in ⇒ Object
-
#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 |
#cst ⇒ String (readonly)
Returns 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 |
#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, :production
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 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.
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.
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.
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 |
#inspect ⇒ String
Returns a human-readable string containing this session’s details.
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.
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.
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_in ⇒ Object
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 sign_in validate_authentication payload = { identifier: username, password: password_encryptor.encrypt(password), encryptedPassword: true } sign_in_result = request method: :post, url: 'session', payload: payload, api_version: API_V1 headers = sign_in_result.fetch(:response).headers @cst = headers.fetch :cst @x_security_token = headers.fetch :x_security_token nil end |
#sign_out ⇒ Object
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 |