mei 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ module Mei
2
+ class LcshSubjectResource
3
+
4
+ def self.find(subject, type, solr_field)
5
+ authority_check = Mei::Loc.new('subjects', solr_field)
6
+ authority_result = authority_check.search(subject) #URI escaping doesn't work for Baseball fields?
7
+ if authority_result.present?
8
+ return authority_result
9
+ else
10
+ return []
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,227 @@
1
+ <% dropdown_opts ||= '' %>
2
+ <div class="modal fade" id="meiLookupModal_<%=key%>" tabindex="-1" role="dialog" aria-labelledby="meiLookupModalLabel_<%=key%>" style="width:90%;">
3
+ <div class="modal-dialog" role="document" style="width:90%;">
4
+ <div class="modal-content">
5
+ <div class="modal-header">
6
+ <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
7
+ <h4 class="modal-title" id="meiLookupModalLabel_<%=key%>">Add <%=key.to_s.gsub('_', ' ').upcase%> Term</h4>
8
+ </div>
9
+ <div class="modal-body">
10
+
11
+ <div class="form-group">
12
+ <div class="row">
13
+ <% if dropdown_opts.present? %>
14
+ <div class="col-xs-2">
15
+ <label for="recipient-name" class="control-label">Qualifier:</label>
16
+ <select name="mei_lcsh_extra_<%=key%>" class="form-control" id="mei_lcsh_extra_<%=key%>">
17
+ <% dropdown_opts.each do |opt| %>
18
+ <option value="<%=opt.last%>"><%=opt.first%></option>
19
+ <% end %>
20
+ </select>
21
+ </div>
22
+
23
+ <div class="col-xs-4">
24
+ <label for="recipient-name" class="control-label">Search Term:</label>
25
+ <input type="text" class="form-control" id="mei_lcsh_term_<%=key%>">
26
+ </div>
27
+ <% else %>
28
+ <div class="col-xs-6">
29
+ <label for="recipient-name" class="control-label">Search Term:</label>
30
+ <input type="text" class="form-control" id="mei_lcsh_term_<%=key%>">
31
+ </div>
32
+ <% end %>
33
+
34
+
35
+ <div class="col-xs-4">
36
+
37
+ <div style="padding-top:23px;float:left;"><button type="button" class="btn btn-default" onClick="mei_lookup_<%=key%>();">Lookup</button></div>
38
+ <div style="padding-top:23px;margin-left:40px;float:left;"><button type="button" class="btn btn-default" onClick="mei_use_last_<%=key%>();">Use Last Search</button></div>
39
+ </div>
40
+ </div>
41
+
42
+ </div>
43
+ <div class="form-group">
44
+
45
+ <label for="message-text" class="control-label">Results:</label>
46
+
47
+ <div id="mei_results_<%=key%>"></div>
48
+ </div>
49
+
50
+ </div>
51
+ <div class="modal-footer">
52
+ <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
53
+ <!--<button type="button" class="btn btn-primary">Send message</button>-->
54
+ </div>
55
+ </div>
56
+ </div>
57
+ </div>
58
+
59
+ <script type="text/javascript">
60
+ var mei_element_to_update_<%=key%>;
61
+ var mei_last_value_<%=key%> = '';
62
+
63
+ //Shown is after bootstrap modal transitions have completed. See: http://getbootstrap.com/javascript/#modals-events
64
+ $("#meiLookupModal_<%=key%>").on('shown.bs.modal', function (event) {
65
+ $('#mei_lcsh_term_<%=key%>').focus();
66
+ });
67
+
68
+ $("#meiLookupModal_<%=key%>").on('show.bs.modal', function (event) {
69
+ var button = $(event.relatedTarget) // Button that triggered the modal
70
+ //alert(button.prev().val());
71
+ mei_element_to_update_<%=key%> = button.prev();
72
+ $('#mei_results_<%=key%>').text('');
73
+ $('#mei_lcsh_term_<%=key%>').val('');
74
+ //var recipient = button.data('whatever') // Extract info from data-* attributes
75
+ // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
76
+ // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
77
+ //var modal = $(this)
78
+ //modal.find('.modal-title').text('New message to ' + recipient)
79
+ //modal.find('.modal-body input').val(recipient)
80
+ });
81
+
82
+ function mei_use_last_<%=key%>() {
83
+ if(mei_last_value_<%=key%> == '' && $('#mei_lcsh_term_<%=key%>').val() != '') {
84
+ alert('No last search value present. Are you sure you didn\'t mean to click "Lookup"?');
85
+ return false;
86
+ } else if(mei_last_value_<%=key%> == '') {
87
+ alert('No stored last value to search by!');
88
+ return false;
89
+ }
90
+ $('#mei_lcsh_term_<%=key%>').val(mei_last_value_<%=key%>);
91
+ mei_lookup_<%=key%>();
92
+ }
93
+
94
+ function mei_lookup_<%=key%>()
95
+ {
96
+ $('#mei_results_<%=key%>').text('LOADING...');
97
+ mei_last_value_<%=key%> = $('#mei_lcsh_term_<%=key%>').val();
98
+
99
+ $.getJSON( "/mei/terms/<%=key%>", {
100
+ q: $('#mei_lcsh_term_<%=key%>').val(),
101
+ e: $('#mei_lcsh_extra_<%=key%>').val()
102
+ }, function( data ) {
103
+ //var json_obj = $.parseJSON(data);//parse JSON
104
+ json_obj = data;
105
+
106
+ counter = 0;
107
+ var output='<table border="1" class="mei_table_style"><tr class="mei_alt_table_row"><th style="text-align:center">Broader Terms</th><th style="text-align:center">Actual Search Term</th><th style="text-align:center">Narrower Terms</th>';
108
+ related_found = false;
109
+ for (i = 0; i < json_obj.length; i++)
110
+ {
111
+ if(json_obj[i].related && json_obj[i].related != '' && json_obj[i].related != [])
112
+ {
113
+ output+='<th style="text-align:center">Related Terms</th>';
114
+ related_found = true;
115
+ i = 10000;
116
+ }
117
+ }
118
+
119
+ output+='</tr>';
120
+
121
+
122
+ for (var i in json_obj)
123
+ {
124
+ counter++;
125
+ if(counter % 2 == 0) {
126
+ output+='<tr class="mei_alt_table_row"><td>';
127
+ } else {
128
+ output+='<tr><td>';
129
+ }
130
+
131
+
132
+ for (var j in json_obj[i].broader)
133
+ {
134
+ output+= '<a href="#" onClick="setMeiLCSHSubjVal_<%=key%>(\'' + json_obj[i].broader[j].label + ' (' + json_obj[i].broader[j].uri_link + ')' + '\'); return false;">' + json_obj[i].broader[j].label + '</a> &nbsp;';
135
+ output+= '<a href="#" onClick="setMeiSearchField_<%=key%>(\'' + json_obj[i].broader[j].label + '\'); return false"><span class="glyphicon glyphicon-search" aria-hidden="true"><span></a> &nbsp;';
136
+ output+='<a href="' + json_obj[i].broader[j].uri_link + '" target="_blank"><span class="glyphicon glyphicon-share" aria-hidden="true"><span></a><br />';
137
+ }
138
+ output+="</td><td width='35%'>";
139
+
140
+ output+='<a href="#" onClick="setMeiLCSHSubjVal_<%=key%>(\'' + json_obj[i].label + ' (' + json_obj[i].uri_link + ')' + '\'); return false;"><strong>' + json_obj[i].label + '</strong></a> (' + json_obj[i].count + ') &nbsp;';
141
+ output+= '<a href="' + json_obj[i].uri_link + '" target="_blank"><span class="glyphicon glyphicon-share" aria-hidden="true"><span></a><br />';
142
+
143
+ if(json_obj[i].variants && json_obj[i].variants != '')
144
+ {
145
+ output+='<span style="font-size:10px;">(<em>'
146
+ /* for (var j in json_obj[i].variants)
147
+ {
148
+ output+='<em>'
149
+ }*/
150
+ output+=json_obj[i].variants.join(', ')
151
+ output+='</em>)</span>'
152
+ }
153
+
154
+
155
+ output+="</td><td>";
156
+
157
+ for (var j in json_obj[i].narrower)
158
+ {
159
+ output+= '<a href="#" onClick="setMeiLCSHSubjVal_<%=key%>(\'' + json_obj[i].narrower[j].label + ' (' + json_obj[i].narrower[j].uri_link + ')' + '\'); return false;">' + json_obj[i].narrower[j].label + '</a> &nbsp;';
160
+ output+= '<a href="#" onClick="setMeiSearchField_<%=key%>(\'' + json_obj[i].narrower[j].label + '\'); return false"><span class="glyphicon glyphicon-search" aria-hidden="true"><span></a> &nbsp;';
161
+ output+='<a href="' + json_obj[i].narrower[j].uri_link + '" target="_blank"><span class="glyphicon glyphicon-share" aria-hidden="true"><span></a><br />';
162
+ }
163
+
164
+ if(related_found) {
165
+ output+='</td><td width="16%">';
166
+ }
167
+ if(json_obj[i].related && json_obj[i].related != '' && json_obj[i].related != [])
168
+ {
169
+
170
+ for (var j in json_obj[i].related)
171
+ {
172
+ output+= '<a href="#" onClick="setMeiLCSHSubjVal_<%=key%>(\'' + json_obj[i].related[j].label + ' (' + json_obj[i].related[j].uri_link + ')' + '\'); return false;">' + json_obj[i].related[j].label + '</a> &nbsp;';
173
+ output+= '<a href="#" onClick="setMeiSearchField_<%=key%>(\'' + json_obj[i].related[j].label + '\'); return false"><span class="glyphicon glyphicon-search" aria-hidden="true"><span></a> &nbsp;';
174
+ output+='<a href="' + json_obj[i].related[j].uri_link + '" target="_blank"><span class="glyphicon glyphicon-share" aria-hidden="true"><span></a><br />';
175
+ }
176
+ } else {
177
+ output+= '&nbsp;'
178
+ }
179
+ output+="</td></tr>";
180
+
181
+ }
182
+ output+="</table>";
183
+
184
+ //$('#mei_results').innerHTML = output;
185
+ $('#mei_results_<%=key%>').html(output);
186
+ //$('mei_lcsh_term').value = 'wtf man';
187
+
188
+ //alert(output);
189
+
190
+ /* var items = [];
191
+ $.each( data, function( key, val ) {
192
+ $.each( val, function( key2, val2 ) {
193
+ items.push( "<li id='" + key2 + "'>" + val2 + "</li>" );
194
+ });
195
+
196
+ });
197
+
198
+ alert(items[1]);
199
+
200
+ $( "<ul/>", {
201
+ "class": "my-new-list",
202
+ html: items.join( "" )
203
+ }).appendTo( $('results') );*/
204
+ });
205
+
206
+ }
207
+
208
+ function setMeiSearchField_<%=key%>(val) {
209
+ $('#mei_lcsh_term_<%=key%>').val(val)
210
+ mei_lookup_<%=key%>();
211
+ }
212
+
213
+ function setMeiLCSHSubjVal_<%=key%>(val) {
214
+ mei_element_to_update_<%=key%>.val(val);
215
+ $('#meiLookupModal_<%=key%>').modal('toggle');
216
+ return false;
217
+ }
218
+
219
+ $("#mei_lcsh_term_<%=key%>").keydown(function(event){
220
+ if(event.keyCode == 13){
221
+ event.preventDefault();
222
+ mei_lookup_<%=key%>();
223
+ return true;
224
+ }
225
+ });
226
+
227
+ </script>
@@ -0,0 +1,17 @@
1
+ default: &default
2
+ ldf_server: http://localhost:3001/
3
+ app_type: curation_concerns
4
+ form_fields:
5
+ -
6
+ id: subject
7
+ adapter: lcsh
8
+ solr_field: subject_primary_label_ssim
9
+
10
+ development:
11
+ <<: *default
12
+
13
+ test:
14
+ <<: *default
15
+
16
+ production:
17
+ <<: *default
@@ -0,0 +1,3 @@
1
+ Mei::Engine.routes.draw do
2
+ get "/mei/terms/:term", controller: :terms, action: :query
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'rails/generators'
2
+
3
+ module Mei
4
+ class InstallGenerator < Rails::Generators::Base
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ desc "InstallGenerator Mei Engine"
9
+
10
+ def verify_curation_concerns_installed
11
+ if !IO.read('Gemfile').include?('curation_concerns')
12
+ raise "It doesn't look like you have curation_concerns installed..."
13
+ end
14
+ end
15
+
16
+ def insert_to_assets
17
+ generate 'mei:localassets'
18
+ end
19
+
20
+ def copy_yml_files
21
+ generate 'mei:yml'
22
+ end
23
+
24
+ def insert_to_routes
25
+ generate 'mei:routes'
26
+ end
27
+
28
+
29
+ def bundle_install
30
+ Bundler.with_clean_env do
31
+ run 'bundle install'
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators'
2
+
3
+ module Mei
4
+ class LocalassetsGenerator < Rails::Generators::Base
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ desc "AssetsGenerator Mei Engine"
9
+
10
+ def assets
11
+
12
+ copy_file "mei.scss", "app/assets/stylesheets/mei.scss"
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/generators'
2
+
3
+ module Mei
4
+ class RoutesGenerator < Rails::Generators::Base
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ desc """
9
+ This generator makes the following changes to your application:
10
+ 1. Injects route declarations into your routes.rb
11
+ """
12
+
13
+ # Add Mei to the routes
14
+ def inject_mei_routes
15
+ unless IO.read("config/routes.rb").include?('Mei::Engine')
16
+ marker = 'Rails.application.routes.draw do'
17
+ insert_into_file "config/routes.rb", :after => marker do
18
+ %q{
19
+ mount Mei::Engine => '/'
20
+ }
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ default: &default
2
+ ldf_server: http://localhost:3001/
3
+ app_type: curation_concerns
4
+ form_fields:
5
+ -
6
+ id: subject
7
+ adapter: lcsh
8
+ solr_field: subject_primary_label_ssim
9
+
10
+ development:
11
+ <<: *default
12
+
13
+ test:
14
+ <<: *default
15
+
16
+ production:
17
+ <<: *default
@@ -0,0 +1 @@
1
+ @import 'mei/mei_styles';
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+
3
+ module Mei
4
+ class YmlGenerator < Rails::Generators::Base
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ desc 'YmlGenerator Mei Engine'
9
+
10
+ def config_yml_copy
11
+ copy_file 'config/mei.yml.sample', 'config/mei.yml' unless File::exists?('config/mei.yml')
12
+ end
13
+
14
+ end
15
+ end
data/lib/mei.rb CHANGED
@@ -1,2 +1,11 @@
1
+ require "mei/engine"
2
+ require "mei/web_service_base"
3
+ require "mei/loc"
4
+ require "mei/helper"
5
+ require "mei/geonames"
6
+ require "active_record"
7
+ require "activerecord-import"
8
+
1
9
  module Mei
