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

WIP Legacy code challenge #1

Open
wants to merge 5 commits into
base: develop
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
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.1'
ruby '2.7.1'

gem 'rails', '~> 6.0.0'
gem 'pg', '>= 0.18', '< 2.0'
Expand All @@ -10,7 +10,6 @@ gem 'bootsnap', '>= 1.4.2', require: false
gem 'rack-cors', require: 'rack/cors'
gem 'clarifai-rails', '~> 0.2.1'
gem 'rest-client'
gem 'rack-cors'
gem 'monkeylearn'

group :development, :test do
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ GEM

PLATFORMS
x86_64-darwin-18
x86_64-darwin-21

DEPENDENCIES
bootsnap (>= 1.4.2)
Expand All @@ -221,7 +222,7 @@ DEPENDENCIES
spring-watcher-listen (~> 2.0.0)

RUBY VERSION
ruby 2.5.1p57
ruby 2.7.1p83

BUNDLED WITH
2.2.27
13 changes: 13 additions & 0 deletions app/controllers/api/analyses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ def image_analysis(url)
def analysis_category
analysis_params[:category].to_sym
end

def validate_params_presence
if params[:category].nil? && params[:resource].nil?
render json: {
message: 'Missing category and resource params'
},
status: 422
elsif params[:category].nil?
render json: { message: 'Missing category param' }, status: 422
else
render json: { message: 'Missing resource param' }, status: 422
end
end
end
12 changes: 0 additions & 12 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@
# require "sprockets/railtie"
require 'rails/test_unit/railtie'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module MyAwesomeApi
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.middleware.insert_before 0, Rack::Cors do
allow do
Expand Down
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
oZG2lEqbiTx7fdneC+QNEfuIup+utLNKy6Aap+W4LR0ssbloFZ5FFFS4FyWDqqwugIEKuyQ+PpkdCg3EwgjyfZj6xq01iBTc116xzLARvHJ/f38RuFTD6YhkN7Xr6uHVjY+W9ExBihjO0fMfTU2j5uAoalxT8mO81cOUl5XWRgNO/EZIYUIr3aCAAMcC7KAUGo57Cf5CxHPQvKhxtZ/mPAbSzYdVDiCTMBdUKzIb9BjYvkXsV4tqthG9+BQRTHMDQOHD0GsyLKFA4Qaj3OEC8RLyB+0xrOYN2FO7quThzNaT1I/ScmoeXkJ5zhheT/njKbH1y78yTEHui8UEXL2Sgb7az1fVjx5mdwb798qnLi323LPrO9cIHOaUiYwjfK1CpqdJBRr5IpK4Xcp+TdhUibx1oBGAgzvKOIZCep/NdeLxJRv8YbuYzjjFwtVbaKDrJxyI/wiPiUGWoI8cPjT6eQnfVMh45VCfoN1yZLLw/QOiEQuaW/WmiB0XVAVlpBNNMID8nrWRqGxh4C7C7Q5CWP3JYOk6RHXX9v1OLo4=--GsdX9mhCHfaOGYPc--EGLBhboCoXFnfQfgRuZmOw==
0e8jOiM9eERMwHuA/RBdShvrTqRzsEfZlv1sFpXiNkKdeWEe2yxVWJ0P2dqbyiMp7E4VU+Du6fX/llAgcjIiMGfiz6Wvznww2/TT6cnZVDE0fQ0WiqLwXED6pGFsI8csJ4WMBrAsvN/dpq9lxyUEZufxRNzOsSgAjza5l133u3pOVshV4afG99M0BZnqREOXcvTCa4OYn4QxoXK15e6Nlnb6rqnYxBt4nKbJJvU0UyhVozBiE3svjijuoVCOWKvNOk9l5kFbyk1JEhjda75lRKzEazoh10cbRQ/8+b/bp9SQiDVy1urpPQ4wuloKflV4twED5pSEenp+zfp4fEe8XBZ7uVldwj7iBZp2evgxpGXTJn6Q2dXoke7rqZX2oZki7EcvCtJA1NpGpXXpigL6OpOa0H+Ie6AyyysgRsqtJcCAdIVzLuyAIjoEUr0gCvdOZQZksNhy1WtxuHJ+yUA8D0M9KMsMEiqvrlsRfXQCH7cyervyXrU49m2sMXAxdmqiCTr/Lk3+Ej0vcTkWj+AxHRxOrfbGuycdgWSWi/gH--ubP5ga0SRTQbPita--J3md7s2nE2sY/eGot7ptsQ==
17 changes: 8 additions & 9 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

# Read more: https://github.com/cyu/rack-cors

# Rails.application.config.middleware.insert_before 0, Rack::Cors do
# allow do
# origins 'example.com'
#
# resource '*',
# headers: :any,
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
# end
# end
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: %i[get post put patch delete options head]
end
end
14 changes: 7 additions & 7 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171219160021) do
ActiveRecord::Schema.define(version: 2017_12_19_160021) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "hstore"
enable_extension "plpgsql"

create_table "analyses", force: :cascade do |t|
t.string "category"
Expand Down
37 changes: 37 additions & 0 deletions spec/request/text_analysis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,42 @@

RSpec.describe Api::AnalysesController, type: :request do
describe 'Text analysis' do
describe 'successfully' do
before do
post '/api/analyses', params: { "analysis": { "resource": 'Are you fucking stupid???', "category": 'text' } }
end

it 'is expected to return a status code of 200' do
expect(response.status).to eq 200
end

it 'is expected to return results of analysis of text' do
expect(eval(JSON.parse(response.body)['results']['classifications'])[0]['tag_name']).to eq 'profanity'
end

it 'is expected to return a degree of certainty with results' do
expect(eval(JSON.parse(response.body)['results']['classifications'])[0]['confidence']).to be_within(0.5).of(1.0)
end
end

describe 'unsuccessfully' do
describe 'when the category is missing' do
end
before do
post '/api/analyses', params: { "analysis": { "resource": 'Are you fucking stupid???', "category": 'text' } }
end

it 'is expected to return an empty result' do
expect(JSON.parse(response.body)['results']['external_id']).to eq nil
end
end

describe 'with category param missing' do
before { post '/api/analyses', params: {} }
it 'is expected to respond with status 422' do
binding.pry
end
end
end
end