Class: QuickSearch::OpenLibrarySearcher

Inherits:
Searcher
  • Object
show all
Defined in:
app/searchers/quick_search/open_library_searcher.rb

Instance Method Summary collapse

Instance Method Details

#author(value) ⇒ Object



46
47
48
49
50
51
52
# File 'app/searchers/quick_search/open_library_searcher.rb', line 46

def author(value)
  if value.has_key?('author_name')
    value['author_name'].join(', ')
  else
    ""
  end
end

#base_urlObject



32
33
34
# File 'app/searchers/quick_search/open_library_searcher.rb', line 32

def base_url
  "http://openlibrary.org/search.json"
end


58
59
60
# File 'app/searchers/quick_search/open_library_searcher.rb', line 58

def build_link(value)
  "http://www.openlibrary.org" + value['key']
end

#cover_image(value) ⇒ Object



70
71
72
73
74
75
76
# File 'app/searchers/quick_search/open_library_searcher.rb', line 70

def cover_image(value)
  if value.has_key?('cover_i')
    "https://covers.openlibrary.org/w/id/" + value['cover_i'].to_s + "-M.jpg"
  else
    ""
  end
end


54
55
56
# File 'app/searchers/quick_search/open_library_searcher.rb', line 54

def loaded_link
  "http://www.openlibrary.org/search?q=" + http_request_queries['uri_escaped']
end

#parametersObject



36
37
38
39
40
# File 'app/searchers/quick_search/open_library_searcher.rb', line 36

def parameters
  {
    'q' => http_request_queries['not_escaped'],
  }
end

#resultsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/searchers/quick_search/open_library_searcher.rb', line 9

def results
  if results_list
    results_list

  else
    @results_list = []

    @response['docs'].first(@per_page).each do |value|
      result = OpenStruct.new
      result.title = title(value)
      result.link = build_link(value)
      result.author = author(value)
      result.date = value['first_publish_year']
      result.fulltext = value['has_fulltext']
      result.thumbnail = cover_image(value)
      @results_list << result
    end

    @results_list
  end

end

#searchObject



4
5
6
7
# File 'app/searchers/quick_search/open_library_searcher.rb', line 4

def search
  resp = @http.get(base_url, parameters)
  @response = JSON.parse(resp.body)
end

#title(value) ⇒ Object



62
63
64
65
66
67
68
# File 'app/searchers/quick_search/open_library_searcher.rb', line 62

def title(value)
  if value.has_key?('subtitle')
    value['title'] + ': ' + value['subtitle']
  else
    value['title']
  end
end

#totalObject



42
43
44
# File 'app/searchers/quick_search/open_library_searcher.rb', line 42

def total
  @response['numFound']
end