Class: Scholar::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/scholar/search.rb

Overview

A Search object containing search results for a query (currently only searches the Google Books API).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Search

Searches for sources in Google Books.

Attributes

  • query - The search term.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scholar/search.rb', line 19

def initialize(query)
  @@connection = Faraday.new(:url => "https://www.googleapis.com") do |f|
    f.request  :json
    f.response :json, :content_type => /\bjson$/
    f.adapter  Faraday.default_adapter
  end

  # Remember the query.
  @query = query

  # Get the raw data.
  @results = perform!(query)

  # Turn that data into something a Citation class can understand.
  @results = format!(@results)

  self
end

Instance Attribute Details

#queryObject (readonly)

The query performed.



9
10
11
# File 'lib/scholar/search.rb', line 9

def query
  @query
end

#resultsObject (readonly)

The results of the query.



12
13
14
# File 'lib/scholar/search.rb', line 12

def results
  @results
end

Instance Method Details

#to_hashObject

Return as hash.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scholar/search.rb', line 39

def to_hash
  hash = {}

  instance_variables.each do |v|
    hash[v.to_s[1..-1].to_sym] = instance_variable_get(v)
  end

  hash[:results].each do |c|
    hash[:results][hash[:results].index(c)] = c.to_hash
  end

  hash
end