-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ndvan
committed
Oct 2, 2018
1 parent
989ffa6
commit ebc6243
Showing
18 changed files
with
256 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
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 @@ | ||
// Place all the styles related to the cns controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
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,94 @@ | ||
class CnsController < ApplicationController | ||
def new | ||
@cn = Cn.new | ||
end | ||
|
||
def create | ||
@cn = current_or_guest_user.cns.build(cn_params) | ||
if @cn.save | ||
name = params[:name].empty? ? @cn.file.original_filename : params[:name] | ||
@list = @cn.create_list(name: name, description: params[:description]) | ||
flash[:success] = @list.name + " list is created!" | ||
|
||
loaded_from_file = {} | ||
raw = Paperclip.io_adapters.for(@cn.file).read.split("\n") | ||
loaded_from_file["commonnames"] = raw.map do |n| | ||
n.to_s.strip.encode('UTF-8', { | ||
:invalid => :replace, | ||
:undef => :replace, | ||
:replace => '?' | ||
}) | ||
end | ||
|
||
loaded_from_file["multiple_match"] = @cn.multiple_match | ||
|
||
mapping_response = {} | ||
mapping_response = Req.post( Rails.configuration.x.sv_NCBI_common_name, | ||
loaded_from_file.to_json, | ||
:content_type => :json ) | ||
if mapping_response.empty? | ||
mapping_response = Req.post( Rails.configuration.x.sv_ITIS_common_name, | ||
loaded_from_file.to_json, | ||
:content_type => :json ) | ||
end | ||
|
||
if mapping_response.empty? | ||
mapping_response = Req.post( Rails.configuration.x.sv_TROPICOS_commmon_name, | ||
loaded_from_file.to_json, | ||
:content_type => :json ) | ||
end | ||
|
||
extracted_response = {} | ||
begin | ||
extracted_response["scientificNames"] = mapping_response["result"].map do |n| | ||
n["matched_names"].map do |m| | ||
m["scientific_name"] | ||
end | ||
end.flatten | ||
rescue | ||
extracted_response["scientificNames"] = [] | ||
end | ||
|
||
@list.update_attributes(extracted: extracted_response.to_json) | ||
|
||
resolved_response = Req.post( Rails.configuration.x.sv_OToL_TNRS_wrapper, | ||
extracted_response.to_json, | ||
:content_type => :json ) | ||
|
||
if resolved_response.empty? | ||
extracted_for_gnr = extracted_response | ||
extracted_for_gnr["fuzzy_match"] = true | ||
extracted_for_gnr["multiple_match"] = false | ||
resolved_response = Req.post( Rails.configuration.x.sv_GNR_TNRS_wrapper, | ||
extracted_for_gnr.to_json, | ||
:content_type => :json ) | ||
end | ||
|
||
if resolved_response.empty? | ||
error = Req.post( Rails.configuration.x.sv_OToL_TNRS_wrapper, | ||
extracted_response.to_json, | ||
{:content_type => :json}, | ||
true ) | ||
fail_record = Failure.last.id | ||
end | ||
|
||
@list.update_attributes(resolved: resolved_response.to_json, | ||
resolving_error: error, | ||
possible_failure_record: fail_record) | ||
|
||
redirect_to list_path(@list) | ||
else | ||
render action: "new" | ||
end | ||
end | ||
|
||
private | ||
|
||
def cn_params | ||
begin | ||
params.require(:cn).permit(:file, :multiple_match) | ||
rescue | ||
{} | ||
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,2 @@ | ||
module CnsHelper | ||
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,24 @@ | ||
class Cn < ApplicationRecord | ||
belongs_to :user | ||
|
||
has_one :list, as: :resource, dependent: :destroy | ||
|
||
has_attached_file :file, default_url: "/images/:style/missing.png" | ||
|
||
default_scope -> { order(created_at: :desc) } | ||
|
||
validates_attachment :file, | ||
content_type: { content_type: [ "application/pdf", | ||
"text/plain", | ||
"application/vnd.ms-excel", | ||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", | ||
"application/msword", | ||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ] }, | ||
size: { in: 0..2.megabytes, :message => "must be less than 2MB" } | ||
|
||
validates_attachment_presence :file | ||
|
||
def meta_origin | ||
file.original_filename | ||
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,59 @@ | ||
<% provide(:title, "Create trees from common names in files that have one name per line") %> | ||
|
||
<div class="container"> | ||
<h1>Extract common names from files that have one name per line</h1> | ||
<%= form_for @cn, html: {multipart: true, class: "form-horizontal"} do |f| %> | ||
|
||
<div class="row"> | ||
<div class="col-sm-6 col-sm-offset-3"> | ||
<%= render 'shared/error_messages', object: f.object %> | ||
</div> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= f.label :file, for: "cn_file", class: "col-sm-3 control-label" do %> | ||
<span data-toggle="tooltip" title="Accepted file extensions: .pdf, .txt, .doc, .docs, .xls or .xlsx"> | ||
<%= fa_icon "info-circle" %> | ||
</span> | ||
<span data-toggle="tooltip" title="Required field"><%= fa_icon "asterisk", text: "File" %></span> | ||
<% end %> | ||
<div class="col-sm-7"> | ||
<%= f.file_field :file, id: "file-dropzone" %> | ||
</div> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= label_tag "cn[multiple_match]", class: "col-sm-3 control-label" do %> | ||
<span data-toggle="tooltip" title="The system will return multiple matches (if available) for each common name in the input list."> | ||
<%= fa_icon "info-circle" %> | ||
</span> | ||
Multiple match | ||
<% end %> | ||
<div class="col-sm-4" style="margin-top: 7px;"> | ||
<%= f.check_box :multiple_match, {}, true, false %> | ||
</div> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= label_tag :name, for: "name", class: "col-sm-3 control-label" do %> | ||
Name for list | ||
<% end %> | ||
<div class="col-sm-4"> | ||
<%= text_field_tag :name, nil, placeholder: "Name", class: "form-control" %> | ||
</div> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= label_tag :description, "Description", for: "description_", class: "col-sm-3 control-label" %> | ||
<div class="col-sm-7"> | ||
<%= text_area :description, "", placeholder: "Description", class: "form-control", rows: 5 %> | ||
</div> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<div class="col-sm-offset-3 col-sm-9"> | ||
<%= f.submit "Submit", class: "btn btn-default btn-lg btn-primary" %> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> |
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
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,10 @@ | ||
class CreateCns < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :cns do |t| | ||
t.references :user, foreign_key: true, index: true | ||
t.boolean :multiple_match, :default => false | ||
|
||
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,11 @@ | ||
class AddAttachmentFileToCns < ActiveRecord::Migration[5.1] | ||
def self.up | ||
change_table :cns do |t| | ||
t.attachment :file | ||
end | ||
end | ||
|
||
def self.down | ||
remove_attachment :cns, :file | ||
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,4 @@ | ||
cucumber | ||
tomato | ||
lettuce | ||
pea |
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,9 @@ | ||
cattle | ||
cat | ||
goat | ||
pig | ||
sheep | ||
duck | ||
chicken | ||
horse | ||
domestic dog |
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,9 @@ | ||
require 'test_helper' | ||
|
||
class CnsControllerTest < ActionDispatch::IntegrationTest | ||
test "should get new" do | ||
get cns_new_url | ||
assert_response :success | ||
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 @@ | ||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
user: | ||
|
||
two: | ||
user: |
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 @@ | ||
require 'test_helper' | ||
|
||
class CnTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |