Class: Spacebunny::EndpointConnection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  unless options.is_a? Hash
    fail ArgumentError, 'connection options must be an Hash'
  end
  options = merge_with_default options
  @key = options[:key]
  @client = options[:client]
  @secret = options[:secret]

  ensure_credentials_have_been_provided
  # API endpoint params
  @scheme = options[:scheme]
  @host = options[:host]
  @port = options[:port]
  @configs_path = options[:configs_path]
  @logger = options.fetch :logger
end

Instance Attribute Details

#configs_pathObject

Returns the value of attribute configs_path.



18
19
20
# File 'lib/spacebunny/endpoint_connection.rb', line 18

def configs_path
  @configs_path
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/spacebunny/endpoint_connection.rb', line 18

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/spacebunny/endpoint_connection.rb', line 19

def logger
  @logger
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/spacebunny/endpoint_connection.rb', line 18

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



18
19
20
# File 'lib/spacebunny/endpoint_connection.rb', line 18

def scheme
  @scheme
end

Instance Method Details

#configsObject



39
40
41
42
43
44
# File 'lib/spacebunny/endpoint_connection.rb', line 39

def configs
  unless @configs
    @configs = fetch
  end
  @configs
end

#fetchObject

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