Handling better response from Aws::Route53::Client.new - ruby-on-rails

First time trying to use the Ruby AWS ADK V2 and I am trying to format the data i am getting back and it seems quiet hard getting it into useable format.
All I want to do is get a list of Hosted Zones and display in a table.
I have a helper that has:
def hosted_zones
r53 = Aws::Route53::Client.new
#convert to hash first so we can parse and covert to json
h = (r53.list_hosted_zones).to_hash
j = JSON.parse((h.to_json))
end
which then returns me the following JSON:
{
"hosted_zones": [{
"id": "/hostedzone/Z1HSDGASSSME",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:45.277646707+01:00",
"config": {
"comment": "Private DNS zone for stage",
"private_zone": true
},
"resource_record_set_count": 10
}, {
"id": "/hostedzone/ZJDGASSS0ZN3",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:41.290143511+01:00",
"config": {
"comment": "Public DNS zone for stage",
"private_zone": false
},
"resource_record_set_count": 7
}],
"is_truncated": false,
"max_items": 100
}
To which I am running a really but while statement to interact through all the hosted_zone entries into a table.
Is this the best way to get the response or can you request the response to be json already?

Why are you converting a hash to JSON, only to convert it to a hash again? JSON.parse(some_hash.to_json) will just give you some_hash.
That being said, I don't think it is possible to get JSON directly from AWS, mainly due to the fact that their API responds with XML. I think that your solution is ideal if that's all you're doing, but if you want, you can make a request with an HTTP client and then take the XML that you receive and use something like ActiveSupport's Hash.from_xml to create a hash that you can then convert to JSON.

Related

Response Templating for a Static Json File

I am using WireMock in java to stub for a POST request. The request returns a json body file that is stored in my local. The stub looks like this:
wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile("testgetupgradeprorationamount/stubThree")));
Part of the response body file, "stubThree" looks like this:
"id": "ii_1EmM93Htp4Kkdrs8",
"object": "line_item",
"amount": 9600,
"currency": "usd",
"description": "Remaining time on 3 × Business after 17 Jun 2019",
"discountable": false,
"invoice_item": "ii_1EmM93HtpLyYzpmOC4Kkdrs8",
"livemode": false,
"metadata": {
},
"period": {
"end": 1563374954,
"start": 1560782957
}
The request url has a number of paramters and looks like this:
/v1/invoices/?subscription_items[0][quantity]=3&subscription_proration_date=1560892137&customer=cus_FHNIIE4b8LH7qL"
The stubbing works fine, but my goal is to give a dynamic response using response templating. I want to update the "start" field of the json file only, using the "subscription_proration_date" value from the request url.
I changed the start field and the stub like this:
"period": {
"end": 1566053354,
"start": "{{request.query.subscription_proration_date}}"
},
wireMockServer.stubFor(get(urlMatching("/v1/invoices/.*"))
.willReturn(aResponse()
.withStatus(200)
.withBodyFile("testgetupgradeprorationamount/stubThree")
.withTransformers("response-template")));
This is not working for me, so any directions would be really helpful. I also tried removing the quotations around the start field handlebar in the file, and that did not work either.
Thank you in advance!
so I was able to resolve my issue. The problem was that I did not add the proper extension to my WireMockServer instance:
.extensions(new ResponseTemplateTransformer(false));
If the boolean value is false, you additionally need to specify the transformer on a per stub basis like I did above. Otherwise, the extension is applied to all stubs.

Album mbId in track's metadata

