-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviews Endpoint Initial Scaffold (#296)
- Loading branch information
1 parent
ced9841
commit 8ba4dec
Showing
25 changed files
with
420 additions
and
1 deletion.
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# Controller for the Review resource. | ||
class ReviewsController < ApplicationController | ||
def index | ||
reviews = ReviewResource.all(params) | ||
respond_with(reviews) | ||
end | ||
|
||
def show | ||
reviews = ReviewResource.find(params) | ||
respond_with(reviews) | ||
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,13 @@ | ||
class Review < ApplicationRecord | ||
belongs_to :card | ||
has_many :review_comments | ||
has_many :review_votes | ||
|
||
def votes | ||
review_votes.count | ||
end | ||
|
||
def comments | ||
review_comments | ||
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,3 @@ | ||
class ReviewComment < ApplicationRecord | ||
belongs_to :review | ||
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,3 @@ | ||
class ReviewVote < ApplicationRecord | ||
belongs_to :review | ||
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
# Resource for the Review object (currently imported from NRDBc) | ||
class ReviewResource < ApplicationResource | ||
primary_endpoint '/reviews', %i[index show] | ||
|
||
attribute :id, :integer | ||
attribute :username, :string do | ||
@object.user_id | ||
end | ||
attribute :body, :string | ||
attribute :card, :string do | ||
@object.card.title | ||
end | ||
attribute :card_id, :string | ||
attribute :created_at, :datetime | ||
attribute :updated_at, :datetime | ||
attribute :votes, :integer | ||
|
||
belongs_to :card | ||
|
||
attribute :comments, :array do | ||
@object.comments.map do |comment| | ||
{ | ||
id: comment.id, | ||
body: comment.body, | ||
user: comment.user_id, | ||
created_at: comment.created_at, | ||
updated_at: comment.updated_at | ||
} | ||
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
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,13 @@ | ||
class CreateReviews < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :reviews do |t| | ||
t.text :ruling | ||
t.string :username | ||
t.text :card_id, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
add_foreign_key :reviews, :cards | ||
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,11 @@ | ||
class CreateReviewComments < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :review_comments do |t| | ||
t.text :body | ||
t.string :username | ||
t.references :review, null: false, foreign_key: true | ||
|
||
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,10 @@ | ||
class CreateReviewVotes < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :review_votes do |t| | ||
t.string :username | ||
t.references :review, null: false, foreign_key: true | ||
|
||
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,7 @@ | ||
class RenameReviewRulingToBody < ActiveRecord::Migration[7.1] | ||
def change | ||
change_table :reviews do |t| | ||
t.rename :ruling, :body | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
db/migrate/20240721065003_rename_review_username_to_user_id.rb
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,18 @@ | ||
class RenameReviewUsernameToUserId < ActiveRecord::Migration[7.1] | ||
def change | ||
change_table :reviews do |t| | ||
t.remove :username | ||
end | ||
add_reference :reviews, :user, type: :string | ||
|
||
change_table :review_comments do |t| | ||
t.remove :username | ||
end | ||
add_reference :review_comments, :user, type: :string | ||
|
||
change_table :review_votes do |t| | ||
# temporary, actual usernames not available yet, using placeholder values instead | ||
t.rename :username, :user_id | ||
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
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,97 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require 'net/http' | ||
require 'optparse' | ||
require 'uri' | ||
require 'reverse_markdown' | ||
namespace :reviews do | ||
desc 'Imports review from NRDBc, currently storing usernames as strings instead of references' | ||
|
||
def text_to_id(text) | ||
text.downcase | ||
.unicode_normalize(:nfd) | ||
.gsub(/\P{ASCII}/, '') | ||
.gsub(/'s(\p{Space}|\z)/, 's\1') | ||
.split(/[\p{Space}\p{Punct}]+/) | ||
.reject { |s| s&.strip&.empty? } | ||
.join('_') | ||
end | ||
|
||
def retrieve_reviews | ||
url = URI('https://netrunnerdb.com/api/2.0/public/reviews') | ||
|
||
http = Net::HTTP.new(url.host, url.port) | ||
http.use_ssl = true | ||
request = Net::HTTP::Get.new(url) | ||
|
||
response = http.request(request) | ||
|
||
return JSON.parse(response.body) if response.is_a?(Net::HTTPSuccess) | ||
|
||
raise "Failed to retrieve reviews! Status code: #{response.code}" | ||
end | ||
|
||
def purge_tables | ||
# Only do this in a transaction | ||
raise 'Called DB purge outside of a transaction!' unless Review.connection.transaction_open? | ||
|
||
puts 'Purging Review Tables' | ||
ReviewVote.delete_all | ||
ReviewComment.delete_all | ||
Review.delete_all | ||
end | ||
|
||
task import: :environment do | ||
puts 'Importing Reviews from NetrunnerDB Classic' | ||
reviews_body = retrieve_reviews | ||
|
||
card_ids = Card.all.pluck(:id).to_set | ||
Review.transaction do | ||
purge_tables | ||
puts 'Starting import' | ||
reviews_body['data'].each do |review| | ||
card_name = review['title'] | ||
rev_body = ReverseMarkdown.convert review['ruling'] | ||
username = review['user'] | ||
comments = review['comments'] | ||
|
||
card_id = text_to_id(card_name) | ||
if card_ids.include? card_id | ||
r = Review.new | ||
r.card_id = card_id | ||
r.user_id = username | ||
r.body = rev_body | ||
r.created_at = DateTime.parse(review['date_create']) | ||
r.updated_at = DateTime.parse(review['date_update']) | ||
r.save! | ||
|
||
# Hack for votes: generate filler entries in the join table | ||
ReviewVote.transaction do | ||
review['votes'].times do | ||
vote = ReviewVote.new | ||
vote.user_id = 'TBD_Future_Problem' | ||
vote.review = r | ||
vote.save! | ||
end | ||
end | ||
|
||
# Generate Comments for each deck | ||
ReviewComment.transaction do | ||
comments.each do |comment| | ||
c = ReviewComment.new | ||
c.user_id = comment['user'] | ||
c.body = ReverseMarkdown.convert comment['comment'] | ||
c.review = r | ||
c.created_at = comment['date_create'] | ||
c.updated_at = comment['date_update'] | ||
c.save! | ||
end | ||
end | ||
else | ||
puts "Missing Card entry with title: #{card_name}" | ||
end | ||
end | ||
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
* Decklists | ||
* Faction | ||
* Printings | ||
* Reviews | ||
* Rulings | ||
* Side | ||
EXPLANATION | ||
|
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'rspec_api_documentation/dsl' | ||
|
||
resource 'Reviews' do | ||
header 'Content-Type', 'application/json' | ||
header 'Host', 'api-preview.netrunnderdb.com' | ||
|
||
explanation <<~EXPLANATION | ||
Card reviews have the following relationships | ||
* Card | ||
EXPLANATION | ||
|
||
get '/api/v3/public/reviews' do | ||
example_request 'All Reviews' do | ||
expect(status).to eq 200 | ||
end | ||
end | ||
|
||
get '/api/v3/public/reviews/:id' do | ||
parameter :id, type: :string, required: true | ||
|
||
let(:id) { '1' } | ||
example_request 'Get A Single Review' do | ||
expect(status).to eq 200 | ||
end | ||
end | ||
|
||
get '/api/v3/public/reviews?filter[card_id]=:query' do | ||
parameter :query, type: :string, required: true | ||
|
||
let(:query) { 'endurance' } | ||
example_request 'Filter on a single card id' do | ||
expect(status).to eq 200 | ||
end | ||
end | ||
end |
Oops, something went wrong.