itteco / iframely

oEmbed proxy. Supports over 1800 domains via custom parsers, oEmbed, Twitter Cards and Open Graph

Home Page:https://iframely.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use library without provided API

LennartKloppenburg opened this issue · comments

The docs specify that you can also use Iframely as a NodeJS Library. How should I go about this? I installed the package into my own API but I don't know how to consume it as a library without the API that is provided. I'd ideally just like to include it by means of a const iframely = require('iframely'); or something similar. Can't seem to find a clear example.

Thanks in advance.

Found it by inspecting the tests. Thanks anyway!

commented

Since you figured it out, could you share and/or add some info to the docs? Thx!

Of course.

I have no idea if this is 'the right way' to do it, but I can expose the library like this:

//library.js
/* The key is to import these files into the file you wish to export as a module. */
global.CONFIG = require('./config');

var iframely = require('./lib/core').getPluginData;
var findWhitelistRecordFor = require('./lib/whitelist').findWhitelistRecordFor;
var utils = require('./lib/utils');

/* Secondly, the getPluginData method we rename as 'iframely' accepts different embedding methods which you can examine in their '/test/main.js' */
const library = {
    oembed: function(url, callback){
        iframely(url, 'oembed', findWhitelistRecordFor, function(err, data){
            if (err) return callback(err);
            callback(null, data);
        });
    },
    meta: function(url, callback){
        iframely(url, 'meta', findWhitelistRecordFor, function(err, data){
            if (err) return callback(err);
            callback(null, data);
        });
    }
};

module.exports = library;

Then you could consume the library like so:

//some_app.js

const iframely = require('./library');
const oembedUrl = 'https://twitter.com/Cool_Running/status/940436809921724417';
lib.oembed(oembedUrl, function(err, data){
    if(err) console.log(err);
    console.log(data);
});

const nonOembedUrl = 'https://blog.fabric8.io/openshift-io-fully-integrated-ci-cd-in-a-single-of-glass-e1b18120d764';
lib.meta(nonOembedUrl, function(err, data){
    if(err) console.log(err);
    console.log(data);
});

Again, I have no idea if they recommend doing it like this, but this could get you started. How you want to expose the library is up to you.

I'm getting some warnings, but that doesn't seem to matter.

I recently needed to use iFramely for a project and couldn't make use of the HTTP API and needed to use the library directly. It took a while and some digging to figure it out, so I've documented it on my blog which I'll link below, maybe someone else might find it useful.

How to use iFramely as a Node library

@dhamaniasad your blog is dead?