├── .gitignore ├── .travis.yml ├── Readme.md ├── index.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "6" 5 | - "5" 6 | - "4" 7 | - "0.10" -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # request-x-ray 2 | [![Build Status](https://travis-ci.org/Crazometer/request-x-ray.svg?branch=master)](https://travis-ci.org/Crazometer/request-x-ray) 3 | 4 | Simple [request](https://github.com/request/request) driver for [x-ray](https://github.com/lapwinglabs/x-ray). Useful for storing cookies, setting custom headers, and changing HTTP methods. 5 | 6 | ## Installation 7 | 8 | ``` 9 | npm install request-x-ray 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | const x = require('x-ray')() 16 | const makeDriver = require('request-x-ray') 17 | 18 | const options = { 19 | method: "GET", //Set HTTP method 20 | jar: true, //Enable cookies 21 | headers: { //Set headers 22 | "User-Agent": "Firefox/48.0" 23 | } 24 | } 25 | 26 | const driver = makeDriver(options) //Create driver 27 | 28 | x.driver(driver) //Set driver 29 | 30 | x("http://www.google.com", "title")(function(err, res) { 31 | console.log("Page retrieved with request!") 32 | }) 33 | ``` 34 | ## API 35 | 36 | ### makeDriver([opts|fn]) 37 | 38 | Creates a driver to be used by `xray.driver()`. Possible arguments are a request [options object](https://github.com/request/request#requestoptions-callback) or a `request` instance e.g. `request.defaults()`. 39 | 40 | ## License 41 | 42 | MIT License 43 | 44 | Copyright (c) 2016 Justin Sprigg 45 | 46 | Permission is hereby granted, free of charge, to any person obtaining a copy 47 | of this software and associated documentation files (the "Software"), to deal 48 | in the Software without restriction, including without limitation the rights 49 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 50 | copies of the Software, and to permit persons to whom the Software is 51 | furnished to do so, subject to the following conditions: 52 | 53 | The above copyright notice and this permission notice shall be included in all 54 | copies or substantial portions of the Software. 55 | 56 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 57 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 58 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 59 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 60 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 61 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 62 | SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Request = require('request') 2 | 3 | function makeDriver(opts) { 4 | if (typeof opts === "function") { 5 | var request = opts 6 | } else { 7 | var request = Request.defaults(opts) 8 | } 9 | 10 | return function driver(context, callback) { 11 | var url = context.url 12 | 13 | request(url, function(err, response, body) { 14 | return callback(err, body) 15 | }) 16 | } 17 | } 18 | 19 | module.exports = makeDriver -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "request-x-ray", 3 | "version": "0.1.4", 4 | "description": "Simple request driver for x-ray", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "author": "Justin Sprigg ", 10 | "license": "MIT", 11 | "dependencies": { 12 | "request": "^2.74.0" 13 | }, 14 | "peerDependencies": { 15 | "x-ray-crawler": "^2.0.0" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/Crazometer/request-x-ray.git" 20 | }, 21 | "engines": { 22 | "node": ">= 0.10" 23 | }, 24 | "keywords": [ 25 | "x-ray", 26 | "driver", 27 | "request" 28 | ], 29 | "bugs": { 30 | "url": "https://github.com/Crazometer/request-x-ray/issues" 31 | }, 32 | "homepage": "https://github.com/Crazometer/request-x-ray#readme", 33 | "devDependencies": { 34 | "mocha": "^3.0.2", 35 | "x-ray": "^2.3.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var request = require('request') 2 | var Xray = require('x-ray') 3 | var makeDriver = require('../') 4 | var assert = require('assert') 5 | 6 | describe('Driver', function() { 7 | describe('Arguments', function() { 8 | it('Should work with no argument', function(done) { 9 | var x = Xray() 10 | var driver = makeDriver() 11 | x.driver(driver) 12 | 13 | testInstance(x, done) 14 | }) 15 | 16 | it('Should work with an object argument', function(done) { 17 | var x = Xray() 18 | var driver = makeDriver({}) 19 | x.driver(driver) 20 | 21 | testInstance(x, done) 22 | }) 23 | 24 | it('Should work with a function argument', function(done) { 25 | var x = Xray() 26 | var driver = makeDriver(request.defaults({method:"POST"})) 27 | x.driver(driver) 28 | 29 | testInstance(x, done) 30 | }) 31 | }) 32 | 33 | describe('Errors', function() { 34 | it('Should pass on errors', function(done) { 35 | var x = Xray() 36 | var driver = makeDriver() 37 | x.driver(driver) 38 | 39 | testInstance(x, "http://NotARealUrl.com", function(err, res) { 40 | assert(err) 41 | done() 42 | }) 43 | }) 44 | }) 45 | }) 46 | 47 | function testInstance(r, url, callback) { 48 | if(!callback) { 49 | callback = url 50 | url = "https://news.ycombinator.com" 51 | } 52 | 53 | r(url, 'title')(callback) 54 | } --------------------------------------------------------------------------------