Class: DataGov::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/data_gov/client.rb

Instance Method Summary collapse

Instance Method Details

#get(path, params = {}) ⇒ Hash

Returns a parsed JSON hash.

Returns:

  • (Hash)

    a parsed JSON hash



8
9
10
11
12
13
14
# File 'lib/data_gov/client.rb', line 8

def get(path, params = {})
  response = connection.get(path) do |req|
    req.params = params
  end
  puts "#{response.status} #{response.env.url}"
  response.body
end

#paginated_get(path, accessor, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/data_gov/client.rb', line 16

def paginated_get(path, accessor, options = {})
  Enumerator.new do |yielder|
    params   = options.dup
    rows     = params.delete(:rows) { 5 }
    start    = params.delete(:start) { 1 }
    max      = params.delete(:max) { 10 }
    total    = 0

    loop do
      data = get(path, { rows: rows, start: start }.merge(params))

      total += data['result'][accessor].length

      data['result'][accessor].each do |element|
        yielder.yield element
      end

      start += rows
      puts "total: #{total}, max: #{max}, count: #{data['result']['count']}"
      break if total >= data['result']['count'] || total >= max
    end
  end
end