Class: Dbla::Repository

Inherits:
Blacklight::AbstractRepository
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/dbla/repository.rb

Instance Method Summary collapse

Instance Method Details

#api_keyObject



49
50
51
# File 'lib/dbla/repository.rb', line 49

def api_key
  Dbla.config.fetch(:api_key)
end

#find(id, params = {}) ⇒ Object



6
7
8
9
# File 'lib/dbla/repository.rb', line 6

def find id, params = {}
  data = get("#{url}/#{id}?api_key=#{api_key}")
  Response.new(data, params,{})
end

#get(uri) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dbla/repository.rb', line 41

def get(uri)
  uri = URI(uri)
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Get.new uri
    response = http.request request
    return JSON.parse(response.body)
  end
end

#search(params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dbla/repository.rb', line 11

def search params = {}
  data = nil
  #TODO Move this into a SearchBuilder, add a generator
  if params['q']
    q = "?api_key=#{api_key}&q=#{params['q']}"
    fq = []
    blacklight_config.facet_fields.each do |f|
      # [fiendName, facetConfig]
      next unless f[0] =~ /^(sourceResource|provider|object|intermediateProvider|dataProvider)/
      fqv = f[0]
      fqv = fqv + ':' + f[1].pin if f[1].pin
      fq << fqv
    end
    q << "&facets=#{fq.join(',')}" unless fq.empty?
    if params.page
      q << "&page=#{params.page}"
    end
    if params.rows
      q << "&page_size=#{params.rows}"
    end
    params.facet_filters do |facet_field, value|
      value = "\"#{value}\"" if value.index(' ')
      q << "&#{facet_field}=#{CGI::escape(value)}"
    end
    puts url + q
    data = get(url + q)
  end
  Response.new(data, params,{})
end

#urlObject



53
54
55
56
# File 'lib/dbla/repository.rb', line 53

def url
  # REVIEW: What if the URL does not have a trailing /; Should it be `File.join?`
  @url ||= (Dbla.config.fetch(:url) + blacklight_config.document_model.name.downcase.pluralize).freeze
end