Module: Megar::Connection
- Included in:
- Session
- Defined in:
- lib/megar/connection.rb
Overview
A simple module to encapsulate network interation
Constant Summary collapse
- DEFAULT_API_ENDPOINT =
There seem to be a number of regional API enpoints, but not sure if there is any guidance yet as to which you should use. Known endpoints: g.api.mega.co.nz/cs, eu.api.mega.co.nz/cs
'https://eu.api.mega.co.nz/cs'
Instance Attribute Summary collapse
-
#sid ⇒ Object
Returns the value of attribute sid.
Instance Method Summary collapse
-
#api_endpoint ⇒ Object
Return the API endpoint url (String) - defaults to DEFAULT_API_ENDPOINT.
-
#api_endpoint=(value) ⇒ Object
Set the API endpoint url to
value
(String). -
#api_request(data) ⇒ Object
Command: Perform a single API request given
data
. -
#api_uri ⇒ Object
Returns the API endpoint uri.
-
#next_sequence_number! ⇒ Object
Command: increments and returns the next sequence number.
-
#sequence_number ⇒ Object
Returns the current session sequence_number.
-
#sequence_number=(value) ⇒ Object
Set the secuence number to
value
(Fixnum).
Instance Attribute Details
#sid ⇒ Object
Returns the value of attribute sid.
10 11 12 |
# File 'lib/megar/connection.rb', line 10 def sid @sid end |
Instance Method Details
#api_endpoint ⇒ Object
Return the API endpoint url (String) - defaults to DEFAULT_API_ENDPOINT
34 35 36 |
# File 'lib/megar/connection.rb', line 34 def api_endpoint @api_endpoint ||= DEFAULT_API_ENDPOINT end |
#api_endpoint=(value) ⇒ Object
Set the API endpoint url to value
(String)
39 40 41 |
# File 'lib/megar/connection.rb', line 39 def api_endpoint=(value) @api_endpoint = value end |
#api_request(data) ⇒ Object
Command: Perform a single API request given data
49 50 51 52 53 54 55 56 |
# File 'lib/megar/connection.rb', line 49 def api_request(data) params = {'id' => next_sequence_number!} params['sid'] = sid if sid json_data = [data].to_json response_body = get_api_response(params,json_data) parse_json_response(response_body) end |
#api_uri ⇒ Object
Returns the API endpoint uri
44 45 46 |
# File 'lib/megar/connection.rb', line 44 def api_uri @api_uri ||= URI.parse(api_endpoint) end |
#next_sequence_number! ⇒ Object
Command: increments and returns the next sequence number
24 25 26 |
# File 'lib/megar/connection.rb', line 24 def next_sequence_number! sequence_number && @sequence_number += 1 end |
#sequence_number ⇒ Object
Returns the current session sequence_number. On first request, it is initialised to a random integer.
14 15 16 |
# File 'lib/megar/connection.rb', line 14 def sequence_number @sequence_number ||= rand(0xFFFFFFFF) end |
#sequence_number=(value) ⇒ Object
Set the secuence number to value
(Fixnum)
19 20 21 |
# File 'lib/megar/connection.rb', line 19 def sequence_number=(value) @sequence_number = value end |