I am using MusicBrainz to get a track's meta data. I want to get the track's album's mbid. I am doing the following lookup using ISRC code.
https://musicbrainz.org/ws/2/isrc/USRC11600201?fmt=json
But in response I don't get any metadata related to the album of the track. I get the following response:
{
"isrc": "USRC11600201",
"recordings": [
{
"disambiguation": "single remix",
"title": "Cheap Thrills",
"id": "92e27a47-3546-4bc2-a9f7-b19e43d7a531",
"length": 223000,
"video": false
},
{
"length": 218540,
"video": false,
"title": "Cheap Thrills",
"disambiguation": "",
"id": "5845e975-33b4-4b0d-8e74-8f57d128b3d1"
}
]
}
I have tried various combinations of the "inc" sub query parameter as well but nothing works. Please help me out. I am really stuck at this.
Using inc=releases in the URL parameters should be enough to get you the information that you want. However, it seems like there's a bug with MusicBrainz's JSON web service (which is still officially in beta), as you can see in the difference between the XML and JSON end points' output:
https://musicbrainz.org/ws/2/isrc/USRC11600201?inc=releases&fmt=json (JSON) vs. https://musicbrainz.org/ws/2/isrc/USRC11600201?inc=releases (XML).
One obvious solution/work-around here would be to switch to using the more mature XML endpoint. If that is not an option, you can use the Recording MBIDs given in the JSON output to look up releases associated with those Recordings, e.g., https://musicbrainz.org/ws/2/recording/5845e975-33b4-4b0d-8e74-8f57d128b3d1?inc=releases&fmt=json (note that inc=releases is also needed here to get the information about the releases, and it actually works when looking up recordings).
So to get the details of the album of a track when I have the ISRC of the track, we need to do the following get request:
https://musicbrainz.org/ws/2/isrc/GBUM71604605?inc=releases
It will give a response in xml. The xml api is more stable click for more details on this
As I need the response in json, we can use a library like xml to json and vice-versa library
As much as I have seen the xml response from the MusicBrainz api is more accurate and gives a lot of information.

Customize request format in Ember

