Class: Restivus

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



78
79
80
# File 'lib/restivus.rb', line 78

def self.all
  @@results
end

.csv(filename) ⇒ Object




54
55
56
57
58
59
60
61
62
# File 'lib/restivus.rb', line 54

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



48
49
50
# File 'lib/restivus.rb', line 48

def self.fields
  @@fields
end

.find(id) ⇒ Object



68
69
70
# File 'lib/restivus.rb', line 68

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

.pk(key = "id") ⇒ Object



64
65
66
# File 'lib/restivus.rb', line 64

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

.where(conditions = {}) ⇒ Object



72
73
74
75
76
# File 'lib/restivus.rb', line 72

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

Instance Method Details

#base_uriObject



87
88
89
90
91
92
93
94
95
# File 'lib/restivus.rb', line 87

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




13
14
15
16
# File 'lib/restivus.rb', line 13

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

#delete_splat(params) ⇒ Object



82
83
84
85
# File 'lib/restivus.rb', line 82

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



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/restivus.rb', line 30

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



44
45
46
# File 'lib/restivus.rb', line 44

def resource_name
  self.class.name
end

#truncated_response(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/restivus.rb', line 18

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