Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jugyo committed Feb 20, 2012
0 parents commit 7b24a64
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in mocktra.gemspec
gemspec
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Mocktra
====

A webmock DSL using sinatra.

Installation
----

$ gem install mocktra

Examples
----

require 'mocktra'

# define stub
Mocktra('www.example.com') do
get '/foo' do
'FOO!!'
end

post '/foo' do
params.inspect
end
end

# test it
require 'httpclient'
client = HTTPClient.new

res = client.get("http://www.example.com/foo")
p res.body
#=> "FOO!!"

res = client.post("http://www.example.com/foo", 'bar' => 'baz')
p res.body
#=> "{\"bar\"=>\"baz\"}"

Notice
----

Mocktra calls `WebMock.allow\_net\_connect!` when loading.

Copyright
----

Copyright (c) 2012 jugyo, released under the MIT license.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
29 changes: 29 additions & 0 deletions lib/mocktra.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "mocktra/version"
require 'webmock'
require 'sinatra/base'

WebMock.allow_net_connect!

module Mocktra
class << self
def mocktra(pattern, &block)
# TODO
pattern = case pattern
when Regexp
pattern
when /^http/
/^#{Regexp.quote(pattern)}/
else
/#{Regexp.quote(pattern)}/
end
rack_app = Class.new(Sinatra::Base, &block)
WebMock::API.stub_request(:any, pattern).to_rack(rack_app)
end
end
end

module Kernel
def Mocktra(*args, &block)
Mocktra.mocktra(*args, &block)
end
end
3 changes: 3 additions & 0 deletions lib/mocktra/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Mocktra
VERSION = "1.0.0"
end
24 changes: 24 additions & 0 deletions mocktra.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "mocktra/version"

Gem::Specification.new do |s|
s.name = "mocktra"
s.version = Mocktra::VERSION
s.authors = ["jugyo"]
s.email = ["jugyo.org@gmail.com"]
s.homepage = "https://github.com/jugyo/mocktra"
s.summary = %q{webmock + sinatra}
s.description = %q{A webmock DSL using sinatra.}

s.rubyforge_project = "mocktra"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
s.add_runtime_dependency "webmock"
end

0 comments on commit 7b24a64

Please sign in to comment.