Sha256: 38a4a064dae4349281271503ffa9846040c4307c92fa9648fdc1507d5f751e65

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

require 'yaml'

module Roart

  module Connections
    RequiredConfig = %w(server adapter)
    RequiredToLogin = %w( user pass )

  end

  class Connection

    attr_reader :agent
    attr_reader :conf

    def initialize(conf)
      if conf.is_a?(String)
        raise RoartError, "Loading Config File not yet implemented"
      elsif conf.class.name == Hash.name #TODO: Figure out why conf.is_a?(Hash) doesn't work
        @conf = conf
      end
      if Roart::check_keys(conf, Roart::Connections::RequiredConfig)
        @agent = @conf[:login]
        add_methods!
      else
        raise RoartError, "Configuration Error"
      end
    end

    def authenticate(conf)
      if Roart::check_keys(conf, Roart::Connections::RequiredToLogin)
        connection.authenticate(conf)
        self
      end
    end

    def rest_path
      self.server + '/REST/1.0/'
    end

    def get(uri)
      connection.get(uri)
    end

    def post(uri, payload)
      connection.post(uri, payload)
    end

    protected

    def connection
      @connection ||= ConnectionAdapter.new(conf)
    end

    def add_methods!
      @conf.each do |key, value|
        (class << self; self; end).send :define_method, key do
          return value
        end
      end
    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ludo-roart-0.1.19 lib/roart/connection.rb
ludo-roart-0.1.18 lib/roart/connection.rb
ludo-roart-0.1.17 lib/roart/connection.rb
ludo-roart-0.1.16 lib/roart/connection.rb
ludo-roart-0.1.15 lib/roart/connection.rb
ludo-roart-0.1.14 lib/roart/connection.rb
ludo-roart-0.1.13 lib/roart/connection.rb
ludo-roart-0.1.12 lib/roart/connection.rb
ludo-roart-0.1.11 lib/roart/connection.rb