Skip to content

Commit fa598e8

Browse files
committed
Initial commit on Building JSON APIs with Jbuilder in Rails.
0 parents  commit fa598e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1005
-0
lines changed

.gitignore

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
/db/*.sqlite3-journal
13+
14+
# Ignore all logfiles and tempfiles.
15+
/log/*
16+
/tmp/*
17+
!/log/.keep
18+
!/tmp/.keep
19+
20+
# Ignore uploaded files in development
21+
/storage/*
22+
!/storage/.keep
23+
24+
.byebug_history
25+
26+
# Ignore master key for decrypting credentials and more.
27+
/config/master.key

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.5.3

Gemfile

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
source 'https://rubygems.org'
2+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3+
4+
ruby '2.5.3'
5+
6+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
7+
gem 'rails', '~> 5.2.1'
8+
# Use sqlite3 as the database for Active Record
9+
gem 'sqlite3'
10+
# Use Puma as the app server
11+
gem 'puma', '~> 3.11'
12+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
13+
gem 'jbuilder', '~> 2.5'
14+
# Use Redis adapter to run Action Cable in production
15+
# gem 'redis', '~> 4.0'
16+
# Use ActiveModel has_secure_password
17+
# gem 'bcrypt', '~> 3.1.7'
18+
19+
# Use ActiveStorage variant
20+
# gem 'mini_magick', '~> 4.8'
21+
22+
# Use Capistrano for deployment
23+
# gem 'capistrano-rails', group: :development
24+
25+
# Reduces boot times through caching; required in config/boot.rb
26+
gem 'bootsnap', '>= 1.1.0', require: false
27+
28+
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
29+
# gem 'rack-cors'
30+
31+
group :development, :test do
32+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
33+
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
34+
end
35+
36+
group :development do
37+
gem 'listen', '>= 3.0.5', '< 3.2'
38+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
39+
gem 'spring'
40+
gem 'spring-watcher-listen', '~> 2.0.0'
41+
end
42+
43+
44+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
45+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
46+
gem 'faker', group: [:development, :test]

Gemfile.lock

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
actioncable (5.2.1)
5+
actionpack (= 5.2.1)
6+
nio4r (~> 2.0)
7+
websocket-driver (>= 0.6.1)
8+
actionmailer (5.2.1)
9+
actionpack (= 5.2.1)
10+
actionview (= 5.2.1)
11+
activejob (= 5.2.1)
12+
mail (~> 2.5, >= 2.5.4)
13+
rails-dom-testing (~> 2.0)
14+
actionpack (5.2.1)
15+
actionview (= 5.2.1)
16+
activesupport (= 5.2.1)
17+
rack (~> 2.0)
18+
rack-test (>= 0.6.3)
19+
rails-dom-testing (~> 2.0)
20+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
21+
actionview (5.2.1)
22+
activesupport (= 5.2.1)
23+
builder (~> 3.1)
24+
erubi (~> 1.4)
25+
rails-dom-testing (~> 2.0)
26+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
27+
activejob (5.2.1)
28+
activesupport (= 5.2.1)
29+
globalid (>= 0.3.6)
30+
activemodel (5.2.1)
31+
activesupport (= 5.2.1)
32+
activerecord (5.2.1)
33+
activemodel (= 5.2.1)
34+
activesupport (= 5.2.1)
35+
arel (>= 9.0)
36+
activestorage (5.2.1)
37+
actionpack (= 5.2.1)
38+
activerecord (= 5.2.1)
39+
marcel (~> 0.3.1)
40+
activesupport (5.2.1)
41+
concurrent-ruby (~> 1.0, >= 1.0.2)
42+
i18n (>= 0.7, < 2)
43+
minitest (~> 5.1)
44+
tzinfo (~> 1.1)
45+
arel (9.0.0)
46+
bootsnap (1.3.2)
47+
msgpack (~> 1.0)
48+
builder (3.2.3)
49+
byebug (10.0.2)
50+
concurrent-ruby (1.1.3)
51+
crass (1.0.4)
52+
erubi (1.7.1)
53+
faker (1.9.1)
54+
i18n (>= 0.7)
55+
ffi (1.9.25)
56+
globalid (0.4.1)
57+
activesupport (>= 4.2.0)
58+
i18n (1.1.1)
59+
concurrent-ruby (~> 1.0)
60+
jbuilder (2.8.0)
61+
activesupport (>= 4.2.0)
62+
multi_json (>= 1.2)
63+
listen (3.1.5)
64+
rb-fsevent (~> 0.9, >= 0.9.4)
65+
rb-inotify (~> 0.9, >= 0.9.7)
66+
ruby_dep (~> 1.2)
67+
loofah (2.2.3)
68+
crass (~> 1.0.2)
69+
nokogiri (>= 1.5.9)
70+
mail (2.7.1)
71+
mini_mime (>= 0.1.1)
72+
marcel (0.3.3)
73+
mimemagic (~> 0.3.2)
74+
method_source (0.9.1)
75+
mimemagic (0.3.2)
76+
mini_mime (1.0.1)
77+
mini_portile2 (2.3.0)
78+
minitest (5.11.3)
79+
msgpack (1.2.4)
80+
multi_json (1.13.1)
81+
nio4r (2.3.1)
82+
nokogiri (1.8.5)
83+
mini_portile2 (~> 2.3.0)
84+
puma (3.12.0)
85+
rack (2.0.6)
86+
rack-test (1.1.0)
87+
rack (>= 1.0, < 3)
88+
rails (5.2.1)
89+
actioncable (= 5.2.1)
90+
actionmailer (= 5.2.1)
91+
actionpack (= 5.2.1)
92+
actionview (= 5.2.1)
93+
activejob (= 5.2.1)
94+
activemodel (= 5.2.1)
95+
activerecord (= 5.2.1)
96+
activestorage (= 5.2.1)
97+
activesupport (= 5.2.1)
98+
bundler (>= 1.3.0)
99+
railties (= 5.2.1)
100+
sprockets-rails (>= 2.0.0)
101+
rails-dom-testing (2.0.3)
102+
activesupport (>= 4.2.0)
103+
nokogiri (>= 1.6)
104+
rails-html-sanitizer (1.0.4)
105+
loofah (~> 2.2, >= 2.2.2)
106+
railties (5.2.1)
107+
actionpack (= 5.2.1)
108+
activesupport (= 5.2.1)
109+
method_source
110+
rake (>= 0.8.7)
111+
thor (>= 0.19.0, < 2.0)
112+
rake (12.3.1)
113+
rb-fsevent (0.10.3)
114+
rb-inotify (0.9.10)
115+
ffi (>= 0.5.0, < 2)
116+
ruby_dep (1.5.0)
117+
spring (2.0.2)
118+
activesupport (>= 4.2)
119+
spring-watcher-listen (2.0.1)
120+
listen (>= 2.7, < 4.0)
121+
spring (>= 1.2, < 3.0)
122+
sprockets (3.7.2)
123+
concurrent-ruby (~> 1.0)
124+
rack (> 1, < 3)
125+
sprockets-rails (3.2.1)
126+
actionpack (>= 4.0)
127+
activesupport (>= 4.0)
128+
sprockets (>= 3.0.0)
129+
sqlite3 (1.3.13)
130+
thor (0.20.3)
131+
thread_safe (0.3.6)
132+
tzinfo (1.2.5)
133+
thread_safe (~> 0.1)
134+
websocket-driver (0.7.0)
135+
websocket-extensions (>= 0.1.0)
136+
websocket-extensions (0.1.3)
137+
138+
PLATFORMS
139+
ruby
140+
141+
DEPENDENCIES
142+
bootsnap (>= 1.1.0)
143+
byebug
144+
faker
145+
jbuilder (~> 2.5)
146+
listen (>= 3.0.5, < 3.2)
147+
puma (~> 3.11)
148+
rails (~> 5.2.1)
149+
spring
150+
spring-watcher-listen (~> 2.0.0)
151+
sqlite3
152+
tzinfo-data
153+
154+
RUBY VERSION
155+
ruby 2.5.3p105
156+
157+
BUNDLED WITH
158+
1.16.2

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# README
2+
3+
This README would normally document whatever steps are necessary to get the
4+
application up and running.
5+
6+
Things you may want to cover:
7+
8+
* Ruby version
9+
10+
* System dependencies
11+
12+
* Configuration
13+
14+
* Database creation
15+
16+
* Database initialization
17+
18+
* How to run the test suite
19+
20+
* Services (job queues, cache servers, search engines, etc.)
21+
22+
* Deployment instructions
23+
24+
* ...

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require_relative 'config/application'
5+
6+
Rails.application.load_tasks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ApplicationCable
2+
class Channel < ActionCable::Channel::Base
3+
end
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ApplicationCable
2+
class Connection < ActionCable::Connection::Base
3+
end
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationController < ActionController::API
2+
end
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ArticlesController < ApplicationController
2+
def index
3+
@articles= Article.all
4+
end
5+
end
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CategoriesController < ApplicationController
2+
def index
3+
@categories = Category.all
4+
end
5+
end

app/controllers/concerns/.keep

Whitespace-only changes.

app/jobs/application_job.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationJob < ActiveJob::Base
2+
end

app/mailers/application_mailer.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ApplicationMailer < ActionMailer::Base
2+
default from: '[email protected]'
3+
layout 'mailer'
4+
end

app/models/application_record.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationRecord < ActiveRecord::Base
2+
self.abstract_class = true
3+
end

app/models/article.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Article < ApplicationRecord
2+
belongs_to :source
3+
belongs_to :category
4+
end

app/models/category.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Category < ApplicationRecord
2+
end

app/models/concerns/.keep

Whitespace-only changes.

app/models/source.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Source < ApplicationRecord
2+
end
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
json.array! @articles do |article|
2+
json.id article.id
3+
json.title article.title
4+
json.url article.url
5+
6+
json.category do
7+
json.partial! 'categories/category', category: article.category
8+
end
9+
10+
json.created_at article.created_at
11+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
json.id category.id
2+
json.name category.name
3+
json.created_at category.created_at
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
json.array! @categories do |category|
2+
json.id category.id
3+
json.name category.name
4+
json.created_at category.created_at
5+
end

app/views/layouts/mailer.html.erb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<style>
6+
/* Email styles need to be inline */
7+
</style>
8+
</head>
9+
10+
<body>
11+
<%= yield %>
12+
</body>
13+
</html>

app/views/layouts/mailer.text.erb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= yield %>

bin/bundle

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env ruby
2+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3+
load Gem.bin_path('bundler', 'bundle')

bin/rails

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
begin
3+
load File.expand_path('../spring', __FILE__)
4+
rescue LoadError => e
5+
raise unless e.message.include?('spring')
6+
end
7+
APP_PATH = File.expand_path('../config/application', __dir__)
8+
require_relative '../config/boot'
9+
require 'rails/commands'

bin/rake

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
begin
3+
load File.expand_path('../spring', __FILE__)
4+
rescue LoadError => e
5+
raise unless e.message.include?('spring')
6+
end
7+
require_relative '../config/boot'
8+
require 'rake'
9+
Rake.application.run

0 commit comments

Comments
 (0)