Class: Rack::RemoteConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/remote-configuration.rb,
lib/rack/remote-configuration/version.rb

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RemoteConfiguration

Returns a new instance of RemoteConfiguration.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/remote-configuration.rb', line 12

def initialize(options = {})
  raise ArgumentError, 'Missing required :configuration option' unless options[:configuration]

  path = options[:path] || '/configuration'
  configuration = case options[:configuration]
                  when Hash
                    options[:configuration]
                  when File
                    return new((begin
                                  Plist.parse_xml(configuration.path) || JSON.parse(configuration.read)
                                rescue StandardError
                                  nil
                                end))
                  when String
                    return new(File.open(configuration)) if File.exist?(configuration)
                  end

  raise ArgumentError, 'Invalid configuration (expected Hash or either .json or .plist File or file path)' if configuration.nil?

  begin
    %i[to_json to_plist].each do |serialization|
      configuration.send(serialization)
    end
  rescue NoMethodError => e
    raise ArgumentError, "Serialization Error: #{e}"
  end

  @app = Class.new(Sinatra::Base) do
    register Sinatra::RespondWith

    disable :raise_errors, :show_exceptions

    get path, provides: ['application/json', 'application/x-plist'] do
      respond_to do |f|
        f.on('application/json') { configuration.to_json }
        f.on('application/x-plist') { configuration.to_plist }
      end
    end
  end
end

Instance Method Details

#call(env) ⇒ Object



53
54
55
# File 'lib/rack/remote-configuration.rb', line 53

def call(env)
  @app.call(env)
end