Commit ac728a8d authored by Michael Rose's avatar Michael Rose

Merge branch 'story-api'

parents da3a7bd9 8ad6d1ac
......@@ -15,6 +15,9 @@ gem 'devise-async'
gem 'rails_12factor'
# Rate limiting
gem 'rack-attack'
# Memcached for caching
gem 'dalli'
gem 'connection_pool'
......
......@@ -218,6 +218,8 @@ GEM
pg (0.21.0)
public_suffix (3.0.0)
rack (2.0.3)
rack-attack (5.0.1)
rack
rack-mini-profiler (0.10.5)
rack (>= 1.2.0)
rack-protection (2.0.0)
......@@ -381,6 +383,7 @@ DEPENDENCIES
minitest-reporters (>= 0.5.0)
newrelic_rpm
pg
rack-attack
rack-mini-profiler
rails (~> 5.1)
rails-timeago (~> 2.0)
......
class StoriesController < ApplicationController
before_action :set_story, only: [:show]
respond_to :json
def show
respond_with(@story)
end
private
def set_story
@story = Story.find(params[:id])
end
def story_params
params.require(:story).permit(:show)
end
end
module StoriesHelper
end
......@@ -4,4 +4,8 @@ class Author < ActiveRecord::Base
def url_slug
name.gsub(/[^a-z0-9\s]/i, '').gsub(/\s+/, '-')
end
def url
"https://www.fanfiction.net/u/#{id}/#{url_slug}"
end
end
......@@ -12,6 +12,18 @@
class Character < ActiveRecord::Base
belongs_to :fandom
def gender
if isfemale.nil?
return :unknown
end
if isfemale?
:female
else
:male
end
end
def self.for_fandoms(fandoms)
if fandoms && fandoms.size > 1
if fandoms.include?(-1)
......
......@@ -55,6 +55,10 @@ class Story < ActiveRecord::Base
title.gsub(/[^a-z0-9\s]/i, '').gsub(/\s+/, '-')
end
def url
"https://www.fanfiction.net/s/#{id}/1/#{url_slug}"
end
## update method
## updates story row, adds story update if changed, indexes into ES
def self.update(doc)
......
json.(story, :id, :title)
json.external_url story.url
json.author do
json.(story.author, :id, :name)
json.external_url story.author.url
end
json.(story, :status, :language, :rated, :chapters, :favs, :follows, :reviews, :words)
json.characters story.characters, :id, :name, :gender
json.categories story.categories, :name
json.fandoms story.fandoms, :name
json.relationships story.relationships do |relationship|
json.array! relationship, :id, :name, :gender
end
\ No newline at end of file
json.array! @stories, partial: 'stories/story', as: :story
json.partial! "stories/story", story: @story
......@@ -28,5 +28,7 @@ module Scryer
config.maintenance = false
config.active_job.queue_adapter = :sidekiq
config.middleware.use Rack::Attack
end
end
class Rack::Attack
# Rack::Attack.safelist('allow from localhost') do |req|
# # Requests are allowed if the return value is truthy
# '127.0.0.1' == req.ip || '::1' == req.ip
# end
# Throttle story id lookups
Rack::Attack.throttle('/stories/by_id', limit: 10, period: 1.minute) do |req|
req.ip if req.path.starts_with? '/stories'
end
# Throttle searches
Rack::Attack.throttle('/search', limit: 25, period: 1.minute) do |req|
req.ip if req.path.starts_with? '/search'
end
end
\ No newline at end of file
......@@ -33,6 +33,8 @@ Rails.application.routes.draw do
post 'pensieve#bookmark' => 'pensieve#bookmark'
match 'pensieve#bookmark' => 'pensieve#bookmark', :via => :options
get 'stories/:id' => 'stories#show', :defaults => { :format => 'json' }
get '/404', :to => 'errors#not_found'
get '/422', :to => 'errors#unacceptable'
get '/500', :to => 'errors#internal_error'
......
require 'test_helper'
class StoriesControllerTest < ActionDispatch::IntegrationTest
setup do
@story = stories(:one)
end
test "should get index" do
get stories_url, as: :json
assert_response :success
end
test "should create story" do
assert_difference('Story.count') do
post stories_url, params: { story: { show: @story.show } }, as: :json
end
assert_response 201
end
test "should show story" do
get story_url(@story), as: :json
assert_response :success
end
test "should update story" do
patch story_url(@story), params: { story: { show: @story.show } }, as: :json
assert_response 200
end
test "should destroy story" do
assert_difference('Story.count', -1) do
delete story_url(@story), as: :json
end
assert_response 204
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment