-
Notifications
You must be signed in to change notification settings - Fork 0
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
Api #17
Open
eprothro
wants to merge
38
commits into
master
Choose a base branch
from
api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Api #17
Changes from 9 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d4fe5f1
basic lapse api
f9d4953
updated paperclip to v 4.1.1
755dc2c
update models with association attribute methods
ad191d1
removed extra routes
c452bca
cleaned up extra controller methods in api
43fd908
added request spec @eprothro will you take a look something in rspec …
bdf287c
api covered with test
619d9d8
removed the puts breaking rspec progress bar
56a1aaa
removed controller specs
8036785
cleaned up request specs
9f84809
changed return method
bda1185
removed unused helper method
3633940
code review changes
ba4ea50
extended spec coverage on moments
1f8fe82
extended spec coverage on lapses
aa29ad5
updated lapse api errors
b569e4b
dirty research with yard
eprothro b67272c
first stab and purely static docs
eprothro 1d3b47f
fixup
eprothro ee6137a
some quick and dirty styles
eprothro 6158223
docs with js examples and helpers
eprothro 0183f7d
hacky json layout appending root node
eprothro 9873ae3
Merge pull request #18 from rocketmobile/api-doc
eprothro f3352f2
syntax highlighting
eprothro 1ce6014
error refactor
963a974
style tweaks
eprothro 03172b9
update error response format
f839691
change to json builder gem
151a23e
add specs for lapse patch with invalid id
3a4069b
remove erb template and replace with jbuilder
ad13699
add response status to doc example
eprothro 12d3336
change 422 response to 400
4f7184b
url param inputs JS
eprothro 7fefa15
update specs for error responses/active record error respose concern
d92356c
doc copy
eprothro 9043acf
tweaked the param and full messages method
daa4de7
doc style tweak
eprothro 3d566fb
spec tweak
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Api::BaseController < ApplicationController | ||
protect_from_forgery with: :null_session | ||
layout false | ||
respond_to :json | ||
|
||
rescue_from Exception, with: :internal_error | ||
rescue_from Timeout::Error, with: :timeout | ||
rescue_from Rack::Timeout::Error, with: :timeout | ||
rescue_from ActiveRecord::RecordNotFound, with: :not_found | ||
|
||
protected | ||
def bad_request | ||
return render "api/errors/bad_request", status: 400 | ||
end | ||
def conflict | ||
return render "api/errors/conflict", status: 409 | ||
end | ||
def forbidden | ||
return render "api/errors/forbidden", status: 403 | ||
end | ||
def internal_error(exception) | ||
return render "api/errors/internal_error", status: 500 | ||
end | ||
def invalid_resource | ||
return render "api/errors/invalid_resource", status: 400 | ||
end | ||
def invalid_version | ||
return render "api/errors/invalid_version", status: 400 | ||
end | ||
def not_found(exception) | ||
return render "api/errors/not_found", status: 404 | ||
end | ||
def throttled | ||
return render "api/errors/throttled", status: 429 | ||
end | ||
def timeout(exception) | ||
# If the timeout occured during the middle of a persistence layer query, we need to cancel the | ||
# query so that a "closed connection" exception isn't raised in the middle | ||
# of the next request that executes a query | ||
ActiveRecord::Base.connection.reset! | ||
return render "api/errors/timeout", status: 503 | ||
end | ||
def unauthenticated | ||
return render "api/errors/unauthenticated", status: 401 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Api::V1::LapsesController < Api::BaseController | ||
before_action :set_lapse, only: [:show, :update, :destroy] | ||
|
||
def index | ||
@lapses = Lapse.all | ||
end | ||
|
||
def show | ||
render 'api/v1/lapses/show', status: 200 | ||
end | ||
|
||
def create | ||
@lapse = Lapse.new(lapse_params) | ||
if @lapse.save | ||
render 'api/v1/lapses/show', status: 201 | ||
else | ||
@resourceful_errors = @lapse.errors.full_messages | ||
render 'api/errors/resourceful_error', status: 422 | ||
end | ||
end | ||
|
||
def update | ||
if @lapse.update(lapse_params) | ||
render 'api/v1/lapses/show', status: 200 | ||
else | ||
@resourceful_errors = @lapse.errors.full_messages | ||
render 'api/errors/resourceful_error', status: 422 | ||
end | ||
end | ||
|
||
def destroy | ||
@lapse.destroy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be a |
||
render json: { message: success_message }, status: 200 | ||
end | ||
|
||
private | ||
def set_lapse | ||
@lapse = Lapse.find(params[:id]) | ||
end | ||
|
||
def lapse_params | ||
{ | ||
name: params[:name] | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Api::V1::MomentsController < Api::BaseController | ||
before_action :set_moment, only: [:show, :destroy] | ||
before_action :current_lapse, only: [:create] | ||
|
||
def index | ||
@moments = Moment.all | ||
end | ||
|
||
def show | ||
render 'api/v1/moments/show', status: 200 | ||
end | ||
|
||
def create | ||
@moment = Moment.new(moment_params) | ||
@moment.lapse = @lapse | ||
if @moment.save | ||
render 'api/v1/moments/show', status: 201 | ||
else | ||
@resourceful_errors = @moment.errors.full_messages | ||
render 'api/errors/resourceful_error', status: 422 | ||
end | ||
end | ||
|
||
def destroy | ||
@moment.destroy | ||
render json: { message: success_message }, status: 200 | ||
end | ||
|
||
private | ||
def set_moment | ||
@moment = Moment.find(params[:id]) | ||
end | ||
|
||
def current_lapse | ||
@lapse = Lapse.find(params[:lapse_id]) | ||
end | ||
|
||
def moment_params | ||
{ active: params[:active] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Lapse < ActiveRecord::Base | ||
|
||
has_many :moments, dependent: :destroy | ||
|
||
validates :name, presence: true | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Moment < ActiveRecord::Base | ||
|
||
belongs_to :lapse | ||
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" } | ||
|
||
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ | ||
# validates :image, presence: true | ||
validates :lapse, presence: true | ||
validates :active, presence: true | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:bad_request, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:conflict, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:forbidden, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:internal_error, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:invalid_resource, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:invalid_version, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:not_found, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { @resourceful_errors } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:throttled, scope: "api.errors") } |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object false | ||
node(:error) { I18n.t(:unauthenticated, scope: "api.errors") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
collection @lapses | ||
attributes :id, :name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object @lapse | ||
attributes :id, :name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
collection @moments | ||
attributes :id, :active |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object @moment | ||
attributes :id, :active |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Apipie.configure do |config| | ||
config.app_name = "RailsBaseApp" | ||
config.app_info = "This is an example api." | ||
config.copyright = "" | ||
config.api_base_url = "1" | ||
config.api_controllers_matcher = "#{Rails.root}/app/controllers/api/**/*.rb" | ||
config.ignored_by_recorder = %w[] | ||
config.doc_base_url = "/" | ||
config.use_cache = Rails.env.production? | ||
config.validate = false | ||
config.force_dsl = true | ||
config.reload_controllers = Rails.env.development? | ||
config.default_version = "v1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# config/initializers/rabl_init.rb | ||
require 'rabl' | ||
Rabl.configure do |config| | ||
# Commented as these are defaults | ||
# config.cache_all_output = false | ||
# config.cache_sources = Rails.env != 'development' # Defaults to false | ||
# config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache | ||
# config.perform_caching = false | ||
# config.escape_all_output = false | ||
# config.json_engine = nil # Class with #dump class method (defaults JSON) | ||
# config.msgpack_engine = nil # Defaults to ::MessagePack | ||
# config.bson_engine = nil # Defaults to ::BSON | ||
# config.plist_engine = nil # Defaults to ::Plist::Emit | ||
# config.include_json_root = true | ||
# config.include_msgpack_root = true | ||
# config.include_bson_root = true | ||
# config.include_plist_root = true | ||
# config.include_xml_root = false | ||
# config.include_child_root = true | ||
# config.enable_json_callbacks = false | ||
# config.xml_options = { :dasherize => true, :skip_types => false } | ||
# config.view_paths = [] | ||
# config.raise_on_missing_attribute = true # Defaults to false | ||
# config.replace_nil_values_with_empty_strings = true # Defaults to false | ||
# config.replace_empty_string_values_with_nil_values = true # Defaults to false | ||
# config.exclude_nil_values = true # Defaults to false | ||
# config.exclude_empty_values_in_collections = true # Defaults to false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
require 'constraints/production_path_constraint' | ||
require 'constraints/api_constraints' | ||
|
||
RailsBaseApp::Application.routes.draw do | ||
|
||
# api* | ||
constraints subdomain: /\Aapi/ do | ||
# apipie | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this have to do with apipie? |
||
scope module: "api/v1", constraints: ApiConstraints.new(version: 1, default: true) do | ||
resources :lapses, only: [:index, :show, :create, :update, :destroy], shallow: true do | ||
resources :moments, only: [:index, :show, :create, :destroy] | ||
end | ||
end | ||
end | ||
|
||
root to: 'pages#home' | ||
get 'timeout', to: 'pages#timeout' | ||
|
||
# catch rest of production paths with 404 page | ||
match '*path', to: 'pages#not_found', via: [:get, :post], | ||
constraints: ProductionPathConstraint.new | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateLapses < ActiveRecord::Migration | ||
def change | ||
create_table :lapses do |t| | ||
t.string :name | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateMoments < ActiveRecord::Migration | ||
def change | ||
create_table :moments do |t| | ||
t.integer :lapse_id | ||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pierce-h is there a reason to rescue
Exception
instead ofStandardError
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that should be standard error. I'll admit when it comes to catching errors and exceptions I am not 100% confident in my implementation. I have a rough idea of how I think it all should work.