iframely icon indicating copy to clipboard operation
iframely copied to clipboard

How to use library without provided API

Open LennartKloppenburg opened this issue 7 years ago • 6 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.

LennartKloppenburg avatar Nov 15 '17 09:11 LennartKloppenburg

Found it by inspecting the tests. Thanks anyway!

LennartKloppenburg avatar Nov 15 '17 16:11 LennartKloppenburg

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

sknebel avatar Dec 11 '17 14:12 sknebel

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.

LennartKloppenburg avatar Dec 12 '17 09:12 LennartKloppenburg

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 avatar Jan 30 '19 11:01 dhamaniasad

@dhamaniasad your blog is dead?

N8Brooks avatar Jul 05 '22 23:07 N8Brooks

It’s fixed now, thanks.

On Wed, 6 Jul 2022 at 5:26 AM, skookumchoocher @.***> wrote:

@dhamaniasad https://github.com/dhamaniasad your blog is dead?

— Reply to this email directly, view it on GitHub https://github.com/itteco/iframely/issues/186#issuecomment-1175612134, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCZMYWNPZ5JQIRSTJSOZ23VSTDR5ANCNFSM4ED2PGXA . You are receiving this because you were mentioned.Message ID: @.***>

-- With Regards, Asad Dhamani

dhamaniasad avatar Jul 06 '22 04:07 dhamaniasad