Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:embedly/embedly-node

Conflicts:
	bin/embedly_oembed.js
	lib/embedly.js
	package.json
	test/objectify-test.js
	test/oembed-test.js
	test/services-test.js
  • Loading branch information...
commit bb73bdd5c9ac96a3e0ae370a913e7514d6dd4a48 2 parents a3b9fcd + 6e53e2c
@dokipen dokipen authored
View
14 bin/embedly_oembed.js
@@ -1,9 +1,11 @@
#!/usr/bin/env node
+
var method = require('path').basename(__filename).match(/^embedly_([^.]+)(\.js)?$/)[1]
- , embedly = require("../lib/embedly")
+ , embedly = require('../')
, args = process.argv.slice(2)
, opts =
{ host: null
+ , secure: false
, key: process.env.EMBEDLY_KEY
, params: {urls: []}
}
@@ -18,9 +20,9 @@ var method = require('path').basename(__filename).match(/^embedly_([^.]+)(\.js)?
, ' -k, --key Embedly Pro key. [default:'
, ' EMBEDLY_KEY environmental variable]'
, ' -o, --option NAME=VALUE Set option to be passed as a query parameter.'
+ , ' -s, --secure Use the secure HTTPS embedly endpoint.'
, ''
, 'Common Options:'
- , ' -v, --verbose Run verbosely.'
, ' -h, --help Display this message.'
, ''
, 'Bob Corsaro <bob@embed.ly>'
@@ -41,14 +43,18 @@ while (args.length) {
case '-o':
case '--option':
kv = args.shift().split('=')
- opts.options[kv[0]] = kv[1]
+ opts.params[kv[0]] = kv[1]
+ break
+ case '-s':
+ case '--secure':
+ opts.secure = true;
break
default:
opts.params.urls.push(arg)
}
}
-var api = new embedly.Api({'key': opts.key, 'host': opts.host})
+var api = new embedly.Api(opts)
api[method](opts.params).
on('complete', function(objs) { process.stdout.write(JSON.stringify(objs,null,'\t')+'\n') }).
on('error', function(e) { process.stdout.write(e+'\n') }).
View
42 lib/embedly.js
@@ -17,6 +17,7 @@ function cast_array(a) {
var Hash = require('hashish')
, http = require('http')
+ , https = require('https')
, EventEmitter = require('events').EventEmitter
, util = require('util')
, querystring = require('querystring')
@@ -52,35 +53,37 @@ var parse_host = function(host) {
* }).start()
*/
var simple_request = function(host, method, path, headers, timeout) {
- var client = http.createClient(host.port, host.hostname, host.protocol == 'https:')
- , request = client.request(method, path, headers)
+ host.method = method
+ host.path = path
+ host.headers = headers
+ var type = host.protocol == 'https:' ? https : http
+ , request = type.request(host)
, emitter = new EventEmitter()
, timeout = timeout || 200000
- if (timeout < 100000) {
- throw new Error('wtf');
- }
log.debug("Calling "+host.protocol+"//"+host.hostname+":"+host.port+path)
headers['X-Embedly-Client'] = 'embedly-node '+VERSION
emitter.start = function() {
request.end()
+ /*
var timer = setTimeout(function req_timeout(){
emitter.emit('timeout')
request.abort()
}, timeout)
- request.on('response', function simple_request_response(response) {
+ */
+ request.on('response', function (response) {
request.connection.setTimeout(1)
var data=[]
- response.on('data', function simple_request_data(chunk) {
+ response.on('data', function (chunk) {
data.push(chunk)
})
- response.on('end', function simple_request_end() {
- clearTimeout(timer)
+ response.on('end', function () {
+ //clearTimeout(timer)
emitter.emit('complete', response, data.join(''))
})
- response.on('error', function simple_request_error(e) {
+ response.on('error', function (e) {
emitter.emit('error', e)
})
})
@@ -111,7 +114,7 @@ try {
if (file) {
var fs = require('fs')
- , log = new Log(level, fs.createWriteStream(file, {flags: 'a'}));
+ , log = new Log(level, fs.createWriteStream(file, {flags: 'a'}))
} else {
var log = new Log(level)
}
@@ -142,7 +145,7 @@ try {
}
}
} catch(e) {
- console.log('WARN: `npm install log for logging.');
+ console.log('WARN: `npm install log` for logging.')
var log = new function() {
this.log = function() {}
this.info = function() {}
@@ -167,7 +170,7 @@ function Api(opts) {
opts = {}
}
if (!opts['user_agent']) {
- log.warn("You didn't pass a user_agent option to embedly.api's constructor, please pass one in the future");
+ log.warn("You didn't pass a user_agent option to embedly.api's constructor, please pass one in the future")
}
this.key = opts['key'] || process.env['EMBEDLY_KEY']
this.host = opts['host'] || 'api.embed.ly'
@@ -178,6 +181,7 @@ function Api(opts) {
, 'preview': 1
}
this.timeout = opts['timeout'] || 200000
+ this.secure = opts['secure'] || false
var self = this
@@ -278,7 +282,7 @@ Api.prototype = new(function() {
if (params['urls'].length > 0) {
var path = "/"+opts['version']+"/"+opts['action']
- , host = parse_host(self.host)
+ , host = parse_host(self.host, self.secure)
simple_request(host, 'GET', path+"?"+querystring.stringify(params)
, {'User-Agent': self.user_agent, 'Host': host.hostname},
@@ -337,7 +341,7 @@ Api.prototype = new(function() {
log.warn('services isn\'t available using the pro endpoints')
}
if (typeof(this._services) == 'undefined') {
- var host = parse_host(self.host)
+ var host = parse_host(self.host, self.secure)
simple_request(host, 'GET'
, '/1/services/javascript'
@@ -351,8 +355,8 @@ Api.prototype = new(function() {
services.emit('error', e)
}
})
- .on('error', function services_error(e) {services.emit('error', e)})
- .on('timeout', function services_timeout() {services.emit('timeout')})
+ .on('error', function (e) {services.emit('error', e)})
+ .on('timeout', function () {services.emit('timeout')})
.start()
} else {
services.emit('complete', this._services)
@@ -392,9 +396,9 @@ Api.prototype = new(function() {
var r = new RegExp(repl.map(function(p){
return p.regex.join("|")
}).join("|"))
- regex.emit('complete', r);
+ regex.emit('complete', r)
} catch(e) {
- regex.emit('error', e);
+ regex.emit('error', e)
}
})
.on('error', function services_regex_error(e) {
View
5 package.json
@@ -31,5 +31,10 @@
"optionalDependencies": {},
"engines": {
"node": "*"
+ },
+ "bin": {
+ "embedly_oembed": "bin/embedly_oembed.js",
+ "embedly_objectify": "bin/embedly_objectify.js",
+ "embedly_preview": "bin/embedly_preview.js"
}
}
View
4 test/objectify-test.js
@@ -7,7 +7,7 @@
*/
var vows = require('vows')
- , embedly = require('../lib/embedly')
+ , embedly = require('../')
, assert = require('assert')
, Hash = require('hashish')
@@ -16,7 +16,7 @@ var vows = require('vows')
*/
function canonize_value(val) {
return (typeof(val) == 'undefined' || val == null) ?
- ''
+ ''
: val
}
View
2  test/oembed-test.js
@@ -6,7 +6,7 @@
* Because I want and oembed for a specific url
*/
var vows = require('vows')
- , embedly = require('../lib/embedly')
+ , embedly = require('../')
, assert = require('assert')
, Hash = require('hashish')
View
4 test/services-test.js
@@ -6,7 +6,7 @@
* Because I want to objectify a url
*/
-var embedly = require('../lib/embedly')
+var embedly = require('../')
, vows = require('vows')
, assert = require('assert')
, Hash = require('hashish')
@@ -17,7 +17,7 @@ var embedly = require('../lib/embedly')
*/
function canonize_value(val) {
return (typeof(val) == 'undefined' || val == null) ?
- ''
+ ''
: val
}
Please sign in to comment.
Something went wrong with that request. Please try again.