Class: RestivusGis

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/restivus-gis.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



80
81
82
# File 'lib/restivus-gis.rb', line 80

def self.all
  @@results
end

.csv(filename) ⇒ Object




56
57
58
59
60
61
62
63
64
# File 'lib/restivus-gis.rb', line 56

def self.csv(filename)
  csv_data = CSV.read filename
  headers = csv_data.shift.map {|i| i.to_s }
  headers.map! {|h| h.gsub(" ", "_")}
  @@fields = headers
  string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
  array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }
  @@results = array_of_hashes
end

.fieldsObject



50
51
52
# File 'lib/restivus-gis.rb', line 50

def self.fields
  @@fields
end

.find(id) ⇒ Object



70
71
72
# File 'lib/restivus-gis.rb', line 70

def self.find(id)
  where(@@pk => id.to_s).first
end

.pk(key = "id") ⇒ Object



66
67
68
# File 'lib/restivus-gis.rb', line 66

def self.pk(key="id")
  @@pk = key
end

.where(conditions = {}) ⇒ Object



74
75
76
77
78
# File 'lib/restivus-gis.rb', line 74

def self.where(conditions={})    
  results = @@results.select do |result|
    conditions.all? { |k,v| result[k] == v }
  end
end

Instance Method Details

#base_uriObject



89
90
91
92
93
94
95
96
97
# File 'lib/restivus-gis.rb', line 89

def base_uri
  uri = "http://#{request.host}"
  
  if request.port
    uri << ":#{request.port}"
  end
  
  uri
end

#curl_req(url) ⇒ Object

Doc helpers these methods generate text for the docs pages




15
16
17
18
# File 'lib/restivus-gis.rb', line 15

def curl_req(url)
  #`curl #{url}`
  RestClient.get(url)
end

#delete_splat(params) ⇒ Object



84
85
86
87
# File 'lib/restivus-gis.rb', line 84

def delete_splat(params)
  %w[splat captures resource].each {|w| params.delete(w)}
  params
end

#format_curl_req(url, description = "TODO", http = "TODO", url_schema = "TODO", div = "TODO") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/restivus-gis.rb', line 32

def format_curl_req(url, description="TODO", http="TODO", url_schema="TODO", div="TODO")
  result = {
    :cmd => "$ curl #{url}",
    :raw_response => curl_req(url),
    :pretty_response => truncated_response(url),
    :description => description,
    :http_verb => http,
    :url_schema => url_schema,
    :div_id => div
  }
    
  result
end

#resource_nameObject



46
47
48
# File 'lib/restivus-gis.rb', line 46

def resource_name
  self.class.name
end

#truncated_response(url) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/restivus-gis.rb', line 20

def truncated_response(url)
  json = curl_req(url)
  h = JSON.parse(json)
  
  if h["results"]
    h["results"] = h["results"].first(3)
    return JSON.pretty_generate(h)
  else
    return JSON.pretty_generate(h)
  end
end