dbla 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/dbla/application.js +13 -0
  5. data/app/assets/stylesheets/dbla/application.css +15 -0
  6. data/app/controllers/dbla/application_controller.rb +4 -0
  7. data/app/helpers/dbla/application_helper.rb +4 -0
  8. data/app/views/layouts/dbla/application.html.erb +14 -0
  9. data/config/routes.rb +2 -0
  10. data/lib/dbla.rb +15 -0
  11. data/lib/dbla/abstract_response.rb +54 -0
  12. data/lib/dbla/document_presenter.rb +111 -0
  13. data/lib/dbla/engine.rb +5 -0
  14. data/lib/dbla/repository.rb +45 -0
  15. data/lib/dbla/response.rb +16 -0
  16. data/lib/dbla/routes.rb +44 -0
  17. data/lib/dbla/search_builder_behavior.rb +4 -0
  18. data/lib/dbla/version.rb +3 -0
  19. data/lib/generators/dbla/models_generator.rb +19 -0
  20. data/lib/generators/dbla/routes_generator.rb +20 -0
  21. data/lib/generators/dbla/search_builder_generator.rb +20 -0
  22. data/lib/generators/dbla/templates/collection.rb +45 -0
  23. data/lib/generators/dbla/templates/item.rb +45 -0
  24. data/lib/generators/dbla/templates/search_builder.rb +3 -0
  25. data/lib/tasks/dbla_key.rake +57 -0
  26. data/spec/dbla_spec.rb +7 -0
  27. data/spec/dummy/README.rdoc +28 -0
  28. data/spec/dummy/Rakefile +6 -0
  29. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  30. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  32. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  33. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/spec/dummy/bin/bundle +3 -0
  35. data/spec/dummy/bin/rails +4 -0
  36. data/spec/dummy/bin/rake +4 -0
  37. data/spec/dummy/config.ru +4 -0
  38. data/spec/dummy/config/application.rb +23 -0
  39. data/spec/dummy/config/boot.rb +5 -0
  40. data/spec/dummy/config/database.yml +25 -0
  41. data/spec/dummy/config/environment.rb +5 -0
  42. data/spec/dummy/config/environments/development.rb +37 -0
  43. data/spec/dummy/config/environments/production.rb +78 -0
  44. data/spec/dummy/config/environments/test.rb +39 -0
  45. data/spec/dummy/config/initializers/assets.rb +8 -0
  46. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  48. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  49. data/spec/dummy/config/initializers/inflections.rb +16 -0
  50. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  51. data/spec/dummy/config/initializers/session_store.rb +3 -0
  52. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/spec/dummy/config/locales/en.yml +23 -0
  54. data/spec/dummy/config/routes.rb +4 -0
  55. data/spec/dummy/config/secrets.yml +22 -0
  56. data/spec/dummy/db/test.sqlite3 +0 -0
  57. data/spec/dummy/log/test.log +0 -0
  58. data/spec/dummy/public/404.html +67 -0
  59. data/spec/dummy/public/422.html +67 -0
  60. data/spec/dummy/public/500.html +66 -0
  61. data/spec/dummy/public/favicon.ico +0 -0
  62. data/spec/spec_helper.rb +11 -0
  63. metadata +215 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edd97f400f8dfae0835dc039528d2a3c7871a263
