From 11094f9e62e08227bb45c351ad9f1a6cc6c828c3 Mon Sep 17 00:00:00 2001 From: Krzysztof Wawer Date: Mon, 13 Apr 2015 21:24:27 +0200 Subject: [PATCH] added json extension --- README.md | 23 +++++++++++++++++++++++ lib/hobbit/contrib.rb | 1 + lib/hobbit/json.rb | 32 ++++++++++++++++++++++++++++++++ test/json_test.rb | 20 ++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 lib/hobbit/json.rb create mode 100644 test/json_test.rb diff --git a/README.md b/README.md index 1b954a6..c92fc7e 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ $ gem install hobbit-contrib * [mote](https://github.com/soveran/mote) if you want to use `Hobbit::Mote`. * [tilt](https://github.com/rtomayko/tilt) if you want to use `Hobbit::Render`. +* [multi_json](https://github.com/intridea/multi_json) if you want to use `Hobbit::JSON`. ## Usage @@ -149,6 +150,28 @@ run App.new **Note**: It is recommended to include `Hobbit::Filter` before `Hobbit::ErrorHandling` if you want to use both extensions. +### Hobbit::JSON + +This extension provides helper method which convert object to json and set json header. +To use this extension just include the module: + +```ruby +require 'hobbit' +require 'hobbit/contrib' + +class App < Hobbit::Base + include Hobbit::JSON + + get '/' do + json({'hello' => 'world'}) + end +end +``` + +#### Available methods + +* `json`: Sets json header and convert object to json. + ### Hobbit::Mote This module provides rendering to your hobbit application using [mote](https://github.com/soveran/mote). diff --git a/lib/hobbit/contrib.rb b/lib/hobbit/contrib.rb index 8b2a18d..2a41bb1 100644 --- a/lib/hobbit/contrib.rb +++ b/lib/hobbit/contrib.rb @@ -1,6 +1,7 @@ require 'hobbit/contrib/version' require 'hobbit/environment' require 'hobbit/error_handling' +require 'hobbit/json' require 'hobbit/filter' begin require 'hobbit/mote' diff --git a/lib/hobbit/json.rb b/lib/hobbit/json.rb new file mode 100644 index 0000000..b26d08d --- /dev/null +++ b/lib/hobbit/json.rb @@ -0,0 +1,32 @@ +require 'multi_json' + +module Hobbit + module JSON + def json(object) + response.headers['Content-Type'] = resolve_content_type + resolve_encoder_action(object, resolve_encoder) + end + + private + + def resolve_content_type + 'application/json' + end + + def resolve_encoder_action(object, encoder) + [:encode, :generate].each do |method| + return encoder.send(method, object) if encoder.respond_to? method + end + + if encoder.is_a? Symbol + object.__send__(encoder) + else + fail "#{encoder} does not respond to #generate nor #encode" + end + end + + def resolve_encoder + ::MultiJson + end + end +end diff --git a/test/json_test.rb b/test/json_test.rb new file mode 100644 index 0000000..297060a --- /dev/null +++ b/test/json_test.rb @@ -0,0 +1,20 @@ +require 'helper' + +scope Hobbit::Render do + setup do + mock_app do + include Hobbit::JSON + + get('/') { json({'hello' => 'world'}) } + end + end + + scope '#json' do + test 'returns json response' do + get '/' + assert last_response.ok? + assert last_response.headers['Content-Type'] == 'application/json' + assert JSON.parse(last_response.body) == {'hello' => 'world'} + end + end +end