Skip to content

Commit

Permalink
add mime in meta info
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfmiller committed Apr 11, 2017
1 parent a6a1765 commit 155f1b6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ A well-tested library of code for working with internet metadata:
RMR = require('rmr-lib'),
mime = RMR.mime.fromPath('script.js')), // 'text/javascript'
ext = RMR.mime.extensionFor('application/json'), // 'json'
isURL = RMR.url.isA('https://google.com'); // true
isURL = RMR.url.isA('https://google.com'), // true
RMR.meta.retrieve('http://readmeansrun.com').then(function(data) {
/*{
"title": "READMEANSRUN",
Expand Down
28 changes: 13 additions & 15 deletions index.js
Expand Up @@ -23,7 +23,11 @@ const parseMetadata = function(markup, options) {
return new Promise(function(resolve, reject) {

var
OBJ = {};
OBJ = options && options.mime ? { mime : options.mime.split(';')[0] } : {},
i = 0,
node = null,
keyAttribute = null,
valueAttribute = null;

if (markup) {

Expand All @@ -40,13 +44,6 @@ const parseMetadata = function(markup, options) {
options.baseURL = '/';
}

var
OBJ = {},
i = 0,
node = null,
keyAttribute = null,
valueAttribute = null;

if (title && title.length) {
OBJ.title = title[0].firstChild.nodeValue;
}
Expand Down Expand Up @@ -190,6 +187,7 @@ const parseMetadata = function(markup, options) {
const retrieveMetadata = function(address, options) {

var ARGS = arguments;
// if (arguments.length == 2) { options = {}; }

return new Promise(function(resolve, reject) {

Expand All @@ -200,10 +198,10 @@ const retrieveMetadata = function(address, options) {
},
resolveWithFullResponse: true
}).then(function(response) {

var type = response.headers['content-type'];

if (type.substring(0,9) == 'text/html') {
var mime = response.headers['content-type'];

if (mime.substring(0,9) == 'text/html') {

request.get({
url : address,
Expand All @@ -221,11 +219,11 @@ const retrieveMetadata = function(address, options) {

options = ARGS.length == 2 ? options : {
baseURL : response.request.uri.href,
icons : true
icons : true,
mime : mime
};


parseMetadata(body, options ? options : { baseURL : response.request.uri.href, icons : true }).then(function(obj) {
parseMetadata(body, options).then(function(obj) {
resolve(obj);
}).catch(function(err) {
reject(err);
Expand All @@ -234,7 +232,7 @@ const retrieveMetadata = function(address, options) {

} else {

parseMetadata(null, options ? options : { baseURL : response.request.uri.href, icons : true }).then(function(obj) {
parseMetadata(null, options ? options : { mime : mime, baseURL : response.request.uri.href, icons : true }).then(function(obj) {
resolve(obj);
}).catch(function(e) {
reject(e);
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "rmr-lib",
"version": "0.0.11",
"version": "0.0.12",
"repository": "git@github.com:davidfmiller/rmr-lib.git",
"description": "🌏",
"license": "MIT",
Expand Down
8 changes: 6 additions & 2 deletions test.js
Expand Up @@ -84,27 +84,29 @@ describe('RMR.meta', function() {
});


it('RMR', function() {
it('RMR og:image', function() {
return RMR.meta.retrieve('https://readmeansrun.com/assets/img/og-image.png').then(function(data) {

expect(data.title).to.equal(undefined);
expect(data['apple-touch-icon'].url).to.equal('https://readmeansrun.com/apple-touch-icon.png');
expect(data['favicon'].url).to.equal('https://readmeansrun.com/favicon.ico');
expect(data['mime']).to.equal('image/png');

}).catch(function(m) { console.log(m); throw new Error(m.toString()); });
});



it('google.ca', function() {
return RMR.meta.retrieve('https://google.ca').then(function(data) {

expect(data.title).to.equal('Google');
expect(data.favicon.url).to.equal('https://www.google.ca/images/branding/product/ico/googleg_lodp.ico');
expect(data['mime']).to.equal('text/html');

}).catch(function(m) { console.log(m); throw new Error(m.toString()); });
});


it('wired', function() {
return RMR.meta.retrieve('https://www.wired.com/2017/04/autodesk-project-discover/').then(function(data) {

Expand All @@ -123,6 +125,7 @@ describe('RMR.meta', function() {
expect(data.title).to.equal('iPad Pro - Apple');
expect(data.favicon.url).to.equal('http://www.apple.com/favicon.ico');
expect(data['og:description']).to.equal('iPad Pro delivers epic power, in 12.9-inch and 9.7-inch sizes. Discover the A9X Chip, Advanced Retina display, 12MP iSight camera, and more.');
expect(data['mime']).to.equal('text/html');

}).catch(function(m) { console.log(m); throw new Error(m.toString()); });
});
Expand All @@ -135,6 +138,7 @@ describe('RMR.meta', function() {
expect(data['og:description']).to.equal('READMEANSRUN makes websites and takes pictures');
expect(data['description']).to.equal('READMEANSRUN makes websites and takes pictures');
expect(data['keywords']).to.equal('READMEANSRUN');
expect(data['mime']).to.equal('text/html');

}).catch(function(m) { console.log(m); throw new Error(m.toString()); });
});
Expand Down

0 comments on commit 155f1b6

Please sign in to comment.