Sha256: 322c3ebe7a875d980c13deda5078d99ff53389bb8a44f0299de6d8809eb91f7f

Contents?: true

Size: 1.4 KB

Versions: 10

Compression:

Stored size: 1.4 KB

Contents

module Smoke
  class Request # :nodoc:
    class Failure < Exception # :nodoc:
      attr_reader :uri
      
      def initialize(uri, msg)
        @uri = URI.parse(uri)
        Smoke.log.error "Smoke Request: Failed to get from #{@uri} (#{msg})"
      end
    end
    
    SUPPORTED_TYPES = %w(json xml javascript)
    @@request_options = {
      :user_agent       => Smoke.config[:user_agent],
      :accept_encoding  => "gzip"
    }
    
    attr_reader :uri, :content_type, :body, :type
       
    def initialize(uri, options = {})
      @uri, @options = uri, options
      dispatch
    end
    
    private
    def dispatch
      get = Smoke::Cache.fetch @uri, @@request_options
      @body = get[:body]
      @content_type = get[:content_type]
      
      present! unless @options[:raw_response]
      
    rescue RestClient::Exception => e
      Failure.new(@uri, e)
    end
    
    def present!
      set_type
      parse!
    end
    
    def set_type
      @type =  @options[:type] || (SUPPORTED_TYPES.detect{|t| @content_type =~ /#{t}/ } || "unknown").to_sym
    end
    
    def parse!
      case @type
        when :json, :javascript
          @body = ::Crack::JSON.parse(@body).symbolize_keys!
        when :xml
          @body = ::Crack::XML.parse(@body).symbolize_keys!
        when :unknown
          Smoke.log.warn "Smoke Request: Format unknown for #{@uri} (#{@content_type})"
      end      
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
benschwarz-smoke-0.5.0 lib/smoke/request.rb
benschwarz-smoke-0.5.1 lib/smoke/request.rb
benschwarz-smoke-0.5.2 lib/smoke/request.rb
benschwarz-smoke-0.5.3 lib/smoke/request.rb
benschwarz-smoke-0.5.4 lib/smoke/request.rb
benschwarz-smoke-0.5.5 lib/smoke/request.rb
benschwarz-smoke-0.5.6 lib/smoke/request.rb
benschwarz-smoke-0.5.7 lib/smoke/request.rb
benschwarz-smoke-0.5.8 lib/smoke/request.rb
smoke-0.5.9 lib/smoke/request.rb