10
+ extend ActiveSupport::Autoload
2
11
  end
@@ -0,0 +1,5 @@
1
+ module Mei
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Mei
4
+ end
5
+ end
@@ -0,0 +1,96 @@
1
+ require 'uri'
2
+
3
+ module Mei
4
+ class Geonames
5
+
6
+ include Mei::WebServiceBase
7
+
8
+ def request_options
9
+ {:params => {:featureClass => "#{@type}", :style => 'full', :maxRows => 20, :name_startsWith => "#{@escaped_query}", :username=>"boston_library"}, accept: :json}
10
+ end
11
+
12
+ def initialize(e)
13
+ @type = e
14
+ end
15
+
16
+ def search q
17
+ @raw_response = get_json(build_query_url(q))
18
+ parse_authority_response
19
+ end
20
+
21
+
22
+ def build_query_url q
23
+ @escaped_query = URI.escape(q)
24
+ @escaped_query = q
25
+ return "http://ws.geonames.org/searchJSON"
26
+ end
27
+
28
+ #result["geonames"].first["name"]
29
+ #result["geonames"].first["geonameId"]
30
+ #bbox
31
+ #adminId1
32
+ #adminId2
33
+ def parse_authority_response
34
+ end_response = []
35
+
36
+ @raw_response["geonames"].each do |geoname|
37
+
38
+ count = ActiveFedora::Base.find_with_conditions("based_near_ssim:#{solr_clean("http://www.geonames.org/#{geoname["geonameId"]}")}", rows: '100', fl: 'id' ).length
39
+
40
+ if count >= 99
41
+ count = "99+"
42
+ else
43
+ count = count.to_s
44
+ end
45
+
46
+ end_response << {
47
+ "uri_link" => "http://www.geonames.org/#{geoname["geonameId"]}" || "missing!",
48
+ "label" => geoname["name"],
49
+ "broader" => broader(geoname),
50
+ "narrower" => narrower(geoname),
51
+ "variants" => variants(geoname),
52
+ "count" => count
53
+ }
54
+ end
55
+
56
+ end_response
57
+ end
58
+
59
+
60
+
61
+ def broader(row)
62
+ broader_list = []
63
+ if row["fcl"] == "P" || row["fcl"] == "S" || row["fcl"] == "A"
64
+ broader_list << {:uri_link=>"http://www.geonames.org/#{row["adminId1"]}", :label=>row["adminName1"]} if row["adminId1"].present? && row["adminName1"].present?
65
+ broader_list << {:uri_link=>"http://www.geonames.org/#{row["adminId2"]}", :label=>row["adminName2"]} if row["adminId2"].present? && row["adminName2"].present?
66
+ broader_list << {:uri_link=>"http://www.geonames.org/#{row["countryId"]}", :label=>row["countryName"]} if row["countryId"].present? && row["countryName"].present?
67
+ end
68
+
69
+ return broader_list
70
+ end
71
+
72
+ def narrower(row)
73
+ return []
74
+ end
75
+
76
+ def variants(row)
77
+ varient_list = []
78
+ if row["alternateNames"].present?
79
+ row["alternateNames"].each do |variant|
80
+ #if ['eng'].include?( variant["lang"])
81
+ varient_list << variant["name"]
82
+ #end
83
+ end
84
+ end
85
+
86
+ return varient_list
87
+ end
88
+
89
+
90
+ def solr_clean(term)
91
+ return term.gsub('\\', '\\\\').gsub(':', '\\:').gsub(' ', '\ ')
92
+ end
93
+
94
+
95
+ end
96
+ end