I wrote a simple coffee script using Jisho API but it didn't work. Why is that?
See original GitHub issueHere’s the code of dict.coffee. When i run it by command line and type this on Slack: @name_of_hubot dict pro. It didn’t reply anything. Could some one tell me how to fix it please?
apiUrl = “http://jisho.org/api/v1/search/words?keyword=” module.exports = (robot) -> robot.respond(/dict (.*)/i, (msg) -> request = robot.http(apiUrl) .query(titles: msg.match[1]) .get()
request((err, res, body) ->
if err
msg.send("There was some error")
return
data = JSON.parse(body)
for id, value of data.query.pages
if value.extract?
respond = "Here's the result from Jisho\n"
respond += value.extract
msg.send(respond)
else
msg.send("Sorry we didn't find anything")
)
)
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
CoffeeScript
CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule of CoffeeScript is: “It's just...
Read more >Why doesn't this coffee-script print out? - Stack Overflow
i returns the index as a string, if you parse them as an integer, it would work a = [ 1, 2, 3...
Read more >Why CoffeeScript Isn't the Answer - Walker Code Ranger
In CoffeeScript there is no variable declaration syntax equivalent to var in JavaScript. Instead, all variables are declared in the scope they ...
Read more >Asynchronous coffeescript made easy, part II #287 - GitHub
I have been working on the Mozilla platform for a while and I have ... That argument does not currently hold in coffeescript...
Read more >Untitled
One day jobs in chennai, River campeon apertura 1992, 1 metric ton is how many kg, ... Canadian cycling magazine circulation, Sql script...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@thinhduckhoi: There are a couple things you’ll need to change with your script before you can start working with the responses from Jisho.
You need to adjust your GET request to pass parameters that the API is expecting, so in your case you’ll want to do something like this:
After that, you’ll be able to get any sort of response, but the next problem lies within your for loop trying to process the response. It’s still referencing the expected results from Wikipedia and you’ll need to adjust this for Jisho’s response. Instead of parsing values from a
query.pages
object, you’ll be wanting to get values off of thedata
object. From there it appears to be an awful lot of data, so it’s up to you to decide what to do with it.Hope this helps!
@thinhduckhoi Alright, so let’s back up here a second and we’re going to go back to the script we started out with as our base.
One thing we need to cover briefly is how APIs work. Swapping out the URL in a script for another and just expecting it to work won’t get you anywhere, every API responds differently so you need to adjust the values you’re expecting to parse. In the case of the Urban Dictionary script, it’s actually parsing the DOM to find values, and that is completely different from what you want to do since you have an actual API returning content to parse. So to answer your one question about what “extract” is in the Wikipedia script, thats the value that specific API is returning.
In your original script, you were trying to pass the parameter
titles
, but that isn’t what your desired API is expecting, so you needed to update that tokeyword
.The results you’ll have to play with coming back from Jisho are the following (I used the word “house” for this example):
You had said you wanted to get the first definition back, that is pretty simple and won’t require any sort of loop. In the case of this API, we will have an array called
data
coming back with results if any, so we’ll want to make sure it does before assuming we can parsedata
. After that you can simply request the first array element from the result set and go through it’s object from there.Given you want the first definition, we will have to grab the first array element off that
senses
array and get our information off of that.Putting it all together:
Here we are getting just the first english definition back, but if you wanted to expand upon it to include the parts of speech, you would just need to add another variable accessing that from the results.
If you wished to include information from the rest of the results, you would then have to loop through the
data
array, and eachsenses
array inside of there to get more results programatically.I think this should be more than enough to get you started toward your desired script and let you expand upon it as needed.