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

Add modules.getMethods() #456

Merged
merged 7 commits into from
Nov 26, 2018

Conversation

piznel
Copy link
Contributor

@piznel piznel commented Nov 21, 2018

Gladys Pull Request check-list

To ensure your Pull Request can be accepted as fast as possible, make sure to review and check all of these items:

  • If your changes affects code, did your write the tests?
  • Are tests passing?
  • Is the linter (eslint) passing?
  • If your changes modify the API (REST or Node.js), did you modify the documentation?

NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.

Description of change

this function returns a list of methods passed in parameters with a true or false value if they are exposed by a module.
Example : Testing if a module expose API Television's method

 params = {
            'module' :"livebox",
            'service':"television",
            'methods' :["getState", "getChannel", "getMuted"]
        };

=> { getState: true, getChannel: false, getMuted: false }
The param.service is optionnel.

@piznel piznel mentioned this pull request Nov 21, 2018
4 tasks
@piznel
Copy link
Contributor Author

piznel commented Nov 21, 2018

First documentation, first test... Not so simple as it seems to be writing! Sorry for the last commits....
The strange thing is that in my case, the tests had passed, as well as the generation of the documentation.

@Boimb
Copy link
Contributor

Boimb commented Nov 23, 2018

Hey @piznel ,
Really nice feature.
I was self-questionning :
Why does this return an array of objects with boolean values ? Wouldn't it be more easy and relevant to get back just an array of "the trully" values in the getMethods ?
From your example it could be :

['getState']

As you put an array of strings in parameters, my point is that it would make more sense.
The result would be more pratictal to use as you just have to check if the method name is in the results.
The code could be like :

module.exports = function getMethods(params) {
  return Promise.resolve(params.methods.filter(method => params.hasOwnProperty('service')
  // If 'service' check if service && method exists
  ? gladys.modules[params.module] && 
    gladys.modules[params.module[method]] &&
    typeof gladys.modules[params.module][params.service][method] === 'function'
  // Else check only if method exists
  : gladys.modules[params.module] && 
    gladys.modules[params.module[method]]));
};

And then, you could use it like :

const params = {
  'module' :"livebox",
  'service':"television",
  'methods' :["getState", "getChannel", "getMuted"]
};

const methods = gladys.module.getMethods(params);
// Checks
const availables = methods
  .filter(methodName => params.methods.includes(methodName))
const missing = methods
  .filter(methodName => !params.methods.includes(methodName))
const allIn = missing.length === 0;

@piznel
Copy link
Contributor Author

piznel commented Nov 23, 2018

Hey @Boimb,
By sending it back like that, you just have to do a value test, without filter :
if(methods.getState) {...

@Boimb
Copy link
Contributor

Boimb commented Nov 23, 2018

Yep. Ofc. For a single test, knowing the name of the function you're testing, it's easier the "object way".
But if you don't know the name of the function, or you want to "abstract" it like in an array, it would become less readable:

const availables = params.methods
  .filter(method => gladys.module.getMethods(params)[method])

But, hey... That was just a simple point of view (mine). If you're OK and confortable with it, stick to that ;)
Great work btw.
And with tests! 👍
"Chapeau bas" ;)

@Pierre-Gilles Pierre-Gilles merged commit b39cb74 into GladysAssistant:master Nov 26, 2018
Pierre-Gilles added a commit that referenced this pull request Nov 26, 2018
@piznel piznel deleted the Add_modules.getMethods() branch November 26, 2018 07:05
R6n0 pushed a commit to R6n0/Gladys that referenced this pull request Dec 2, 2020
…oute to get available function from module
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

Successfully merging this pull request may close these issues.

3 participants