dbla 0.0.2 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb8fae248060253ada17d9cbc785ec9bef380173
4
- data.tar.gz: f9c302e800a86a250dcbf215b16464e73e07f438
3
+ metadata.gz: ed3c90144eabe9791538f773b22ba691467248be
4
+ data.tar.gz: 7cab3b50332d69e51d978538022ff2034b88265f
5
5
  SHA512:
6
- metadata.gz: d86878a38f42a993afc633916a57e83a251f78dba365c28cccccaf2035e6528c663ac6bbcdacae8139684b68356bdc8117f5aeefcbc7876643e5bb1732450536
7
- data.tar.gz: 37ad200951dfc84c7b99c005daa60465db099b5c4b3857115e402f74f9006ee2f9dee669d6750913f90bd9670bad5836cdd7ed3e3290b26a02a06c73cc1f471a
6
+ metadata.gz: 276e2e397be231484d882ce6c8111f7905a79e1dbe428be6dff04944c02efa4598f47106c01e454c8f23012714159f52f8c90b4078136114813c237edfea6c68
7
+ data.tar.gz: 19da1e0593e6f066ac31505d6d9df0a0120685186966fd112ed4481c04ec6e0e7a7e7284df49466f0b1ff91a1fc914b1b40e7d1c7c441b2901934dd8f514858d
@@ -12,4 +12,5 @@ module Dbla
12
12
  autoload :Repository, 'dbla/repository'
13
13
  autoload :Response, 'dbla/response'
14
14
  autoload :Routes, 'dbla/routes'
15
+ autoload :SearchBuilderBehavior, 'dbla/search_builder_behavior'
15
16
  end
@@ -49,10 +49,10 @@ module Dbla
49
49
  when Array
50
50
  value.each { |v| force_to_utf8(v) }
51
51
  when String
52
- value.force_encoding("utf-8") if value.respond_to?(:force_encoding)
52
+ value.force_encoding("utf-8") if value.respond_to?(:force_encoding)
53
53
  end
54
54
  value
55
55
  end
56
56
 
57
57
  end
58
- end
58
+ end
@@ -4,6 +4,8 @@ module Dbla
4
4
  include ActionView::Helpers::TagHelper
5
5
  extend Deprecation
6
6
 
7
+ DESCRIPTIVE_METADATA_KEY = 'sourceResource'.freeze
8
+
7
9
  # @param [Item or Collection] document
8
10
  # @param [ActionController::Base] controller scope for linking and generating urls
9
11
  # @param [Blacklight::Configuration] configuration
@@ -19,7 +21,7 @@ module Dbla
19
21
  # @param [SolrDocument] document
20
22
  # @return [String]
21
23
  def document_heading
22
- @document['sourceResource']['title']
24
+ @document[DESCRIPTIVE_METADATA_KEY]['title']
23
25
  end
24
26
  ##
25
27
  # Get the document's "title" to display in the <title> element.
@@ -28,7 +30,7 @@ module Dbla
28
30
  # @see #document_heading
29
31
  # @return [String]
30
32
  def document_show_html_title
31
- @document['sourceResource']['title']
33
+ @document[DESCRIPTIVE_METADATA_KEY]['title']
32
34
  end
33
35
  ##
34
36
  # Render a value (or array of values) from a field
@@ -49,7 +51,7 @@ module Dbla
49
51
  def render_document_index_label field, opts ={}
50
52
  if Symbol === field
51
53
  # these are shenanigans to find a nested field
52
- field.to_s.split('/').inject(@document) {|m,v| m[v]}
54
+ field.to_s.split('.').inject(@document) {|m,v| m[v]}
53
55
  elsif Proc === field
54
56
  field.call
55
57
  else
@@ -87,7 +89,7 @@ module Dbla
87
89
  # Rendering:
88
90
  # - helper_method
89
91
  # - link_to_search
90
- # TODO : maybe this should be merged with render_field_value, and the ugly signature
92
+ # TODO : maybe this should be merged with render_field_value, and the ugly signature
91
93
  # simplified by pushing some of this logic into the "model"
92
94
  # @param [SolrDocument] document
93
95
  # @param [String] field name
@@ -108,4 +110,4 @@ module Dbla
108
110
 
109
111
 
110
112
  end
111
- end
113
+ end
@@ -12,12 +12,16 @@ module Dbla
12
12
  data = nil
13
13
  #TODO Move this into a SearchBuilder, add a generator
14
14
  if params['q']
15
- q = "?api_key=#{api_key}&q=#{params['q']}"
15
+ q = "?api_key=#{api_key}&q=#{params['q']}&facets=sourceResource.format"
16
16
  if params.page
17
- q << "&page=#{params.page}"
17
+ q << "&page=#{params.page}"
18
18
  end
19
19
  if params.rows
20
- q << "&page_size=#{params.rows}"
20
+ q << "&page_size=#{params.rows}"
21
+ end
22
+ params.facet_filters do |facet_field, value|
23
+ value = "\"#{value}\"" if value.index(' ')
24
+ q << "&#{facet_field}=#{CGI::escape(value)}"
21
25
  end
22
26
  puts url + q
23
27
  data = get(url + q)
@@ -34,12 +38,13 @@ module Dbla
34
38
  end
35
39
  end
36
40
  def api_key
37
- Dbla.config[:api_key]
41
+ Dbla.config.fetch(:api_key)
38
42
  end
39
43
 
40
44
  def url
41
- @url ||= (Dbla.config[:url] + blacklight_config.document_model.name.downcase.pluralize).freeze
45
+ # REVIEW: What if the URL does not have a trailing /; Should it be `File.join?`
46
+ @url ||= (Dbla.config.fetch(:url) + blacklight_config.document_model.name.downcase.pluralize).freeze
42
47
  end
43
48
 
44
49
  end
45
- end
50
+ end
@@ -1,6 +1,8 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Dbla
3
3
  class Response < AbstractResponse
4
+ autoload :Facets, 'dbla/response/facets'
5
+
4
6
  def initialize(data, request_params, options = {})
5
7
  super(force_to_utf8(data))
6
8
  @request_params = request_params
@@ -11,6 +13,29 @@ module Dbla
11
13
  @documents = (data['docs'] || []).map {|d| document_model.new(d,self)}
12
14
  @start = data['start']
13
15
  @limit = data['limit']
16
+ @aggregations = (data['facets'] || {})
17
+ else
18
+ @aggregations = {}
19
+ @total = 0
20
+ @start = 0
21
+ @limit = 10
22
+ @documents = []
23
+ end
24
+ end
25
+ def aggregations
26
+ # "facets":{"sourceResource.format":{"_type":"terms","missing":77,"total":250,"other":16,"terms":[{"term":"Photographs","count":44},
27
+ Hash[@aggregations.map do |k,v|
28
+ items = v['terms'].map {|term| Facets::FacetItem.new(value: term['term'], hits: term['count'])}
29
+ [k, Facets::FacetField.new(k,items)]
30
+ end]
31
+ end
32
+ def spelling
33
+ Words.new([])
34
+ end
35
+ class Words
36
+ attr_accessor :words
37
+ def initialize(words)
38
+ @words = words
14
39
  end
15
40
  end
16
41
  end
@@ -0,0 +1,62 @@
1
+ require 'ostruct'
2
+ module Dbla::Response::Facets
3
+ # Borrowed from Blacklight
4
+ class FacetField
5
+ attr_reader :name, :items
6
+ def initialize name, items, options = {}
7
+ @name, @items = name, items
8
+ @options = options
9
+ end
10
+
11
+ def limit
12
+ @options[:limit] || default_limit
13
+ end
14
+
15
+ def sort
16
+ @options[:sort] || default_sort
17
+ end
18
+
19
+ def offset
20
+ @options[:offset] || default_offset
21
+ end
22
+
23
+ private
24
+ def default_limit
25
+ 100
26
+ end
27
+
28
+ def default_sort
29
+ if limit > 0
30
+ 'count'
31
+ else
32
+ 'index'
33
+ end
34
+ end
35
+
36
+ def default_offset
37
+ 0
38
+ end
39
+ end
40
+ class FacetItem < OpenStruct
41
+ def initialize *args
42
+ options = args.extract_options!
43
+
44
+ # Backwards-compat method signature
45
+ value = args.shift
46
+ hits = args.shift
47
+
48
+ options[:value] = value if value
49
+ options[:hits] = hits if hits
50
+
51
+ super(options)
52
+ end
53
+
54
+ def label
55
+ super || value
56
+ end
57
+
58
+ def as_json(props = nil)
59
+ table.as_json(props)
60
+ end
61
+ end
62
+ end
@@ -1,4 +1,24 @@
1
1
  module Dbla
2
2
  module SearchBuilderBehavior
3
+ def processed_parameters
4
+ request.tap do |request_parameters|
5
+ if blacklight_params[:q]
6
+ request_parameters[:q] = blacklight_params[:q]
7
+ end
8
+ end
9
+ end
10
+ def facet_filters
11
+ # :fq, map from :f.
12
+ if ( blacklight_params[:f])
13
+ f_request_params = blacklight_params[:f]
14
+
15
+ f_request_params.each_pair do |facet_field, value_list|
16
+ Array(value_list).each do |value|
17
+ next if value.blank? # skip empty strings
18
+ yield *[facet_field, value]
19
+ end
20
+ end
21
+ end
22
+ end
3
23
  end
4
24
  end
@@ -1,3 +1,3 @@
1
1
  module Dbla
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,7 +2,7 @@
2
2
  module Dbla
3
3
  class Assets < Rails::Generators::Base
4
4
  source_root File.expand_path('../templates', __FILE__)
5
-
5
+
6
6
  def assets
7
7
  copy_file "dbla.css.scss", "app/assets/stylesheets/dbla.css.scss"
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module Dbla
2
2
  class Install < Rails::Generators::Base
3
-
3
+
4
4
  source_root File.expand_path('../templates', __FILE__)
5
-
5
+
6
6
  argument :controller_name, type: :string , default: "catalog"
7
7
  argument :search_builder_name, type: :string , default: "search_builder"
8
8
 
@@ -15,11 +15,11 @@ module Dbla
15
15
 
16
16
  # Copy all files in templates/public/ directory to public/
17
17
  # Call external generator in AssetsGenerator, so we can
18
- # leave that callable seperately too.
19
- def copy_public_assets
18
+ # leave that callable seperately too.
19
+ def copy_public_assets
20
20
  generate "dbla:assets"
21
21
  end
22
-
22
+
23
23
  def generate_search_builder
24
24
  generate 'dbla:search_builder', search_builder_name
25
25
  end
@@ -32,4 +32,4 @@ module Dbla
32
32
  generate 'dbla:routes'
33
33
  end
34
34
  end
35
- end
35
+ end
@@ -1,8 +1,8 @@
1
1
  module Dbla
2
2
  class RoutesGenerator < Rails::Generators::Base
3
-
3
+
4
4
  source_root File.expand_path('../templates', __FILE__)
5
-
5
+
6
6
  argument :controller_name , type: :string , default: "catalog"
7
7
 
8
8
  desc """
@@ -12,9 +12,9 @@ module Dbla
12
12
 
13
13
  def inject_dbla_routes
14
14
  # These will end up in routes.rb file in reverse order
15
- # we add em, since each is added at the top of file.
16
- # we want "root" to be FIRST for optimal url generation.
15
+ # we add em, since each is added at the top of file.
16
+ # we want "root" to be FIRST for optimal url generation.
17
17
  route("Dbla::Routes.for(self, :#{controller_name})")
18
18
  end
19
19
  end
20
- end
20
+ end
@@ -35,8 +35,8 @@ class Collection
35
35
 
36
36
  def has? f, *values
37
37
  # these are shenanigans to find a nested field
38
- if f.is_a? String and f.index('/')
39
- f = f.split('/')
38
+ if f.is_a? String and f.index('.')
39
+ f = f.split('.')
40
40
  f.inject(self) {|m,v| (m || {})[v]}
41
41
  else
42
42
  super
@@ -35,8 +35,8 @@ class Item
35
35
 
36
36
  def has? f, *values
37
37
  # these are shenanigans to find a nested field
38
- if f.is_a? String and f.index('/')
39
- f = f.split('/')
38
+ if f.is_a? String and f.index('.')
39
+ f = f.split('.')
40
40
  f.inject(self) {|m,v| (m || {})[v]}
41
41
  else
42
42
  super
@@ -10,7 +10,7 @@ module Dbla
10
10
  }
11
11
  def self.usage(key=:all)
12
12
  puts "Usage:"
13
- USAGE.select {|k,v| k == key || key == :all}.each{|k,v| puts v}
13
+ USAGE.select {|k,v| k == key || key == :all}.each{|k,v| puts v}
14
14
  end
15
15
  end
16
16
  end
@@ -43,9 +43,9 @@ namespace :dbla do
43
43
  end
44
44
  task config: :environment do
45
45
  if ENV['key']
46
- open(Dbla.config_path,'a') do |blob|
47
- blob.write YAML.dump('api_key' => ENV['key'])
48
- end
46
+ open(Dbla.config_path,'a') do |blob|
47
+ blob.write YAML.dump('api_key' => ENV['key'])
48
+ end
49
49
  else
50
50
  Dbla::ApiKey.usage(:config)
51
51
  end
@@ -54,4 +54,4 @@ namespace :dbla do
54
54
  Dbla::ApiKey.usage
55
55
  end
56
56
  end
57
- end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Audrey Altman
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: 4.1.10
28
+ - !ruby/object:Gem::Dependency
29
+ name: blacklight
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '5.10'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '5.10'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: sqlite3
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -104,6 +118,7 @@ files:
104
118
  - lib/dbla/engine.rb
105
119
  - lib/dbla/repository.rb
106
120
  - lib/dbla/response.rb
121
+ - lib/dbla/response/facets.rb
107
122
  - lib/dbla/routes.rb
108
123
  - lib/dbla/search_builder_behavior.rb
109
124
  - lib/dbla/version.rb