Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json api res #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ group :development, :test do
gem 'pry-rails', '~> 0.3.2'
gem 'pry-remote'
end

gem 'jsonapi-resources'
87 changes: 46 additions & 41 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.1)
actionpack (= 5.0.1)
nio4r (~> 1.2)
actioncable (5.0.2)
actionpack (= 5.0.2)
nio4r (>= 1.2, < 3.0)
websocket-driver (~> 0.6.1)
actionmailer (5.0.1)
actionpack (= 5.0.1)
actionview (= 5.0.1)
activejob (= 5.0.1)
actionmailer (5.0.2)
actionpack (= 5.0.2)
actionview (= 5.0.2)
activejob (= 5.0.2)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.1)
actionview (= 5.0.1)
activesupport (= 5.0.1)
actionpack (5.0.2)
actionview (= 5.0.2)
activesupport (= 5.0.2)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.1)
activesupport (= 5.0.1)
actionview (5.0.2)
activesupport (= 5.0.2)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.1)
activesupport (= 5.0.1)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.0.2)
activesupport (= 5.0.2)
globalid (>= 0.3.6)
activemodel (5.0.1)
activesupport (= 5.0.1)
activerecord (5.0.1)
activemodel (= 5.0.1)
activesupport (= 5.0.1)
activemodel (5.0.2)
activesupport (= 5.0.2)
activerecord (5.0.2)
activemodel (= 5.0.2)
activesupport (= 5.0.2)
arel (~> 7.0)
activesupport (5.0.1)
activesupport (5.0.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
arel (7.1.4)
builder (3.2.2)
builder (3.2.3)
byebug (9.0.6)
coderay (1.1.1)
coffee-rails (4.2.1)
Expand All @@ -51,7 +51,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.4)
concurrent-ruby (1.0.5)
connection_pool (2.2.1)
debug_inspector (0.0.2)
erubis (2.7.0)
Expand All @@ -61,14 +61,18 @@ GEM
thor (~> 0.19.1)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
i18n (0.8.1)
jbuilder (2.6.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.2.2)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jsonapi-resources (0.9.0)
activerecord (>= 4.1)
concurrent-ruby
railties (>= 4.1)
libv8 (5.3.332.38.3)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand All @@ -86,7 +90,7 @@ GEM
libv8 (~> 5.3)
minitest (5.10.1)
multi_json (1.12.1)
nio4r (1.2.1)
nio4r (2.0.0)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
pg (0.19.0)
Expand All @@ -104,26 +108,26 @@ GEM
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.1)
actioncable (= 5.0.1)
actionmailer (= 5.0.1)
actionpack (= 5.0.1)
actionview (= 5.0.1)
activejob (= 5.0.1)
activemodel (= 5.0.1)
activerecord (= 5.0.1)
activesupport (= 5.0.1)
rails (5.0.2)
actioncable (= 5.0.2)
actionmailer (= 5.0.2)
actionpack (= 5.0.2)
actionview (= 5.0.2)
activejob (= 5.0.2)
activemodel (= 5.0.2)
activerecord (= 5.0.2)
activesupport (= 5.0.2)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.1)
railties (= 5.0.2)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.2)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.1)
actionpack (= 5.0.1)
activesupport (= 5.0.1)
railties (5.0.2)
actionpack (= 5.0.2)
activesupport (= 5.0.2)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
Expand Down Expand Up @@ -162,7 +166,7 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.4)
thread_safe (0.3.5)
thread_safe (0.3.6)
tilt (2.0.5)
turbolinks (5.0.1)
turbolinks-source (~> 5)
Expand All @@ -176,7 +180,7 @@ GEM
activemodel (>= 5.0)
debug_inspector
railties (>= 5.0)
websocket-driver (0.6.4)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

Expand All @@ -188,6 +192,7 @@ DEPENDENCIES
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails
jsonapi-resources
listen (~> 3.0.5)
mini_racer
pg (~> 0.18)
Expand All @@ -206,4 +211,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.12.5
1.14.4
7 changes: 7 additions & 0 deletions api_calls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl -i -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' -X POST -d '{"data": {"type":"books", "attributes":{"author":"John Doe", "title":"Does autobiography"}}}' http://localhost:3000/books

