Skip to content

A Hobby extension for JSON requests and responses

License

Notifications You must be signed in to change notification settings

ch1c0t/hobby-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hobby-JSON

This repository provides a Hobby extension for JSON requests and responses.

Installation

gem install hobby-json

Introduction

It tries to parse request.body as JSON. If succeeded, it will store a parsed Ruby object in json. If request.body happened not to have a valid JSON, it will halt the request with 400 status code.

A response will be converted to JSON by calling .to_json on the latest action object.

Here is an echo service(it returns the same JSON posted by a request):

require 'hobby'
require 'hobby/json'

class Echo
  include Hobby
  include JSON

  post { json }
end

Another example could be an adding service. It expects to get an array of numbers in the numbers field, after which it sums them up, and returns the result in the result field.

require 'hobby'
require 'hobby/json'

class Adder
  include Hobby
  include JSON

  post do
    { result: json['numbers'].sum }
  end
end

You can put this in config.ru:

run Adder.new

and run it with rackup. Then, the service can be accessed like so:

~ curl -H "Content-Type: application/json" -X POST -d '{"numbers":[1,2,3]}' http://localhost:9292
{"result":6}%

Development

To run the specs:

bundle exec rspec

To perform mutation analysis:

bundle exec mutant --use rspec 'Hobby::JSON*' --include lib --require hobby/json

About

A Hobby extension for JSON requests and responses

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages