Save data from api request to db (JSON) - ruby-on-rails

I sending messages from my rail app through third party and track the deliver details and store it in my database table.After sending messages third party post deliver details to my given URL,I got response to my URL like.
http://yourdomain.com/dlr/pushUrl.php?data=%7B%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A%7B%22911234567890%22%3A%7B%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22%7D%7D%7D
Like following format,
data={
"requestId":"546b384ce51f469a2e8b4567",
"numbers":{
"911234567890":{
"date":"2014-11-18 17:45:59",
"status":1,
"desc":"DELIVERED"
}}}
I used following code in my controller to display data.
json = params["data"]["numbers"]
puts json
But it displays NULL. Now I want to save the data into database.Is there any Gem to be used or any other method is good.Am new to ROR.

Your issue is that params["data"] is a string, but you are treating it like a hash.
data = JSON.parse(params["data"])
puts data['numbers']

Related

Rails-API How to test Active Storage

I added Active Storage into my Rails API application and want to test it, but I don't know how to do it. I was trying send file with JSON data in Postman, but JSON data doesn't send correctly or I am doing something wrong. I did it like that:
Image from postman
Is there any option to send request with file and JSON data without creating any view?
As far as I know, an upload can't be done via an API rest way. You need to use enctype: multipart/form-data as regular POST done via form.
In your screenshot you are already sending as form-post, because you chose to send via form-data. This is why your json isn't properly parsed by rails.
If you want to upload an image and post data in the same request you will need to break your json attributes into form data fields like:
data[name]=asaa
data[last_name]=foo
...
Or, you can send a JSON in your data field and do the manual parsing when fetching in controller like:
def upload
file = params[:file]
data = parsed_params_data!(params)
# do your magic
end
def parsed_params_data!(params)
JSON.parse(params[:data])
end

Where does FactoryGirl save db entries in a Rails API app?

I'm fairly new to rails and I'm currently creating my first API only rails app.
What I'm struggling to get my head around is where exactly are entries being stored in the test environment? In a full rails app FactoryGirl would store entries in the _test database and passed to the controller - tested via something like RSpec.
I understand that in a rails API app you're foregoing the use of databases and instead aiming to store data in JSON format on a server - but in the case where I'm writing request specs, where is the information being stored/retrieved from? There is no server (locally or remotely) to hold the data!?
Apologies for my newbie question!
JSON is just the format you use to make the communication from the client and the server, and backward, happen. But data are stored in a database as well. This is a spec I wrote in a rails api project to save a category (it's a request spec).
context 'everything is ok' do
it 'gets a successfull response' do
params = { category: category }.to_json
post "/categories", headers: headers.merge("X-Api-Key" => partner.token), params: params
json = JSON.parse(response.body)
expect(json['success']).to be true
expect(json['category']['name']).to eq 'Programming Languages'
end
end
Just to give you an idea, I'm not pro at testing, I'm learning as well.
and instead aiming to store data in JSON format on a server
Nope. That would be silly. Rails API app is a regular rails app (with a database and whatnot). Only it does waaaay less html view rendering (ERB and such). That's the main difference.

Configure Ember to send JSON to Rails Server

I want to send base64 image string to my server. When I send it as a parameter in my controller like this:
this.get('store').createRecord(Emb.Painting, {name: newName, image:base64string]});
this.get('store').commit();
The upload succeeds but I get RequestURITooLarge error.
How do I get ember to send my data as a JSON package?
If anyone could also explain how to recieve JSON in the rails controller thatd be great too.

Finding form fields in a Rails POST request

I'm using a plain old HTML form to send a post request to a rails app. Inside the app I would like to grab some of the input fields. I'm having trouble finding them inside the request.
Trying this:
logger.debug "Request Headers #{request.headers.inspect}"
Spits out a massive amount of data but I cannot find my form fields in there. Does anyone know where I can find them?
You can grab the request parameters submitted using:
logger.debug params.inspect
That will show all the form data submitted.

Access URL on another website from a model in rails

I want to access a URL of another website from one of my models, parse some information and send it back to my user. Is this possible?
For example, the user sends me an address through a POST, and I want to validate the information through a third party website (USPS or GMaps)
What methods would I use to create the request and parse the response?
This is not a redirect. I want to open a new request that is transparent from the client.
There are a lot of libraries to handle this such as:
HTTParty on http://github.com/jnunemaker/httparty
Curb on http://curb.rubyforge.org/
Patron on http://github.com/toland/patron
Example using Patron:
sess = Patron::Session.new
sess.timeout = 10
sess.base_url = "http://myserver.com:9900"
sess.headers['User-Agent'] = 'myapp/1.0'
resp = sess.get("/foo/bar")
if resp.status < 400
puts resp.body
end
Each solution has its own way of handling requests and parsing them as well as variations in their API. Look for what fits your needs the best.

Resources