Class: Socrata::View

Inherits:
Data show all
Defined in:
lib/socrata/data.rb

Constant Summary

Constants inherited from Socrata

DEFAULT_CONFIG

Instance Attribute Summary

Attributes inherited from Data

#data

Attributes inherited from Socrata

#batching, #config, #error

Instance Method Summary collapse

Methods inherited from Data

#id, #initialize, #method_missing

Methods inherited from Socrata

#batch_request, #initialize, #user, #view

Constructor Details

This class inherits a constructor from Socrata::Data

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Socrata::Data

Instance Method Details

#filter(filter = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/socrata/data.rb', line 80

def filter(filter = {})
  inline = {
    :name => "Inline Filter for #{self.id}",
    :id => self.id,
    :original_view_id => self.id,
    :query => filter
  }

  return post_request("/views/INLINE/rows.json?method=index", 
                      {:body => Util.camelize_keys(inline).to_json} )["data"]
end

#get_row(row_id) ⇒ Object



76
77
78
# File 'lib/socrata/data.rb', line 76

def get_row(row_id)
  return get_request("/views/#{self.id}/rows/#{row_id}.json")
end

#raw_rows(params = {}) ⇒ Object

Get the rows for this View as a raw array of arrays



37
38
39
# File 'lib/socrata/data.rb', line 37

def raw_rows(params = {})
  return get_request("/views/#{self.id}/rows.json", :query => params)
end

#rows(params = {}, by = "name") ⇒ Object

Get the rows as an array of hashes by their column ID or something overriden with the second param



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/socrata/data.rb', line 43

def rows(params = {}, by = "name")
  # TODO: I really hate this method, I'd like to redo it.

  # Get the raw rows
  response = get_request("/views/#{self.id}/rows.json", :query => params)

  # Create a mapping from array index to ID
  columns = response["meta"]["view"]["columns"]
  mapping = Hash.new
  columns.each_index do |idx|
    mapping[idx] = columns[idx][by]
  end

  # Loop over the rows, replacing each with a proper hash
  rows = response["data"]
  new_rows = Array.new
  rows.each do |row_arr|
    row_hash = Hash.new

    # Loop over the row, building our hash
    row_arr.each_index do |idx|
      row_hash[mapping[idx]] = row_arr[idx]
    end

    # Add a few meta columns
    row_hash["_id"] = row_arr[0]

    new_rows.push row_hash
  end

  return new_rows
end