Sha256: 814c2cd9dc5baa2d2ef24e11f4d3fc60c82c1b0c344bf33031d16189716f23c3

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'active_support'
require 'webmock'

module Support
  module Webmock
    extend ActiveSupport::Concern

    included do
      before :each do
        Support::Webmock.mock!
      end
    end

    URLS = %w(
      https://github.com/api/v2/json/repos/show/svenfuchs
      http://github.com/api/v2/json/repos/show/svenfuchs/gem-release
      http://github.com/api/v2/json/repos/show/svenfuchs/minimal
      http://github.com/api/v2/json/repos/show/travis-ci/travis-ci
      http://github.com/api/v2/json/user/show/svenfuchs
      http://github.com/api/v2/json/organizations/travis-ci/public_members
      http://github.com/api/v2/json/user/show/LTe
    )

    class Request
      attr_reader :url, :filename

      def initialize(url)
        @url = url
        @filename = "spec/fixtures/github/#{url.gsub(%r(https?://github.com/), '')}.json"
      end

      def stub!
        WebMock.stub_request(:get, url).to_return(:status => 200, :body => body, :headers => {})
      end

      def body
        store unless stored?
        File.read(filename)
      end

      def store
        puts "Storing #{url} to #{filename}."
        `curl -so #{filename} --create-dirs #{url}`
      end

      def stored?
        File.exists?(filename)
      end
    end

    class << self
      def mock!
        URLS.each { |url| Request.new(url).stub! }
      end
    end

    def mock_github_api
      Support::GithubApi.mock!
    end

    def stub_github_api_post
      url = 'https://api.github.com/hub?access_token=github_oauth_token'
      stub_request(:post, url).to_return(:status => 200, :body => '')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
travis-core-0.0.1 spec/support/webmock.rb