Browse Source

round up some tweaks from a while back

Brennen Bearnes 5 months ago
parent
commit
4c7b329a94
2 changed files with 21 additions and 8 deletions
  1. 11
    5
      app/index.php
  2. 10
    3
      lib/Adafruit/FeedMangler/Mangler.php

+ 11
- 5
app/index.php View File

@@ -1,22 +1,28 @@
1 1
 <?php
2 2
 require '/var/www-php/Adafruit-Feed-Mangler/app.rc.php';
3 3
 
4
-use \SparkLib\Fail;
5
-
6 4
 $app = new \Slim\Slim;
7
-$app->config('debug', true);
5
+$app->config('debug', false);
8 6
 
9 7
 $app->error(function (\Exception $e) use ($app) {
10 8
   print "something broke";
11 9
 });
12 10
 
13 11
 $app->get('/', function () use ($app) {
14
-  $app->response->headers->set('Content-Type', 'application/atom+xml');
12
+
15 13
   $term = 'adafruit';
16 14
   if ($app->request->get('search')) {
17 15
     $term = $app->request->get('search');
18 16
   }
19
-  print \Adafruit\FeedMangler\Atom::searchResults($term);
17
+
18
+  if ($app->request->get('type') === 'json') {
19
+    $app->response->headers->set('Content-Type', 'application/json');
20
+    print \Adafruit\FeedMangler\Mangler::getSearchJson($term);
21
+  } else {
22
+    $app->response->headers->set('Content-Type', 'application/atom+xml');
23
+    print \Adafruit\FeedMangler\Atom::searchResults($term);
24
+  }
25
+
20 26
 });
21 27
 
22 28
 $app->run();

+ 10
- 3
lib/Adafruit/FeedMangler/Mangler.php View File

@@ -5,7 +5,13 @@ use \Doctrine\Common\Cache\FilesystemCache;
5 5
 
6 6
 class Mangler {
7 7
 
8
-  public static function getSearchData ($term) {
8
+  public static function getSearchData ($term)
9
+  {
10
+    return json_decode(self::getSearchJson($term), true /* use assoc array */);
11
+  }
12
+
13
+  public static function getSearchJson ($term)
14
+  {
9 15
     // example: https://api.hackaday.io/v1/search?api_key=7yRgvQsCczOev&search_term=test
10 16
     $query = http_build_query([
11 17
       'api_key'     => API_KEY,
@@ -17,14 +23,15 @@ class Mangler {
17 23
       return file_get_contents($search_url);
18 24
     });
19 25
 
20
-    return json_decode($raw_search_json, true /* use assoc array */);
26
+    return $raw_search_json;
21 27
   }
22 28
 
23 29
   /**
24 30
    * Get a cached value by key, or stash the value returned from $callback
25 31
    * under that key and return it.
26 32
    */
27
-  protected static function getOrSetCache ($key, $callback, $expire = 600) {
33
+  protected static function getOrSetCache ($key, $callback, $expire = 600)
34
+  {
28 35
     $cache = new FilesystemCache('/tmp');
29 36
     $cache->setNamespace('hackaday_projecten');
30 37
 

Loading…
Cancel
Save