Permalink
Browse files

initial commit

  • Loading branch information...
EricR committed Mar 21, 2012
0 parents commit 47c68233fcf3330035fdf6a84350ab32498a90c8
Showing with 125 additions and 0 deletions.
  1. +76 −0 README.md
  2. +11 −0 lib/mocki.rb
  3. +18 −0 mocki.gemspec
  4. +20 −0 spec/mocki_spec.rb
@@ -0,0 +1,76 @@
+A micro library for mocking up APIs with Sinatra, allowing you to do
+things like this:
+
+ require 'sinatra'
+ require 'faker'
+ require 'mocki'
+
+ get '/users' do
+ mocki do
+ {
+ status: :ok,
+ results: 2.records {|id| {
+ id: id,
+ name: Faker::Name.name,
+ email: Faker::Internet.email,
+ comments: 2.records {{
+ title: Faker::Lorem.words.map(&:capitalize).join(" "),
+ body: Faker::Lorem.paragraph
+ }}
+ }}
+ }
+ end
+ end
+
+ post '/users' do
+ mocki do
+ {
+ status: :created,
+ first_name: params[:first_name],
+ last_name: params[:last_name],
+ email: params[:email],
+ }
+ end
+ end
+
+Which, combined with the awesomeness that is Sinatra, allows you to make
+an HTTP request to /users and get this back:
+
+ {
+ status: "ok",
+ results: [
+ id: 1,
+ name: "Aaron Becker Sr.",
+ email: "aleandro_halvorson@kling.net",
+ comments: [
+ {
+ title: "Iste Ratione Dolorum",
+ body: "Deserunt porro ab qui doloremque est ea sint. Amet tempora officiis. Porro atque quo libero ipsa voluptas doloremque possimus. Sunt dolore et amet aperiam. Sit aut voluptatem sunt ipsum non odio iste."
+ },
+ {
+ title: "Enim Et Aut",
+ body: "Dolorum a est quae nisi qui placeat. Non iusto ut dignissimos accusantium at aut est. Alias illum repellat qui pariatur. Dolorem reiciendis eaque impedit illum corrupti enim."
+ }
+ ]
+ },
+ {
+ id: 2,
+ name: "Leta Volkman",
+ email: "jerrell_luettgen@balistreri.org",
+ comments: [
+ {
+ title: "Deleniti Ut Quia",
+ body: "Assumenda rem impedit. Accusantium in esse voluptas harum nisi doloribus nihil. Esse animi occaecati quibusdam atque vel autem laboriosam. Eos rerum est sit. Autem voluptatum reprehenderit inventore esse quidem voluptatem."
+ },
+ {
+ title: "Sunt Quidem Nam",
+ body: "In temporibus iure. Sapiente fuga ipsum praesentium illum dolor consequuntur est. Nemo ab sit exercitationem et ex delectus. Placeat voluptatum voluptas maiores temporibus. Hic ipsam error est qui illum."
+ }
+ ]
+ }
+ ]
+ }
+
+To install:
+
+ gem install mocki
@@ -0,0 +1,11 @@
+require 'json'
+
+def mocki
+ yield.to_json
+end
+
+class Fixnum
+ def records
+ times.each_with_index.map {|i| yield i+1}
+ end
+end
@@ -0,0 +1,18 @@
+lib = File.expand_path('../lib/', __FILE__)
+$:.unshift lib unless $:.include?(lib)
+
+require 'bundler/version'
+
+Gem::Specification.new do |s|
+ s.name = "mocki"
+ s.version = "1.0.1"
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Eric Rafaloff"]
+ s.email = ["hello@ericrafaloff.com"]
+ s.homepage = "http://github.com/ericr/mocki"
+ s.summary = "A micro library for mocking up APIs with Sinatra."
+ s.description = "A micro library for mocking up APIs with Sinatra."
+ s.files = Dir.glob("{lib}/**/*") + %w(README.markdown)
+ s.require_path = 'lib'
+end
+
@@ -0,0 +1,20 @@
+require './lib/mocki'
+
+describe "mocki" do
+ before :all do
+ @resp = { :hello => :world }
+ end
+
+ it "should return JSON given a block" do
+ mocki {@resp}.should == @resp.to_json
+ end
+
+ describe "records" do
+ it "repeats the block" do
+ mocki {2.records {@resp}}.should include 2.times.map {@resp}.to_json
+ end
+ it "increments an id with each cycle" do
+ mocki {5.records {|id| {id: id} }}.should include "{\"id\":5}"
+ end
+ end
+end

0 comments on commit 47c6823

Please sign in to comment.