curl -i -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' -X POST -d '{ "data": { "type": "chapters", "relationships": { "book": { "data": { "type": "books", "id": "38" } } }, "attributes": { "title": "The Beginning", "body": "Some text" } } }' http://localhost:3000/chapters

curl -i -H "Accept: application/vnd.api+json" "http://localhost:3000/books"

curl -i -H "Accept: application/vnd.api+json" "http://localhost:3000/books?include=chapters"
39 changes: 39 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,42 @@
*= require_tree .
*= require_self
*/
html, body {
height: 100%;
}

body {
display: flex;
font-family: Helvetica Neue;
}

.container {
width: 500px;
}

.note {
color: gray;
margin: 8px 0;
}

input.active:valid {
box-shadow: 0 0 5px 1px green;
}

input.active:invalid {
box-shadow: 0 0 5px 1px red;
}

input.valid {
box-shadow: 0 0 5px 1px green;
}

input.invalid {
box-shadow: 0 0 5px 1px red;
}

.error {
color: red;
font-size: 12px;
margin-top: 5px;
}
4 changes: 2 additions & 2 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Api::V1::BaseController < ApplicationController
respond_to :json
class Api::V1::BaseController < JSONAPI::ResourceController

end
16 changes: 0 additions & 16 deletions app/controllers/api/v1/books_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
class Api::V1::BooksController < Api::V1::BaseController
def index
respond_with Book.all
end

def create
respond_with :api, :v1, Book.create(book_params)
end

def destroy
respond_with Book.destroy(params[:id])
end

private

def book_params
params.require(:book).permit(:id, :author, :title, :year)
end
end
3 changes: 3 additions & 0 deletions app/controllers/api/v1/chapters_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Api::V1::ChaptersController < Api::V1::BaseController

end
3 changes: 0 additions & 3 deletions app/controllers/books_controller.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MainController < ApplicationController

end
2 changes: 2 additions & 0 deletions app/helpers/books_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BooksHelper
end
2 changes: 2 additions & 0 deletions app/helpers/chapters_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ChaptersHelper
end
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Book < ApplicationRecord
has_many :chapters
end
3 changes: 3 additions & 0 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Chapter < ApplicationRecord
belongs_to :book
end
4 changes: 4 additions & 0 deletions app/resources/api/v1/book_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Api::V1::BookResource < JSONAPI::Resource
attributes :author, :title, :year
has_many :chapters
end
6 changes: 6 additions & 0 deletions app/resources/api/v1/chapter_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Api::V1::ChapterResource < JSONAPI::Resource
attributes :title, :body
has_one :book

filter :book
end
3 changes: 0 additions & 3 deletions app/views/books/index.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>
Expand Down
2 changes: 2 additions & 0 deletions app/views/main/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= react_component('BookApp', props: {}, prerender: false) %>

21 changes: 21 additions & 0 deletions client/app/bundles/HelloWorld/components/BookApp.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { Component } from 'react';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import BookList from '../components/BookList';
import NewBook from '../components/NewBook';
import EditBook from '../components/EditBook';
import BookHome from '../components/BookHome';

export default class BookApp extends Component {
render() {
return(
<Router history={browserHistory}>
<Route path="/" component={BookHome}>
<IndexRoute component={BookList} />
<Route path="/books" component={BookList} />
<Route path="/books/new" component={NewBook} />
<Route path="/books/:book_id/edit" component={EditBook} />
</Route>
</Router>
);
}
}
19 changes: 19 additions & 0 deletions client/app/bundles/HelloWorld/components/BookHome.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { Component } from 'react';
import { Link } from 'react-router';

export default class BookHome extends Component {
render() {
return(
<div>
<ul className="header">
<li><Link to="/">Home</Link></li>
<li><Link to="/books">List</Link></li>
<li><Link to="/books/new">Add</Link></li>
</ul>
<div className="content">
{this.props.children}
</div>
</div>
);
}
}
Loading