4
+ data.tar.gz: 898b2b9ebce6a913683681001be63c6fc1b1c8c1
5
+ SHA512:
6
+ metadata.gz: db96548cea5437ab103e652e8664d443ec57385cc7433bb1975ef6b11b8ecbc9cec4e3efd0a2dd1372213d1d4ac8f72504ce71bb8af9b0ef5c2bcb4dd85928bb
7
+ data.tar.gz: 441f24c79d6bec54919ffd7edcdc8880cc464f64b19da454ef753849fcea862fd0a5c647420d9a395879866f06b7cc6dda7562924ddd2638efabe111f7d46722
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Dbla'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module Dbla
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Dbla
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dbla</title>
5
+ <%= stylesheet_link_tag "dbla/application", media: "all" %>
6
+ <%= javascript_include_tag "dbla/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,2 @@
1
+ Dbla::Engine.routes.draw do
2
+ end
@@ -0,0 +1,15 @@
1
+ require "dbla/engine"
2
+
3
+ module Dbla
4
+ def self.config_path
5
+ @config_path ||= File.join(Rails.root,'config','dpla.yml').freeze
6
+ end
7
+ def self.config
8
+ @config ||= YAML.load(File.read(config_path)).symbolize_keys
9
+ end
10
+ autoload :AbstractResponse, 'dbla/abstract_response'
11
+ autoload :DocumentPresenter, 'dbla/document_presenter'
12
+ autoload :Repository, 'dbla/repository'
13
+ autoload :Response, 'dbla/response'
14
+ autoload :Routes, 'dbla/routes'
15
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Dbla
3
+ class AbstractResponse < HashWithIndifferentAccess
4
+ require 'blacklight/solr_response/pagination_methods'
5
+ include Blacklight::SolrResponse::PaginationMethods
6
+ # secret api
7
+ attr_accessor :document_model, :blacklight_config, :total, :documents, :start, :limit, :request_params
8
+
9
+ def header
10
+ self['responseHeader'] || {}
11
+ end
12
+
13
+ def params
14
+ header['params'] || request_params
15
+ end
16
+
17
+ def start
18
+ @start
19
+ end
20
+
21
+ def rows
22
+ @limit
23
+ end
24
+
25
+ def sort
26
+ params[:sort]
27
+ end
28
+
29
+ # secret api
30
+ def grouped?
31
+ false
32
+ end
33
+
34
+ def more_like(doc)
35
+ []
36
+ end
37
+
38
+ private
39
+
40
+ # borrowed from Blacklight::SolrRepository
41
+ def force_to_utf8(value)
42
+ case value
43
+ when Hash
44
+ value.each { |k, v| value[k] = force_to_utf8(v) }
45
+ when Array
46
+ value.each { |v| force_to_utf8(v) }
47
+ when String
48
+ value.force_encoding("utf-8") if value.respond_to?(:force_encoding)
49
+ end
50
+ value
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,111 @@
1
+ module Dbla
2
+ class DocumentPresenter
3
+ include ActionView::Helpers::OutputSafetyHelper
4
+ include ActionView::Helpers::TagHelper
5
+ extend Deprecation
6
+
7
+ # @param [Item or Collection] document
8
+ # @param [ActionController::Base] controller scope for linking and generating urls
9
+ # @param [Blacklight::Configuration] configuration
10
+ def initialize(document, controller, configuration = controller.blacklight_config)
11
+ @document = document
12
+ @configuration = configuration
13
+ @controller = controller
14
+ end
15
+ ##
16
+ # Get the value of the document's "title" field, or a placeholder
17
+ # value (if empty)
18
+ #
19
+ # @param [SolrDocument] document
20
+ # @return [String]
21
+ def document_heading
22
+ @document['sourceResource']['title']
23
+ end
24
+ ##
25
+ # Get the document's "title" to display in the <title> element.
26
+ # (by default, use the #document_heading)
27
+ #
28
+ # @see #document_heading
29
+ # @return [String]
30
+ def document_show_html_title
31
+ @document['sourceResource']['title']
32
+ end
33
+ ##
34
+ # Render a value (or array of values) from a field
35
+ #
36
+ # @param [String] value or list of values to display
37
+ # @param [Blacklight::Solr::Configuration::Field] solr field configuration
38
+ # @return [String]
39
+ def render_field_value value=nil, field_config=nil
40
+ end
41
+ ##
42
+ # Render the document index heading
43
+ #
44
+ # @param [Hash] opts (Deprecated)
45
+ # @option opts [Symbol] :label Render the given field from the document
46
+ # @option opts [Proc] :label Evaluate the given proc
47
+ # @option opts [String] :label Render the given string
48
+ # @param [Symbol, Proc, String] field Render the given field or evaluate the proc or render the given string
49
+ def render_document_index_label field, opts ={}
50
+ if Symbol === field
51
+ # these are shenanigans to find a nested field
52
+ field.to_s.split('/').inject(@document) {|m,v| m[v]}
53
+ elsif Proc === field
54
+ field.call
55
+ else
56
+ field
57
+ end
58
+ end
59
+ ##
60
+ # Render the index field label for a document
61
+ #
62
+ # Allow an extention point where information in the document
63
+ # may drive the value of the field
64
+ # @param [String] field
65
+ # @param [Hash] opts
66
+ # @options opts [String] :value
67
+ def render_index_field_value field, options = {}
68
+
69
+ end
70
+ ##
71
+ # Render the show field value for a document
72
+ #
73
+ # Allow an extention point where information in the document
74
+ # may drive the value of the field
75
+ # @param [String] field
76
+ # @param [Hash] options
77
+ # @options opts [String] :value
78
+ def render_document_show_field_value field, options={}
79
+ end
80
+
81
+ ##
82
+ # Get the value for a document's field, and prepare to render it.
83
+ # - highlight_field
84
+ # - accessor
85
+ # - solr field
86
+ #
87
+ # Rendering:
88
+ # - helper_method
89
+ # - link_to_search
90
+ # TODO : maybe this should be merged with render_field_value, and the ugly signature
91
+ # simplified by pushing some of this logic into the "model"
92
+ # @param [SolrDocument] document
93
+ # @param [String] field name
94
+ # @param [Blacklight::Solr::Configuration::Field] solr field configuration
95
+ # @param [Hash] options additional options to pass to the rendering helpers
96
+ def get_field_values field, field_config, options = {}
97
+ end
98
+
99
+ ##
100
+ # Default separator to use in #render_field_value
101
+ #
102
+ # @return [String]
103
+ def field_value_separator
104
+ ', '
105
+ end
106
+
107
+
108
+
109
+
110
+ end
111
+ end
@@ -0,0 +1,5 @@
1
+ module Dbla
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Dbla
4
+ end
5
+ end
@@ -0,0 +1,45 @@
1
+ module Dbla
2
+ class Repository < Blacklight::AbstractRepository
3
+ extend Deprecation
4
+ self.deprecation_horizon = 'blacklight 6.0'
5
+
6
+ def find id, params = {}
7
+ data = get("#{url}/#{id}?api_key=#{api_key}")
8
+ Response.new(data, params,{})
9
+ end
10
+
11
+ def search params = {}
12
+ data = nil
13
+ #TODO Move this into a SearchBuilder, add a generator
14
+ if params['q']
15
+ q = "?api_key=#{api_key}&q=#{params['q']}"
16
+ if params.page
17
+ q << "&page=#{params.page}"
18
+ end
19
+ if params.rows
20
+ q << "&page_size=#{params.rows}"
21
+ end
22
+ puts url + q
23
+ data = get(url + q)
24
+ end
25
+ Response.new(data, params,{})
26
+ end
27
+
28
+ def get(uri)
29
+ uri = URI(uri)
30
+ Net::HTTP.start(uri.host, uri.port) do |http|
31
+ request = Net::HTTP::Get.new uri
32
+ response = http.request request
33
+ return JSON.parse(response.body)
34
+ end
35
+ end
36
+ def api_key
37
+ Dbla.config[:api_key]
38
+ end
39
+
40
+ def url
41
+ @url ||= (Dbla.config[:url] + blacklight_config.document_model.name.downcase.pluralize).freeze
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Dbla
3
+ class Response < AbstractResponse
4
+ def initialize(data, request_params, options = {})
5
+ super(force_to_utf8(data))
6
+ @request_params = request_params
7
+ self.document_model = options[:solr_document_model] || options[:document_model] || Item
8
+ self.blacklight_config = options[:blacklight_config]
9
+ # {"count":228,"start":0,"limit":10,"docs"
10
+ @total = data['count']
11
+ @documents = (data['docs'] || []).map {|d| document_model.new(d,self)}
12
+ @start = data['start']
13
+ @limit = data['limit']
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Dbla
3
+ class Routes < Blacklight::Routes
4
+
5
+ def self.for(router, *resources)
6
+ raise_no_blacklight_secret_key unless Blacklight.secret_key
7
+ options = resources.extract_options!
8
+ resources.map!(&:to_sym)
9
+
10
+ Dbla::Routes.new(router, options.merge(resources: resources)).draw
11
+ end
12
+
13
+ def draw
14
+ item_routing(primary_resource)
15
+ collection_routing(primary_resource)
16
+ end
17
+
18
+ def item_routing(primary_resource)
19
+ add_routes do |options|
20
+ args = {only: [:show]}
21
+ args[:constraints] = options[:constraints] if options[:constraints]
22
+
23
+ resources :item, args.merge(path: primary_resource, controller: primary_resource) do
24
+ member do
25
+ post "track"
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def collection_routing(primary_resource)
32
+ add_routes do |options|
33
+ args = {only: [:show]}
34
+ args[:constraints] = options[:constraints] if options[:constraints]
35
+
36
+ resources :collection, args.merge(path: primary_resource, controller: primary_resource) do
37
+ member do
38
+ post "track"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ module Dbla
2
+ module SearchBuilderBehavior
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Dbla
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+
3
+ module Dbla
4
+ class DocumentGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ desc """
10
+ This generator makes the following changes to your application:
11
+ 1. Creates the Item and Collection in your /app/models directory
12
+ """
13
+ def create_models
14
+ template "item.rb", "app/models/item.rb"
15
+ template "collection.rb", "app/models/collection.rb"
16
+ end
17
+
18
+ end
19
+ end