Class: Crunchbase::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/crunchbase/search.rb

Overview

The Search class provides access to the Crunchbase search API. To perform a search, call the find class method, which returns an object of the Search class. This object represents an array of SearchResult objects, which may be addressed in a way analogous to an Array. These results are loaded on demand, in line with the CB API which returns results in pages of 10. When requesting a result index that has not been loaded yet, a new request is made to fetch it, resulting in a small delay. The class implements the Enumerable module, allowing usage of map, select, etc. If this is not sufficient, and full access to the underlying array is required, you may call to_ary, which will return the entire array including all results. If not all results have been fetched yet, there will be a delay to retrieve them, so consider this if your search contains a large number of results.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, json) ⇒ Search

Returns a new instance of Search.



27
28
29
30
31
32
33
# File 'lib/crunchbase/search.rb', line 27

def initialize(query, json)
  @query = query
  @results = []
  @size = json["total"]
  @crunchbase_url = json["crunchbase_url"]
  populate_results(json)
end

Instance Attribute Details

#crunchbase_urlObject (readonly)

Returns the value of attribute crunchbase_url.



18
19
20
# File 'lib/crunchbase/search.rb', line 18

def crunchbase_url
  @crunchbase_url
end

#sizeObject (readonly) Also known as: length

Returns the value of attribute size.



18
19
20
# File 'lib/crunchbase/search.rb', line 18

def size
  @size
end

Class Method Details

.find(query) ⇒ Object

Performs a Crunchbase search for query.



22
23
24
25
# File 'lib/crunchbase/search.rb', line 22

def self.find(query)
  j = API.search(query)
  s = Search.new(query, j)
end

Instance Method Details

#[](*args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/crunchbase/search.rb', line 35

def [](*args)
  case args.length
  when 1
    key = args[0]
    if key.kind_of?(Integer)
      get_single_key(key)
    elsif key.kind_of?(Range)
      get_range(key)
    end
  when 2
    start = args[0]
    length = args[1]
    start = @size + start if start < 0
    get_range(start..start+length-1)
  end
end

#each(&block) ⇒ Object

Calls block once for each search result, passing that item as a parameter.



61
62
63
# File 'lib/crunchbase/search.rb', line 61

def each(&block) # :yields: result
  0.upto(@size - 1) {|x| yield self[x]}
end

#to_aryObject

Returns array of all search results (not just ones currently loaded.) This enables the user to take advantage of all Array methods, not just the ones implmented on Search.



55
56
57
# File 'lib/crunchbase/search.rb', line 55

def to_ary
  self.map{|result| result}
end