Sha256: b59e6396e65efc5d71f45fcbae0a5db3b01dbaab54f047935aa86bdeba20a55e

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

require 'addressable/uri'
require 'patron' # Needed to prevent https://github.com/technoweenie/faraday/issues/140
require 'faraday'
require 'faraday_middleware'
require 'rack-cache'

require "frenetic/configuration"
require "frenetic/hal_json"
require "frenetic/resource"
require "frenetic/version"

class Frenetic

  class MissingAPIReference < StandardError; end

  extend Forwardable
  def_delegators :@connection, :get, :put, :post, :delete

  attr_reader  :connection
  alias_method :conn, :connection

  def initialize( config = {} )
    config      = Configuration.new( config )

    api_url     = Addressable::URI.parse( config[:url] )
    @root_url   = api_url.path

    @connection = Faraday.new( config ) do |builder|
      builder.use HalJson
      builder.request :basic_auth, config[:username], config[:password]

      if config[:cache]
        builder.use FaradayMiddleware::RackCompatible, Rack::Cache::Context, config[:cache]
      end

      builder.adapter :patron
    end
  end

  def description
    @description ||= load_description
  end

  # A naive approach to reloading a Frenetic instance for testing purpose.
  def reload!
    instance_variables.each { |var| instance_variable_set(var, nil) }
  end

private

  def load_description
    if response = get( @root_url ) and response.success?
      response.body
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
frenetic-0.0.2.alpha4 lib/frenetic.rb
frenetic-0.0.2.alpha3 lib/frenetic.rb
frenetic-0.0.2.alpha2 lib/frenetic.rb
frenetic-0.0.2.alpha1 lib/frenetic.rb