Sha256: 9994c2a22a01a91c44135b920769114f1d9a88d8f6868f0ff930a2d419016246

Contents?: true

Size: 1.86 KB

Versions: 13

Compression:

Stored size: 1.86 KB

Contents

module Infoblox
  class Connection
    attr_accessor :adapter, 
                  :adapter_block,
                  :connection, 
                  :host, 
                  :logger, 
                  :password, 
                  :ssl_opts, 
                  :username

    def get(href, params={})
      wrap do
        connection.get(href, params)
      end
    end

    def post(href, body)
      wrap do
        connection.post do |req|
          req.url href
          req.body = body.to_json
        end
      end
    end

    def put(href, body)
      wrap do
        connection.put do |req|
          req.url href
          req.body = body.to_json
        end
      end
    end

    def delete(href)
      wrap do
        connection.delete(href)
      end
    end

    def initialize(opts={})
      self.username = opts[:username]
      self.password = opts[:password]
      self.host     = opts[:host]
      self.logger   = opts[:logger]
      self.ssl_opts = opts[:ssl_opts] 
    end

    def connection
      @connection ||= Faraday.new(:url => self.host, :ssl => {:verify => false}) do |faraday|
        faraday.use Faraday::Response::Logger, logger if logger
        faraday.request :json
        faraday.basic_auth(self.username, self.password)
        faraday.adapter(self.adapter, &self.adapter_block)
      end
    end

    ##
    # The host variable is expected to be a protocol with a host name. 
    # If the host has no protocol, https:// is added before it. 
    #
    def host=(new_host)
      unless new_host =~ /^http(s)?:\/\//
        new_host = "https://#{new_host}"
      end
      @host = new_host
    end

    def adapter
      @adapter ||= :net_http
    end

    private

    def wrap
      yield.tap do |response|
        unless response.status < 300
          raise Exception.new("Error: #{response.status} #{response.body}")
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
infoblox-0.2.15 lib/infoblox/connection.rb
infoblox-0.2.14 lib/infoblox/connection.rb
infoblox-0.2.13 lib/infoblox/connection.rb
infoblox-0.2.12 lib/infoblox/connection.rb
infoblox-0.2.11 lib/infoblox/connection.rb
infoblox-0.2.9 lib/infoblox/connection.rb
infoblox-0.2.8 lib/infoblox/connection.rb
infoblox-0.2.7 lib/infoblox/connection.rb
infoblox-0.2.6 lib/infoblox/connection.rb
infoblox-0.2.5 lib/infoblox/connection.rb
infoblox-0.2.4 lib/infoblox/connection.rb
infoblox-0.2.3 lib/infoblox/connection.rb
infoblox-0.2.2 lib/infoblox/connection.rb