Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed May 18, 2012
0 parents commit 7535c4f
Show file tree
Hide file tree
Showing 52 changed files with 1,210 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.bundle/
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm --create use 1.9.3@hypermodel
19 changes: 19 additions & 0 deletions Gemfile
@@ -0,0 +1,19 @@
source "http://rubygems.org"

# Declare your gem's dependencies in hypermodel.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# jquery-rails is used by the dummy application
gem "jquery-rails"
gem "mongoid"
gem "bson_ext"

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
109 changes: 109 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,109 @@
PATH
remote: .
specs:
hypermodel (0.0.1)
mongoid
rails (~> 3.2.3)

GEM
remote: http://rubygems.org/
specs:
actionmailer (3.2.3)
actionpack (= 3.2.3)
mail (~> 2.4.4)
actionpack (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.1)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.1.2)
activemodel (3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
activerecord (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
bson (1.6.2)
bson_ext (1.6.2)
bson (~> 1.6.2)
builder (3.0.0)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.3)
jquery-rails (2.0.2)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
json (1.7.3)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.18)
mongo (1.6.2)
bson (~> 1.6.2)
mongoid (2.4.10)
activemodel (~> 3.1)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
multi_json (1.3.5)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
rails (3.2.3)
actionmailer (= 3.2.3)
actionpack (= 3.2.3)
activerecord (= 3.2.3)
activeresource (= 3.2.3)
activesupport (= 3.2.3)
bundler (~> 1.0)
railties (= 3.2.3)
railties (3.2.3)
actionpack (= 3.2.3)
activesupport (= 3.2.3)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)

PLATFORMS
ruby

DEPENDENCIES
bson_ext
hypermodel!
jquery-rails
mongoid
sqlite3
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright 2012 YOURNAME

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
@@ -0,0 +1,63 @@
# hypermodel [![Build Status](https://secure.travis-ci.org/codegram/hypermodel.png)](http://travis-ci.org/codegram/hypermodel) [![Dependency Status](https://gemnasium.com/codegram/hypermodel.png)](http://gemnasium.com/codegram/hypermodel)

A Rails Responder that renders any [Mongoid][mongoid] model to a [JSON HAL
format][hal], suitable for Hypermedia APIs.

## Install

Put this in your Gemfile:

gem 'hypermodel'

## Usage

````ruby
class PostsController < ApplicationController
respond_to :json

def show
@post = Post.find params[:id]
respond_with(@post, responder: Hypermodel::Responder)
end
end
````

Now if you ask your API for a Post:

{"_id"=>"4fb648996b98c90919000012",
"title"=>"My post",
"body"=>"Foo bar baz.",
"author_id"=>"4fb648996b98c9091900000f",
"updated_at"=>"2012-05-18T13:03:21Z",
"created_at"=>"2012-05-18T13:03:21Z",
"_links"=>
{"self"=>{"href"=>"http://test.host/posts/4fb648996b98c90919000012"},
"author"=>{"href"=>"http://test.host/authors/4fb648996b98c9091900000f"},
"reviews"=>
{"href"=>"http://test.host/posts/4fb648996b98c90919000012/reviews"}},
"_embedded"=>
{"comments"=>
[{"_id"=>"4fb648996b98c9091900000d", "body"=>"Comment 1"},
{"_id"=>"4fb648996b98c9091900000e", "body"=>"Comment 2"}]}}

## Contributing

* [List of hypermodel contributors][contributors]

* Fork the project.
* Make your feature addition or bug fix.
* Add specs for it. This is important so we don't break it in a future
version unintentionally.
* Commit, do not mess with rakefile, version, or history.
If you want to have your own version, that is fine but bump version
in a commit by itself I can ignore when I pull.
* Send me a pull request. Bonus points for topic branches.

## License

MIT License. Copyright 2012 [Codegram Technologies][codegram]

[mongoid]: http://mongoid.org
[hal]: http://stateless.co/hal_specification.html
[contributors]: https://github.com/codegram/hypermodel/contributors
[codegram]: http://codegram.com
38 changes: 38 additions & 0 deletions Rakefile
@@ -0,0 +1,38 @@
#!/usr/bin/env rake
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
begin
require 'rdoc/task'
rescue LoadError
require 'rdoc/rdoc'
require 'rake/rdoctask'
RDoc::Task = Rake::RDocTask
end

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Hypermodel'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end




Bundler::GemHelper.install_tasks

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end


task :default => :test
23 changes: 23 additions & 0 deletions hypermodel.gemspec
@@ -0,0 +1,23 @@
$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "hypermodel/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "hypermodel"
s.version = Hypermodel::VERSION
s.authors = ["Josep M. Bach"]
s.email = ["josep.m.bach@gmail.com"]
s.homepage = "https://github.com/codegram/hypermodel"
s.summary = "Rails Responder to generate an automagic JSON HAL representation for your Mongoid models"
s.description = "Rails Responder to generate an automagic JSON HAL representation for your Mongoid models"

s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]

s.add_dependency "rails", "~> 3.2.3"
s.add_dependency "mongoid"

s.add_development_dependency "sqlite3"
end
5 changes: 5 additions & 0 deletions lib/hypermodel.rb
@@ -0,0 +1,5 @@
module Hypermodel
end

require 'hypermodel/responder'

29 changes: 29 additions & 0 deletions lib/hypermodel/responder.rb
@@ -0,0 +1,29 @@
require 'hypermodel/serializers/mongoid'

module Hypermodel
Serializer = Serializers::Mongoid

class Responder
def self.call(*args)
controller = args[0]
resource = args[1].first
resource_name = controller.params["controller"]
action = controller.params["action"]

responder = new resource_name, action, resource, controller

controller.render json: responder
end

def initialize(resource_name, action, resource, controller)
@resource_name = resource_name
@action = action
@resource = Serializer.new(resource, controller)
end

def to_json(*opts)
@resource.to_json(*opts)
end
end
end

0 comments on commit 7535c4f

Please sign in to comment.