I am building a Rails 5 backend API which will receive requests from my Ember app. However I'm having some trouble getting Ember to format the request in a way my Rails server understands.
By default, Rails creates controllers to expect parameters in this format, assuming the model is a, say, Car:
"car": {
"id": "1",
"name": "Foo",
"bar": "Bar",
...
}
However it looks like Ember is sending requests in this format:
"data": [
{
id: "1",
type: "cars",
attributes: {
"name: "Foo",
"bar": "Bar",
...
}
]
What can I do to make Ember send request payloads in a way my Rails server will understand? Thank you.
Your Rails is accepting REST adapter format, for that to work properly, your adapter should extend DS.RESTAdapter and serializer should extend DS.RESTSerializer. By default it will comes with JSONAPIAdapter and JSONAPISerializer.
If you are having control over the back end code, then consider writing json-api format response for that ember will work out of the box.
Reference:
https://emberjs.com/api/ember-data/2.14/classes/DS.RESTAdapter
https://emberjs.com/api/ember-data/2.14/classes/DS.RESTSerializer
https://emberjs.com/api/ember-data/2.14/classes/DS.JSONAPIAdapter
https://emberjs.com/api/ember-data/2.14.9/classes/DS.JSONAPISerializer

Rails 4 API, how to create a URL from a JSON response?

I'm working on an API where I have a few routes setup, ie
http://localhost:3000/phone_number_lookup/1234567890
which can return a JSON response like so:
{
"AccountCode": "1234",
"AccountID": 13579,
"BalanceCurrent": "5000",
"Phone": "1234567890",
"Id": 123123,
"SerialNumber": "Y2K2000XY2016",
"MACADDRESS": "y2k2000xy2016",
"EQUIPMENTTYPE_Name": "Motorola DCX100 HD DVR",
"ADDRESS_Zip": "90210",
"ItemID": 12345,
"iVideoSystemID": 1000001
"id": null
}
The next 'step' of the API consumption would be, 'given the initially returned response, use 4 of those parameters and pass them into a remote URL that will then do something.'
Like so:
http://myremoteURL.com/Service/?Param1=sSerialNumber&Param2=iVideoSystemID&Param3=sMAC&Param4=ItemID
It would be one thing to just set up a route that takes 4 parameters, but the route needs to be contingent on what the initial JSON response was.
What is the proper way to do this?
First of all, you'll have to convert your JSON to hash. Something like this will do:
[7] pry(main)> hash=JSON.parse(json)
=> {"AccountCode"=>"1234",
"AccountID"=>13579,
"BalanceCurrent"=>"5000",
"Phone"=>"1234567890",
"Id"=>123123,
"SerialNumber"=>"Y2K2000XY2016",
"MACADDRESS"=>"y2k2000xy2016",
"EQUIPMENTTYPE_Name"=>"Motorola DCX100 HD DVR",
"ADDRESS_Zip"=>"90210",
"ItemID"=>12345,
"iVideoSystemID"=>1000001,
"id"=>nil}
Then you'll have to choose 4 parameters to send. I just took last 4 parameters
[14] pry(main)> chosen_params = hash.slice("ItemID", "id", "iVideoSystemID", "ADDRESS_Zip")
=> {"ItemID"=>12345, "id"=>nil, "iVideoSystemID"=>1000001, "ADDRESS_Zip"=>"90210"}
Then you'll have to pass them to your remote url. This can be done using a helper described here. Then you'll have to just do something like generate_url("YOUR-URL-ADDR-HERE", chosen_params).
At this point you might want to change the generate_url helper in a way you need it to be to generate the url you need. Maybe it should take third parameter called action which will then generate url like http://www.google.com/action?{chosen_params}
The result will be:
[23] pry(main)> generate_url("http://www.google.com", chosen_params)
=> "http://www.google.com?ADDRESS_Zip=90210&ItemID=12345&iVideoSystemID=1000001&id="
Hope it helps. Let me know about any questions.
Could you modify the JSON response?
{
"AccountCode": "1234",
"AccountID": 13579,
...
"id": null
"follow_up_url": "http://myremoteURL.com/Service/?Param1=sSerialNumber&Param2=iVideoSystemID&Param3=sMAC&Param4=ItemID"
}
This allows your JSON to tell the requester "where to go next".

Obj. C - how to deal with wrong written JSON

I obtained some badly written JSON file that's totally not readable through any AFNetworking and/or any other JSON serializing library. I must underline that I am not able to force to change it on server-side so I have to parse it as is.
Unfortunately it has some minor errors (I will paste small part of it):
locations = [{
"city": "Tokio",
(...)
"link": "http://somethig.com",
"text": "Mon-Fr.",
}, {
(... same repeated mistakes, but they're not regular)
}]
Aaand to parse it correctly in XCode i need to change it to correct format e.g.:
{
"locations": [{
"city": "Tokio",
(...)
"link": "http://somethig.com",
"text": "Mon-Fr."
}, {
(...)
}]
}
Do you have any idea how to deal with that?
If I will have to write my own parser - please advice me how to. Any help will be appreciated. I download this JSON from http link.
It looks like the API is sending you a string of JavaScript code. In JavaScript, you would use JSON.stringify() to convert the object to valid JSON. Here's an example from an interactive node.js shell:
> locations = [{
... "city": "Tokio",
... "link": "http://somethig.com",
... "text": "Mon-Fr.",
... }]
[ { city: 'Tokio', link: 'http://somethig.com', text: 'Mon-Fr.' } ]
> JSON.stringify(locations)
'[{"city":"Tokio","link":"http://somethig.com","text":"Mon-Fr."}]'
If you really can't change this on the server side, you might try creating a hidden UIWebView, adding the JavaScript, calling JSON.stringify(), and extracting the result. However, this will use many more resources than are needed. It would be much better (in terms of computing power, memory, and your time) to have the API call JSON.stringify() and send you a valid JSON string.
You could also try porting some of json3.js to Objective-C or Swift. I would start with the section beginning // Public: `JSON.stringify`. if you attempt this.

Resources