'use strict';
/**
 * @module WP
 * @submodule PostsRequest
 * @beta
 */
var CollectionRequest = require( './shared/collection-request' );
var inherit = require( 'util' ).inherits;
var _ = require( 'lodash' );

/**
 * PostsRequest extends CollectionRequest to handle the /posts API endpoint
 *
 * @class PostsRequest
 * @constructor
 * @extends CollectionRequest
 * @param {Object} options A hash of options for the PostsRequest instance
 * @param {String} options.endpoint The endpoint URI for the invoking WP instance
 * @param {String} [options.username] A username for authenticating API requests
 * @param {String} [options.password] A password for authenticating API requests
 */
function PostsRequest( options ) {
	/**
	 * Configuration options for the request such as the endpoint for the invoking WP instance
	 * @property _options
	 * @type Object
	 * @private
	 * @default {}
	 */
	this._options = options || {};

	/**
	 * A hash of filter values to parse into the final request URI
	 * @property _filters
	 * @type Object
	 * @private
	 * @default {}
	 */
	this._filters = {};

	/**
	 * A hash of taxonomy terms to parse into the final request URI
	 * @property _taxonomyFilters
	 * @type Object
	 * @private
	 * @default {}
	 */
	this._taxonomyFilters = {};

	/**
	 * A hash of non-filter query parameters
	 *
	 * @property _params
	 * @type Object
	 * @private
	 * @default {}
	 */
	this._params = {};

	/**
	 * A hash of values to assemble into the API request path
	 *
	 * @property _path
	 * @type Object
	 * @private
	 * @default {}
	 */
	this._path = {};

	/**
	 * The URL template that will be used to assemble endpoint paths
	 *
	 * @property _template
	 * @type String
	 * @private
	 * @default 'posts(/:id)(/:action)(/:actionId)'
	 */
	this._template = 'posts(/:id)(/:action)(/:actionId)';

	/**
	 * @property _supportedMethods
	 * @type Array
	 * @private
	 * @default [ 'head', 'get', 'post' ]
	 */
	this._supportedMethods = [ 'head', 'get', 'post' ];

	// Default all .posts() requests to assume a query against the WP API v2 endpoints
	this.namespace( 'wp/v2' );
}

inherit( PostsRequest, CollectionRequest );

/**
 * A hash table of path keys and regex validators for those path elements
 *
 * @property _pathValidators
 * @type Object
 * @private
 */
PostsRequest.prototype._pathValidators = {

	/**
	 * ID must be an integer
	 *
	 * @property _pathValidators.id
	 * @type {RegExp}
	 */
	id: /^\d+$/,

	/**
	 * Action must be one of 'meta' or 'revisions'
	 *
	 * @property _pathValidators.action
	 * @type {RegExp}
	 */
	action: /(meta|revisions)/
};

/**
 * Specify a post ID to query
 *
 * @method id
 * @chainable
 * @param {Number} id The ID of a post to retrieve
 * @return {PostsRequest} The PostsRequest instance (for chaining)
 */
PostsRequest.prototype.id = function( id ) {
	this._path.id = parseInt( id, 10 );
	this._supportedMethods = [ 'head', 'get', 'put', 'post', 'delete' ];

	return this;
};

/**
 * Specify that we are retrieving Post Meta (forces basic auth)
 *
 * Either return a collection of all meta objects for the specified post,
 * or (if a meta ID was provided) return the requested meta object.
 *
 * @method meta
 * @chainable
 * @param {Number} [metaId] ID of a specific meta property to retrieve
 * @return {PostsRequest} The PostsRequest instance (for chainin)
 */
PostsRequest.prototype.meta = function( metaId ) {
	this._path.action = 'meta';
	this._supportedMethods = [ 'head', 'get', 'post' ];
	this._path.actionId = parseInt( metaId, 10 ) || null;

	if ( this._path.actionId ) {
		this._supportedMethods = [ 'head', 'get', 'put', 'post', 'delete' ];
	}

	return this.auth();
};

/**
 * Specify that we are requesting the revisions for a specific post (forces basic auth)
 *
 * @method revisions
 * @chainable
 * @return {PostsRequest} The PostsRequest instance (for chaining)
 */
PostsRequest.prototype.revisions = function() {
	this._path.action = 'revisions';
	this._supportedMethods = [ 'head', 'get' ];

	return this.auth();
};

/**
 * Adds the Year filter into the request to retrieve posts for a given year
 *
 * @method year
 * @chainable
 * @param {Number} year Integer representation of year requested
 * @returns {PostsRequest} The PostsRequest instance (for chaining)
 */
PostsRequest.prototype.year = function( year ) {
	return this.filter( 'year', year );
};

/**
 * Add the monthnum filter into the request to retrieve posts for a given month
 *
 * Filters posts by a given month in either Integer or String Format.
 *
 * @method month
 * @chainable
 * @param {Number|String} month Integer for month or month string ("january")
 * @returns {PostsRequest} The PostsRequest instance (for chaining)
 */
PostsRequest.prototype.month = function( month ) {
	if ( _.isString( month ) ) {
		// Append a arbitrary day and year to the month to parse the string into a Date
		month = new Date( Date.parse( month + ' 1, 2012' ) );
		// If month is NaN, then the passed string is not a valid month.
		if ( isNaN( month ) ) {
			return this;
		}
		// JS Dates are 0 indexed, WP API requires a 1 indexed integer.
		month = month.getMonth() + 1;
	}
	// If month is a Number, add the monthnum filter to the request
	if ( _.isNumber( month ) ) {
		return this.filter( 'monthnum', month );
	}
	return this;
};

/**
 * Add the day filter into the request to retrieve posts for a given day
 *
 * @method day
 * @chainable
 * @param {Number} day Integer representation of the day requested
 * @returns {PostsRequest} The PostsRequest instance (for chaining)
 */
PostsRequest.prototype.day = function( day ) {
	return this.filter( 'day', day );
};

module.exports = PostsRequest;

@@ -176,37 +176,8 @@
 its;
-%0Avar _ = require( 'lodash' );
 %0A%0A/*
@@ -3966,1615 +3966,8 @@
 %7D;%0A%0A
-/**%0A * Adds the Year filter into the request to retrieve posts for a given year%0A *%0A * @method year%0A * @chainable%0A * @param %7BNumber%7D year Integer representation of year requested%0A * @returns %7BPostsRequest%7D The PostsRequest instance (for chaining)%0A */%0APostsRequest.prototype.year = function( year ) %7B%0A%09return this.filter( 'year', year );%0A%7D;%0A%0A/**%0A * Add the monthnum filter into the request to retrieve posts for a given month%0A *%0A * Filters posts by a given month in either Integer or String Format.%0A *%0A * @method month%0A * @chainable%0A * @param %7BNumber%7CString%7D month Integer for month or month string (%22january%22)%0A * @returns %7BPostsRequest%7D The PostsRequest instance (for chaining)%0A */%0APostsRequest.prototype.month = function( month ) %7B%0A%09if ( _.isString( month ) ) %7B%0A%09%09// Append a arbitrary day and year to the month to parse the string into a Date%0A%09%09month = new Date( Date.parse( month + ' 1, 2012' ) );%0A%09%09// If month is NaN, then the passed string is not a valid month.%0A%09%09if ( isNaN( month ) ) %7B%0A%09%09%09return this;%0A%09%09%7D%0A%09%09// JS Dates are 0 indexed, WP API requires a 1 indexed integer.%0A%09%09month = month.getMonth() + 1;%0A%09%7D%0A%09// If month is a Number, add the monthnum filter to the request%0A%09if ( _.isNumber( month ) ) %7B%0A%09%09return this.filter( 'monthnum', month );%0A%09%7D%0A%09return this;%0A%7D;%0A%0A/**%0A * Add the day filter into the request to retrieve posts for a given day%0A *%0A * @method day%0A * @chainable%0A * @param %7BNumber%7D day Integer representation of the day requested%0A * @returns %7BPostsRequest%7D The PostsRequest instance (for chaining)%0A */%0APostsRequest.prototype.day = function( day ) %7B%0A%09return this.filter( 'day', day );%0A%7D;%0A%0A
 modu

Remove duplicate date query code in postsrequest