/**
 * archur - Search for and download Arch Linux packages
 * https://github.com/gavinhungry/archur
 */

(function() {
  'use strict';

  var Q = require('q');
  var request = require('request');
  var sprintf = require('sprintf');

  // Arch Linux JSON API
  var ARCH_URI = 'https://www.archlinux.org/packages';
  var PKG_URI = ARCH_URI + '/%s/%s/%s/json'; // repo/arch/pkg
  var SEARCH_URI = ARCH_URI + '/search/json/?repo=%s&arch=%s&q=%s';

  var archur = module.exports;

  /**
   * Get default search arguments
   *
   * @param {String} [repo] - repository name (lowercase)
   * @param {String} [arch] - system architecture
   * @return {Object}
   */
  var def = function(repo, arch) {
    return {
      repo: repo || 'core',
      arch: arch || 'x86_64'
    };
  };

  /**
   * Capitalize dash seperated strings
   *
   * @example
   * caps('foo-bar'); // "Foo-Bar"
   *
   * @param {String} str - input string
   * @return {String} str with capitalized words
   */
  var caps = function(str) {
    return str.split('-').map(function(sub) {
      return sub[0].toUpperCase() + sub.slice(1);
    }).join('-');
  };

  /**
   * Get remote JSON
   *
   * @param {String} uri
   * @return {Promise} to resolve to parsed Object, or reject with error
   */
  var json = function(uri) {
    var d = Q.defer();

    request(uri, function(error, response, data) {
      if (error || response.statusCode !== 200) {
        return d.reject(error);
      }

      try {
        var obj = JSON.parse(data);
      } catch(err) {
        return d.reject(err);
      }

      d.resolve(obj);
    });

    return d.promise;
  };

  /**
   * Search for packages
   *
   * @param {String} query - package query
   * @param {String} [repo] - repository name (lowercase), defaults to core
   * @param {String} [arch] - system architecture, defaults to x86_64
   * @return {Promise} to resolve to results, or reject with error
   */
  archur.search = function(query, repo, arch) {
    var args = def(repo, arch);
    var uri = sprintf(SEARCH_URI, caps(args.repo), args.arch, query);

    return json(uri).then(function(obj) {
      return obj.results;
    });
  };

  /**
   * Get data for a single package
   *
   * @param {String} pkg - package name
   * @param {String} [repo] - repository name (lowercase), defaults to core
   * @param {String} [arch] - system architecture, defaults to x86_64
   * @return {Promise} to resolve to result, or reject with error
   */
  archur.package = function(pkg, repo, arch) {
    var args = def(repo, arch);
    var uri = sprintf(PKG_URI, caps(args.repo), args.arch, pkg);

    return json(uri);
  };

})();

@@ -126,24 +126,56 @@
 e strict';%0A%0A
+  var archur = module.exports;%0A%0A
   var Q = re
@@ -476,330 +476,179 @@
 %0A%0A  
-var 
 archur
- = module.exports;%0A%0A  /**%0A   * Get default search arguments%0A   *%0A   * @param %7BString%7D %5Brepo%5D - repository name (lowercase)%0A   * @param %7BString%7D %5Barch%5D - system architecture%0A   * @return %7BObject%7D%0A   */%0A  var def = function(repo, arch) %7B%0A    return %7B%0A      repo: repo %7C%7C 'core',%0A      arch: arch %7C%7C 'x86_64'%0A    %7D;
+.defaults = %7B%0A    repo: 'core',%0A    arch: 'x86_64',%0A%0A    // same format as /etc/pacman.d/mirrorlist%0A    mirror: 'https://mirrors.kernel.org/archlinux/$repo/os/$arch'
 %0A  %7D
@@ -1624,38 +1624,8 @@
 name
- (lowercase), defaults to core
 %0A   
@@ -1670,36 +1670,16 @@
 itecture
-, defaults to x86_64
 %0A   * @r
@@ -1800,35 +1800,86 @@
     
-var args = def(repo, arch);
+repo = repo %7C%7C archur.defaults.repo;%0A    arch = arch %7C%7C archur.defaults.arch;%0A
 %0A   
@@ -1914,33 +1914,23 @@
 I, caps(
-args.
 repo), 
-args.
 arch, qu
@@ -1935,17 +1935,16 @@
 query);%0A
-%0A
     retu
@@ -2152,38 +2152,8 @@
 name
- (lowercase), defaults to core
 %0A   
@@ -2202,28 +2202,8 @@
 ture
-, defaults to x86_64
 %0A   
@@ -2326,121 +2326,599 @@
     
-var args = def(repo, arch);%0A    var uri = sprintf(PKG_URI, caps(args.repo), args.arch, pkg);%0A%0A    return json(uri
+repo = repo %7C%7C archur.defaults.repo;%0A    arch = arch %7C%7C archur.defaults.arch;%0A%0A    var uri = sprintf(PKG_URI, caps(repo), arch, pkg);%0A    return json(uri);%0A  %7D;%0A%0A  /**%0A   * Get a download URI for a package result%0A   *%0A   * @param %7BString%7D pkg - package name%0A   * @return %7BPromise%7D to resolve to download URI, or reject with error%0A   */%0A  archur.download = function(pkg, repo, arch) %7B%0A    return archur.package(pkg, repo, arch).then(function(obj) %7B%0A      return archur.defaults.mirror%0A        .replace('$repo', obj.repo)%0A        .replace('$arch', obj.arch)%0A        + '/' + obj.filename;%0A    %7D
 );%0A 

Add archur.download