Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Echo service implemented in ruby

  • Loading branch information...
commit 5ededabb037e0d139e5f568e5a4ea91f6eb4fe40 1 parent b9d9839
@aeden authored
View
1  echo/ruby/echo/.rvmrc
@@ -0,0 +1 @@
+rvm use ruby-1.9.3@echo --create
View
4 echo/ruby/echo/Gemfile
@@ -0,0 +1,4 @@
+source :rubygems
+
+gem 'rack'
+gem 'yajl-ruby'
View
12 echo/ruby/echo/Gemfile.lock
@@ -0,0 +1,12 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ rack (1.4.1)
+ yajl-ruby (1.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rack
+ yajl-ruby
View
27 echo/ruby/echo/README.md
@@ -0,0 +1,27 @@
+# echo
+
+Web service written in ruby that parses and echoes JSON data.
+
+## Usage
+
+Install bundler
+
+ gem install bundler
+
+Download required dependencies:
+
+ bundle install
+
+Run the service
+
+ rackup
+
+Call the service from curl:
+
+ curl -i "http://127.0.0.1:3000" -d '{"foo":"bar"}'
+
+## License
+
+Copyright (C) 2012 Anthony Eden
+
+Distributed under the MIT License.
View
3  echo/ruby/echo/config.ru
@@ -0,0 +1,3 @@
+require 'app'
+
+run App
View
9 echo/ruby/echo/lib/app.rb
@@ -0,0 +1,9 @@
+require 'yajl'
+
+App = lambda { |env|
+ [
+ 200,
+ {"Content-Type" => "application/json"},
+ [Yajl::Encoder.encode(Yajl::Parser.parse(env['rack.input']))]
+ ]
+}
View
14 echo/ruby/echo/test/app_test.rb
@@ -0,0 +1,14 @@
+require 'rack'
+require 'minitest/spec'
+require 'minitest/autorun'
+require 'app'
+
+describe App do
+ let(:env) { Rack::MockRequest.env_for('/', {"rack.input" => StringIO.new('{"foo":"bar"}')}) }
+ it 'returns the JSON string' do
+ status, headers, body = App.call(env)
+ status.must_equal 200
+ headers.must_equal({'Content-Type' => 'application/json'})
+ body.must_equal(["{\"foo\":\"bar\"}"])
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.