Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use library without provided API #186

Closed
LennartKloppenburg opened this issue Nov 15, 2017 · 6 comments
Closed

How to use library without provided API #186

LennartKloppenburg opened this issue Nov 15, 2017 · 6 comments

Comments

@LennartKloppenburg
Copy link

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
Copy link
Author

Found it by inspecting the tests. Thanks anyway!

@sknebel
Copy link

sknebel commented Dec 11, 2017

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

@LennartKloppenburg
Copy link
Author

LennartKloppenburg commented Dec 12, 2017

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.

@dhamaniasad
Copy link

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

@N8Brooks
Copy link

N8Brooks commented Jul 5, 2022

@dhamaniasad your blog is dead?

@dhamaniasad
Copy link

dhamaniasad commented Jul 6, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants