where do I put an API curl request in a rails app - ruby-on-rails

I'm using the G Adventure API to populate a database in my super-basic rails app.
The docs https://developers.gadventures.com/docs/index.html#api-endpoint
say to use a CURL request to get back JSON data, which obviously works fine, but what I'm wondering is where I should then store this request in the app itself...
Hunch is that it should be linked to a method inside the controller?
Thanks.

You don't really want to use curl in the application. Instead it would be better to use Ruby's Net::HTTP to make the request. Also, if relates to the logic of your app, and if you are saving the data in a database, it sounds like it does, this sort of thing belongs on the model, not the controller.
So in your model try
Net::HTTP::Get.new(API END POINT URL)

Related

Rails providing search through API with themoviedb-api gem

I am creating movie/series focused web application. In this application users would be able to search through movies/series and subscribe them. I want to provide users with the movie/series search engine through API. In my app's database I have no information about movies/series, so when the users is typing title in the search field I need to send a search request to API to fetch movies/series that match the given pattern. The problem is that at this point I don't see any way of implementing this functionality. I thought about another controller e.i. Searcher. That's fine, but I would like to put this search field in my app's layout so it would be available everywhere not only for users that are on particular URL.
First have you an API key from IMDB API ? (https://github.com/18Months/themoviedb-api)
Make it a remote form and catch results through Ajax callback ajax:complete.
When a request is made, show a loader on the view to wait the time you call the api.
Call Your SearchController throught its route.
In your controller Search in you DB, if you do not find it, make an API call, save in DB and serve the result.
(Would be better to call the API in background, and serve results through Websockets but would be over-engineering right now.)

Binding a irregular URL encoded POST request with Nancy

I'm supposed to make an old sqlite database editable trough a Sketchup Plugin in its WebDialog. Sketchups Ruby is not able to install the sqlite3 gem and since I already need to display the table in a WebDialog, I opted for a micro service with the help of Nancy. The existing Plugin already uses the dhtmlxSuite and its Grid Component is exactly what I need. They even offer a Connector that can send requests based on the actions of the user in the grid.
They also offer a connector for the server side, but that too does not work with sqlite. I already managed to connect to my database, used Dapper to bin the result of an SQL query to an object and return that data with a manually crafted JSON object. Very hacky, but it populates the data successful.
Now the client-Connector sends data manipulated by the user (delete and insert is forbidden) back with an url encoded POST request that looks quite weird:
4_gr_id=4&4_c0=0701.041&4_c1=Diagonale%20f%3Fr%202.07%20X%201.50%20m&4_c2=AR&4_c3=8.3&4_c4=380&4_c5=38.53&4_c6=0&4_c7=0&4_!nativeeditor_status=updated&ids=4
I'm not sure exactly why the hell they would use the row index inside the key for the key-value pairs, but this just makes my life that much harder. The only thing that comes to my mind is extracting the data I'm interested in with a regex, but that sounds even worse than what I did before.
Any suggestions for me how I could map that POST request to something usable?

Object expected Hash found, Ruby on Rails, Rest

I have written up a Rest api for my website. Currently I have it so that a user has many computers that they can own. I am trying to send a json payload that updates the user account with a computer. For testing this is what I send:
{"auth_token"=>"AiprpscAqN6hnvNDHSwh",
"user"=>"{"id":1,
"username":"Rizowski",
"email":"test#gmail.com",
"computer":[{"id":0,
"user_id":1,
"name":"Desktop",
"enviroment":"Windows 8",
"ip_address":"192.168.1.10",
"account":[]}]}",
"format"=>"json",
"id"=>"1"}
Once I send it, the Rails controller receives it and parses the request into a hash using the JSON.parse method. Once I try to save the user object it says this:
ActiveRecord::AssociationTypeMismatch in Api::V1::UsersController#update
Computer(#51701664) expected, got Hash(#17322756)
Side question: I am still trying to completely understand rails and rest, but is it a good practice to send a computer object as I am? OR should I be sending the computer data through my computer api controller?
The solution to your error is to use the key computer_attributes instead of computer.
This is a normal practice for strongly associated and hierarchical data. If computers are going to be updated independently of user, you should certainly use its own controller.

rails RESTful updates

In Rails, should you be able to update a field using a URL.
Is that called RESTful?
For example, should something similar to this work to update workorder.wostatus_id for workorder with id=2?
http://localhost:5000/workorders/2?wostatus_id=4
Thanks!
In the case you've provided that shouldn't work as updates should be performed via a PUT request although if the URL was requested via PUT then it should work.
Remember the idea is that:
GET to access and retrieve data
PUT to update data
POST to create data
DELETE to remove data
EDIT: Often the actual parameter names can vary depending on the controller implementation so in rails you often find ?workorder[wostatus_id]=4, where it will reference the model name.
You will have to call a URL to perform any updates, but you'll need to use a POST or PUT request. POST to create, PUT to update, but I believe Rails uses "data-action" attribute for PUTs, then actually calls POST behind the scenes.
Just putting the URL in a browser, like the one you gave, will by default perform a GET request, which should never be used to change data, only to retrieve it.
Here's a good tutorial that explains the basics of REST: REST API tutorial
The Rest for Rails screencast is pretty helpful, as well.

Play Framework - GET vs POST

New to web development, my understanding is that GET is used to get user input and POST to give them output. If I have a hybrid page, eg. on StackOverflow, if I write a question, it POSTs a page with my question, but also has a text box to GET my answer. In my routes file, what method would the URL associated with my postQgetA() method specify - GET or POST?
From technical point of view you can use only GET to perform almost every operation, but...
GET is most common method and it's used when you ie. click on the link, to get data (and do not modify it on server), optionally you send id of the resource to get (if you need to get data of single user).
POST is most often used to sending new data to the server ie. from form - to store them in your database (or proccess in any other way)
There are also other request methods (ie. DELETE, PUT) you can use with Play, however some of them need to be 'emulated' via ie. ajax, as there is not possible to set method of the common link ie. to DELETE. It's described how to use non-GET/POST methods in Play! (Note, that Julien suggests there, using GET for delete action although is possible it's a broken semantics.)
There are also other discussions on StackOverflow where you can find examples and suggestions for choosing correct method for your routes.
BTW, if you sending some request, let's say it's POST you don't need to perform separate GET as sending a request generates a response in other words, after sending new question with POST first you're trying to save it to DB, if no errors render the page and send it back in response.

Resources