Sha256: 0672dcf03824cde8ae2fe2b828c89ea235151ad455aa25f75e2b6bc6b1ebb4af

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 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
       
    def initialize(uri, options = {})
      @uri, @options = uri, options
      dispatch
    end
    
    def type
      @type ||= @options[:type] || (SUPPORTED_TYPES.detect{|t| @content_type =~ /#{t}/ } || "unknown").to_sym
    end
    
    private
    def dispatch
      get = Smoke::Cache.fetch @uri, @@request_options
      @body = get[:body]
      @content_type = get[:content_type]
      
      parse! unless @options[:raw_response]
      
    rescue RestClient::Exception => e
      Failure.new(@uri, e)
    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

9 entries across 9 versions & 2 rubygems

Version Path
benschwarz-smoke-0.5.10 lib/smoke/request.rb
benschwarz-smoke-0.5.13 lib/smoke/request.rb
smoke-0.5.16 lib/smoke/request.rb
smoke-0.5.15 lib/smoke/request.rb
smoke-0.5.14 lib/smoke/request.rb
smoke-0.5.13 lib/smoke/request.rb
smoke-0.5.12 lib/smoke/request.rb
smoke-0.5.10 lib/smoke/request.rb
smoke-0.5.11 lib/smoke/request.rb