Skip to content

Instantly share code, notes, and snippets.

@dleonard00
Last active April 26, 2017 16:48
Show Gist options
  • Save dleonard00/3b26b6e7398f6138d94677b65898ad4e to your computer and use it in GitHub Desktop.
Save dleonard00/3b26b6e7398f6138d94677b65898ad4e to your computer and use it in GitHub Desktop.
// implementation detail relevant whenever code is edited.
'use strict';

// cloud-provider / trigger specifc dependancy relevant only to a single cloud provider.
const functions = require('firebase-functions');

// business logic dependancies relevant when understanding solution details, and when making changes.
const moment = require('moment'); // Moments library to format dates.
const cors = require('cors')({origin: true});// CORS Express middleware to enable CORS Requests.

// debugging comment that is relevant when debugging, understanding solution details, making changes, and implementing or testing.
/**
 * Returns the server's date. You must provide a `format` URL query parameter or `format` vaue in
 * the request body with which we'll try to format the date.
 *
 * Format must follow the Node moment library. See: http://momentjs.com/
 *
 * Example format: "MMMM Do YYYY, h:mm:ss a".
 * Example request using URL query parameters:
 *   https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20Do%20YYYY%2C%20h%3Amm%3Ass%20a
 * Example request using request body with cURL:
 *   curl -H 'Content-Type: application/json' /
 *        -d '{"format": "MMMM Do YYYY, h:mm:ss a"}' /
 *        https://us-central1-<project-id>.cloudfunctions.net/date
 *
 * This endpoint supports CORS.
 */

// cloud provider specific(?) trigger interface.
exports.date = functions.https.onRequest((req, res) => {

  // start of body
  // Forbidding PUT requests.
  if (req.method === 'PUT') { // trigger specific implementation detail statement (cloud provider specific(?)) I only question if it's specific to a cloud provider because it may be standardized by express.
    res.status(403).send('Forbidden!'); // a trigger specific function-result (cloud provider specific(?))
  }


  // Enable CORS using the `cors` express middleware.
  cors(req, res, () => { // cloud provider specific (?) also trigger specific
    // Reading date format from URL query parameter.
    let format = req.query.format; // trigger specific statement
    // Reading date format from request body query parameter
    if (!format) { // body statement
      format = req.body.format; // trigger specific statement
    }
    
    const formattedDate = moment().format(format); // body result
    // end body

    console.log('Sending Formatted date:', formattedDate); // debugging statement
    res.status(200).send(formattedDate); // trigger specific function result
  });
});






/**
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment