Class: Spacebunny::EndpointConnection
- Inherits:
-
Object
- Object
- Spacebunny::EndpointConnection
- Defined in:
- lib/spacebunny/endpoint_connection.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ scheme: 'https', host: 'api.spacebunny.io', port: 443, configs_path: { device: '/device_configurations', live_stream: '/live_stream_key_configurations' } }.freeze
Instance Attribute Summary collapse
-
#configs_path ⇒ Object
Returns the value of attribute configs_path.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
Returns the value of attribute port.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
Instance Method Summary collapse
- #configs ⇒ Object
-
#fetch ⇒ Object
Contact APIs endpoint to retrieve configs.
-
#initialize(options = {}) ⇒ EndpointConnection
constructor
A new instance of EndpointConnection.
Constructor Details
#initialize(options = {}) ⇒ EndpointConnection
Returns a new instance of EndpointConnection.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/spacebunny/endpoint_connection.rb', line 21 def initialize( = {}) unless .is_a? Hash fail ArgumentError, 'connection options must be an Hash' end = merge_with_default @key = [:key] @client = [:client] @secret = [:secret] ensure_credentials_have_been_provided # API endpoint params @scheme = [:scheme] @host = [:host] @port = [:port] @configs_path = [:configs_path] @logger = .fetch :logger end |
Instance Attribute Details
#configs_path ⇒ Object
Returns the value of attribute configs_path.
18 19 20 |
# File 'lib/spacebunny/endpoint_connection.rb', line 18 def configs_path @configs_path end |
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/spacebunny/endpoint_connection.rb', line 18 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
19 20 21 |
# File 'lib/spacebunny/endpoint_connection.rb', line 19 def logger @logger end |
#port ⇒ Object
Returns the value of attribute port.
18 19 20 |
# File 'lib/spacebunny/endpoint_connection.rb', line 18 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
18 19 20 |
# File 'lib/spacebunny/endpoint_connection.rb', line 18 def scheme @scheme end |
Instance Method Details
#configs ⇒ Object
39 40 41 42 43 44 |
# File 'lib/spacebunny/endpoint_connection.rb', line 39 def configs unless @configs @configs = fetch end @configs end |
#fetch ⇒ Object
Contact APIs endpoint to retrieve configs
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/spacebunny/endpoint_connection.rb', line 59 def fetch uri_builder = case scheme.to_s when 'http' URI::HTTP when 'https' URI::HTTPS end unless uri = uri_builder.build(host: host, port: port, path: "#{configs_path}") raise SchemeNotValid.new(scheme) end response = contact_endpoint_with uri content = JSON.parse(response.to_s, symbolize_names: true) rescue nil status = response.status if status != 200 if content phrase = "Auto-configuration failed: #{response.status} => #{content[:error]}" if status == 401 if device? phrase = "#{phrase}. Is Device Key correct?" else phrase = "#{phrase} Are Client and Secret correct?" end end else phrase = "#{response.status}" end raise EndpointError, phrase end content end |