Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Namespace app

Give it a name too (BlackBook).
  • Loading branch information...
commit 426dede9a5d6dfcf92a9cabf350e3cc5995ba105 1 parent 2412e70
Jon authored

Showing 2 changed files with 96 additions and 89 deletions. Show diff stats Hide diff stats

  1. 89  address.rb
  2. 96  app.rb
89  address.rb
... ...
@@ -1,89 +0,0 @@
1  
-#!/usr/bin/ruby
2  
-# Jonathan D. Stott <jonathan.stott@gmail.com>
3  
-require 'sinatra'
4  
-require 'mongo'
5  
-require 'views'
6  
-
7  
-set :mustache, { :templates => 'templates' }
8  
-
9  
-helpers do
10  
-  def addresses
11  
-    DB['addresses']
12  
-  end
13  
-
14  
-  def clean_params(param_set=params)
15  
-    case param_set
16  
-    when Array
17  
-      param_set.each do |v|
18  
-        if Array === v or Hash === v
19  
-          clean_params(v)
20  
-        end
21  
-      end
22  
-      param_set.delete_if { |v| v.empty? or v =~ /\A\s*\z/ }
23  
-    when Hash
24  
-      param_set.each do |k, v|
25  
-        if Array === v or Hash === v
26  
-          clean_params(v)
27  
-        end
28  
-      end
29  
-      param_set.delete_if { |k, v| v.empty? or v =~ /\A\s*\z/ }
30  
-    else
31  
-      param_set
32  
-    end
33  
-  end
34  
-end
35  
-
36  
-before do
37  
-  content_type :html, :charset => 'utf-8'
38  
-  cache_control :private, :no_cache
39  
-end
40  
-
41  
-get '/' do
42  
-  @addresses = addresses.find({}, :fields => [:name, :page], :sort => [[:page, :asc]]).to_a
43  
-  mustache :index
44  
-end
45  
-
46  
-get '/new' do
47  
-  @person = {}
48  
-  mustache :edit
49  
-end
50  
-
51  
-get '/:page' do |page|
52  
-  @person = addresses.find_one(:page => page)
53  
-  not_found unless @person
54  
-  mustache :show
55  
-end
56  
-
57  
-get '/:page/edit' do |page|
58  
-  @person = addresses.find_one(:page => page) || {'page' => page}
59  
-  mustache :edit
60  
-end
61  
-
62  
-post '/:page' do |page|
63  
-  throw :halt, [400, "Need a person in there to hold addresses"] unless params['person'] and Hash === params['person']
64  
-  # clean up the params
65  
-  params.delete_if { |k,v| "person" != k }
66  
-  params['person'].delete_if { |k,v| !%w|name numbers emails addresses|.include?(k) }
67  
-  params['person']['page'] = page
68  
-  clean_params
69  
-
70  
-  @person = addresses.find_one({:page => page}, :fields => [:_id]) || {}
71  
-  @person.merge!(params['person'])
72  
-
73  
-  addresses.save(@person)
74  
-  redirect "/#{page}"
75  
-end
76  
-
77  
-post '/' do
78  
-  throw :halt, [400, "Need a person in there to hold addresses"] unless params['person'] and Hash === params['person']
79  
-  # clean up the params
80  
-  params.delete_if { |k,v| "person" != k }
81  
-  params['person'].delete_if { |k,v| !%w|name page numbers emails addresses|.include?(k) }
82  
-  clean_params
83  
-
84  
-  @person = addresses.find_one({:page => person['page']}, :fields => [:_id]) || {}
85  
-  @person.merge!(params['person'])
86  
-
87  
-  addresses.save(@person)
88  
-  redirect "/#{person['page']}"
89  
-end
96  app.rb
... ...
@@ -0,0 +1,96 @@
  1
+#!/usr/bin/ruby
  2
+# Jonathan D. Stott <jonathan.stott@gmail.com>
  3
+require 'sinatra/base'
  4
+require 'mongo'
  5
+require 'views'
  6
+
  7
+
  8
+module BlackBook
  9
+  class App < ::Sinatra::Base
  10
+
  11
+    register Mustache::Sinatra
  12
+    set :mustache, { :templates => 'templates' }
  13
+
  14
+    helpers do
  15
+      def addresses
  16
+        settings.mongo['addresses']
  17
+      end
  18
+
  19
+      def clean_params(param_set=params)
  20
+        case param_set
  21
+        when Array
  22
+          param_set.each do |v|
  23
+            if Array === v or Hash === v
  24
+              clean_params(v)
  25
+            end
  26
+          end
  27
+          param_set.delete_if { |v| v.empty? or v =~ /\A\s*\z/ }
  28
+        when Hash
  29
+          param_set.each do |k, v|
  30
+            if Array === v or Hash === v
  31
+              clean_params(v)
  32
+            end
  33
+          end
  34
+          param_set.delete_if { |k, v| v.empty? or v =~ /\A\s*\z/ }
  35
+        else
  36
+          param_set
  37
+        end
  38
+      end
  39
+    end
  40
+
  41
+    before do
  42
+      content_type :html, :charset => 'utf-8'
  43
+      cache_control :private, :no_cache
  44
+    end
  45
+
  46
+    get '/' do
  47
+      @addresses = addresses.find({}, :fields => [:name, :page], :sort => [[:page, :asc]]).to_a
  48
+      mustache :index
  49
+    end
  50
+
  51
+    get '/new' do
  52
+      @person = {}
  53
+      mustache :edit
  54
+    end
  55
+
  56
+    get '/:page' do |page|
  57
+      @person = addresses.find_one(:page => page)
  58
+      not_found unless @person
  59
+      mustache :show
  60
+    end
  61
+
  62
+    get '/:page/edit' do |page|
  63
+      @person = addresses.find_one(:page => page) || {'page' => page}
  64
+      mustache :edit
  65
+    end
  66
+
  67
+    post '/:page' do |page|
  68
+      throw :halt, [400, "Need a person in there to hold addresses"] unless params['person'] and Hash === params['person']
  69
+      # clean up the params
  70
+      params.delete_if { |k,v| "person" != k }
  71
+      params['person'].delete_if { |k,v| !%w|name numbers emails addresses|.include?(k) }
  72
+      params['person']['page'] = page
  73
+      clean_params
  74
+
  75
+      @person = addresses.find_one({:page => page}, :fields => [:_id]) || {}
  76
+      @person.merge!(params['person'])
  77
+
  78
+      addresses.save(@person)
  79
+      redirect "/#{page}"
  80
+    end
  81
+
  82
+    post '/' do
  83
+      throw :halt, [400, "Need a person in there to hold addresses"] unless params['person'] and Hash === params['person']
  84
+      # clean up the params
  85
+      params.delete_if { |k,v| "person" != k }
  86
+      params['person'].delete_if { |k,v| !%w|name page numbers emails addresses|.include?(k) }
  87
+      clean_params
  88
+
  89
+      @person = addresses.find_one({:page => person['page']}, :fields => [:_id]) || {}
  90
+      @person.merge!(params['person'])
  91
+
  92
+      addresses.save(@person)
  93
+      redirect "/#{person['page']}"
  94
+    end
  95
+  end
  96
+end

0 notes on commit 426dede

Please sign in to comment.
Something went wrong with that request. Please try again.