Permalink
Browse files

initial beers app draft (heh)

  • Loading branch information...
0 parents commit cb686895117f659097cbbd6a11f554588ebea7ec @BigBlueHat BigBlueHat committed Apr 25, 2012
Showing with 1,240 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 .gitmodules
  3. +10 −0 .htaccess
  4. +19 −0 LICENSE
  5. +8 −0 README.markdown
  6. +69 −0 beers.php
  7. +64 −0 breweries.php
  8. +33 −0 index.php
  9. +12 −0 templates/beer.mustache
  10. +12 −0 templates/beers.mustache
  11. +9 −0 templates/breweries.mustache
  12. +24 −0 templates/brewery.mustache
  13. +931 −0 vendors/Mustache.php
  14. +1 −0 vendors/Slim
  15. +1 −0 vendors/Slim-Extras
  16. +40 −0 vendors/couchsimple.php
@@ -0,0 +1 @@
+.buildpath
@@ -0,0 +1,6 @@
+[submodule "vendors/Slim-Extras"]
+ path = vendors/Slim-Extras
+ url = https://github.com/codeguy/Slim-Extras.git
+[submodule "vendors/Slim"]
+ path = vendors/Slim
+ url = https://github.com/codeguy/Slim.git
@@ -0,0 +1,10 @@
+RewriteEngine On
+
+# Some hosts may require you to use the `RewriteBase` directive.
+# If you need to use the `RewriteBase` directive, it should be the
+# absolute physical path to the directory that contains this htaccess file.
+#
+# RewriteBase /
+
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^(.*)$ index.php [QSA,L]
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Couchbase, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,8 @@
+# Couchbase Beers
+
+Couchbase Beers is a sample PHP application using the [OpenBeerDB.com](http://openbeerdb.com/)
+dataset (which we ship with Couchbase 2.0) and our [PHP SDK](http://couchbase.com/develop/php/next).
+
+## Open Source License
+
+Couchbase Beers is released under the MIT public license.
@@ -0,0 +1,69 @@
+<?php
+function breweryUrl($name) {
+ return '../breweries/' . str_replace(' ' , '_', $name);
+}
+
+// GET /beers
+$app->get('/beers/', function() use ($app, $cb, $cbv) {
+ if ($by = $app->request()->get('by')) {
+ $by = str_replace('_', ' ', $by);
+ $qp = '&' . http_build_query(array('startkey' => '["' . $by . '"]',
+ 'endkey' => '["' . $by . '", "Z"]'));
+ } else {
+ $qp = '';
+ }
+ $url = '/beer-sample/_design/dev_beer/_view/brewery_beers?full_set=true&reduce=false' . $qp;
+ $beers = json_decode($cbv->send('GET', $url), true);
+ //$beers = $cb->view('_design/dev_beer/_view/beers', '');
+ $beers_by_brewery = array();
+ if (!isset($beers['error']) && $beers['total_rows'] > 0) {
+ $i = -1;
+ $beer_count = 0;
+ foreach ($beers['rows'] as $beer) {
+ // if this is a brewery entry...
+ if (count($beer['key']) == 1) {
+ // add the count to the last brewery
+ $i++;
+ $beer_count = 0;
+ $beers_by_brewery[$i] = array('brewery' => $beer['key'][0],
+ 'brewery_url' => breweryUrl($beer['key'][0]),
+ 'beers'=> array());
+ } else if (count($beer['key']) > 1) {
+ $beer_count++;
+ $beers_by_brewery[$i]['beers'][] = array('beer' => $beer['key'][1],
+ 'beer_url' => urlencode($beer['key'][1]));
+ $beers_by_brewery[$i]['beer_count'] = $beer_count;
+ }
+ }
+ $app->render('beers.mustache', array('beers_by_brewery'=>$beers_by_brewery));
+ } else {
+ echo $beers['reason'];
+ }
+});
+
+$app->get('/beers/:id', function($id) use ($app, $cb, $cbv) {
+ $url = '/beer-sample/beer_' . str_replace(' ', '_', urldecode($id));
+ $beer = json_decode($cbv->send('GET', $url), true);
+ if (isset($beer['error']) && $beer['error'] == 'not_found') {
+ $app->notFound();
+ }
+ if (isset($beer['brewery'])) {
+ $beer['brewery_url'] = breweryUrl($beer['brewery']);
+ }
+ $app->render('beer.mustache', $beer);
+});
+
+//POST route
+$app->post('/beers', function () {
+ echo 'This is a POST route';
+});
+
+//PUT route
+$app->put('/beers/:id', function ($id) {
+ echo 'This is a PUT route';
+});
+
+//DELETE route
+$app->delete('/beers/:id', function ($id) {
+ echo 'This is a DELETE route';
+});
@@ -0,0 +1,64 @@
+<?php
+// GET /breweries
+$app->get('/breweries/', function() use ($app, $cb, $cbv) {
+ // request reduced list of brewery names with their beer counts
+ $breweries = json_decode($cbv->send('GET', '/beer-sample/_design/dev_beer/_view/brewery_beers?full_set=true&group_level=1'), true);
+ //$breweries = $cb->view('_design/dev_beer/_view/brewery_beers?full_set=true&group_level=1', '');
+ // loop through the list of brewery names removing the brewery "entries" from the index
+ // and building up an array of ($brewery_name => $beer_count)
+ $beer_counts = array();
+ if (!isset($breweries['error']) && count($breweries['rows']) > 0) {
+ foreach ($breweries['rows'] as $entry) {
+ $beer_counts[] = array('name' => $entry['key'][0],
+ 'url' => urlencode(str_replace(' ', '_', $entry['key'][0])),
+ // the brewery doc itself adds to the count of docs
+ // so we're removing it here.
+ 'count' => $entry['value'] - 1,
+ 'beers_url' => '../beers/?by=' . $entry['key'][0]);
+ }
+ //print_r($beer_counts);
+ $app->render('breweries.mustache', array('breweries'=> $beer_counts));
+ //print_r(var_dump($cb->getMulti($ids)));
+ } else {
+ echo $breweries['reason'];
+ }
+ exit;
+ $ids = array();
+ if (!isset($breweries['error']) && $breweries['total_rows'] > 0) {
+ foreach ($breweries['rows'] as $beer) {
+ array_push($ids, $beer['id']);
+ }
+ print_r(var_dump($cb->getMulti($ids)));
+ //echo json_encode($ids);
+ } else {
+ echo $breweries['reason'];
+ }
+});
+
+$app->get('/breweries/:id', function($id) use ($app, $cb, $cbv) {
+ $brewery = json_decode($cbv->send('GET', '/beer-sample/brewery_' . str_replace(' ', '_', urldecode($id))), true);
+ if (isset($brewery['geo']['loc'])
+ && is_array($brewery['geo']['loc'])
+ && count($brewery['geo']['loc']) > 0) {
+ $brewery['geo']['latitude'] = $brewery['geo']['loc'][0];
+ $brewery['geo']['longitude'] = $brewery['geo']['loc'][1];
+ } else {
+ unset($brewery['geo']);
+ }
+ $app->render('brewery.mustache', $brewery);
+})->name('brewery');
+
+//POST route
+$app->post('/breweries', function () {
+ echo 'This is a POST route';
+});
+
+//PUT route
+$app->put('/breweries/:id', function ($id) {
+ echo 'This is a PUT route';
+});
+
+//DELETE route
+$app->delete('/breweries/:id', function ($id) {
+ echo 'This is a DELETE route';
+});
@@ -0,0 +1,33 @@
+<?php
+
+// require stuffs
+require 'vendors/Slim/Slim/Slim.php';
+require 'vendors/Slim-Extras/Views/MustacheView.php';
+require 'vendors/couchsimple.php';
+
+// set up the app
+MustacheView::$mustacheDirectory = 'vendors';
+$app = new Slim(array('view' => 'MustacheView'));
+
+// Setup Couchbase connected objects
+try {
+ $cb = new Couchbase("127.0.0.1:9000", "Administrator", "asdasd", "beer-sample");
+} catch (ErrorException $e) {
+ die($e->getMessage());
+}
+
+$cbv = new CouchSimple(array('host'=>'127.0.0.1', 'port'=>9500));
+
+// openbeers application goodness
+
+// GET route
+$app->get('/', function () {
+ echo '<a href="beers">beers!</a><br />';
+ echo '<a href="breweries">breweries!</a>';
+});
+// beer routes
+require_once 'beers.php';
+// brewery routes
+require_once 'breweries.php';
+// run, Slim, run
+$app->run();
@@ -0,0 +1,12 @@
+<div class="vproduct" data-last-updated="{{updated}}">
+ <h1 class="fn">{{name}}</h1>
+ <h4 class="brand">by <a href="{{brewery_url}}">{{brewery}}</a></h4>
+ <ul class="categories">
+ <li>{{category}}</li>
+ <li>{{style}}</li>
+ </ul>
+ <div class="description">{{description}}</div>
+ <ul class="attribs">
+ {{#abv}}<li class="abv"><abbr title="alcohol by volume">ABV</abbr>: {{abv}}</li>{{/abv}}
+ </ul>
+</div>
@@ -0,0 +1,12 @@
+<h1>Beers</h1>
+{{#beers_by_brewery}}
+<h4><a href="{{brewery_url}}">{{brewery}}</a> - {{beer_count}}</h4>
+<ul>
+{{#beers}}
+ <li><a href="{{beer_url}}">{{beer}}</a></li>
+{{/beers}}
+{{^beers}}
+ <li>Sorry, no beers :(</li>
+{{/beers}}
+</ul>
+{{/beers_by_brewery}}
@@ -0,0 +1,9 @@
+<h1>Breweries</h1>
+<ul>
+{{#breweries}}
+ <li>
+ <a href="{{url}}" title="more about {{name}}">{{name}}</a>
+ - <a href="{{beers_url}}">{{count}}</a>
+ </li>
+{{/breweries}}
+</ul>
@@ -0,0 +1,24 @@
+<div class="vcard" data-last-updated="{{updated}}">
+ <h1 class="org">{{name}}</h1>
+ {{#website}}<a class="url" href="{{website}}">website</a>{{/website}}
+ <div class="adr">
+ {{#address}}
+ <div class="street-address">{{.}}</div>
+ {{/address}}
+ <div>
+ <span class="locality">{{city}}</span>
+ <span class="region">{{state}}</span>
+ <span class="postal-code">{{code}}</span>
+ </div>
+ <div class="country-name">{{country}}</div>
+ {{#geo}}
+ <div class="geo" data-accuracy="{{accuracy}}">
+ <span class="latitude">{{latitude}}</span>,
+ <span class="longitude">{{longitude}}</span>
+ </div>
+ {{/geo}}
+ </div>
+ <div class="tel"><a href="tel:{{phone}}">{{phone}}</a></div>
+ <div class="note">{{description}}</div>
+</div>
+<a href="../beers/?by={{name}}">Beers List</a>
Oops, something went wrong.

0 comments on commit cb68689

Please sign in to comment.