Sha256: c2d555004891049e59c21cf51d549d86b45fc5facbe852707c81e1dad31e9248

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8
module Sucker

  # Stubs Sucker::Response to run specs offline.
  class MockResponse < Response
    def initialize(mock_response_body)
      self.body = mock_response_body
      self.code = 200
      self.time = 0.1
    end
  end

  class << self
    attr_accessor :fixtures_path

    # Records a request on first run and fakes subsequently
    def stub(request)
      request.instance_eval do
        self.class.send :define_method, :fixture do
          values = parameters.
            reject { |k, v| %w{AWSAccessKeyId Service}.include? k }.
            values.
            flatten.
            join
          filename = Digest::MD5.hexdigest(host + values)
          "#{Sucker.fixtures_path}/#{filename}.xml"
        end

        self.class.send :define_method, :get do
          if File.exists?(fixture)
            MockResponse.new(File.new(fixture, "r").read)
          else
            curl.url = uri.to_s
            curl.perform
            response = Response.new(curl)

            File.open(fixture, "w") { |f| f.write response.body }

            response
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sucker-0.9.2 lib/sucker/stub.rb