Domanda

Here is a sample batch request response.

HTTP/1.1 200 OK
Content-Type: multipart/mixed; boundary=batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0

--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: response-TIMELINE_INSERT_USER_1

HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 304

{
 "kind": "glass#timelineItem",
 "id": "1234567890",
 "selfLink": "https://www.googleapis.com/mirror/v1/timeline/1234567890",
 "created": "2012-09-25T23:28:43.192Z",
 "updated": "2012-09-25T23:28:43.192Z",
 "etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
 "text": "Hello there!"
}
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: response-TIMELINE_INSERT_USER_2

HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 304

{
 "kind": "glass#timelineItem",
 "id": "0987654321",
 "selfLink": "https://www.googleapis.com/mirror/v1/timeline/0987654321",
 "created": "2012-09-25T23:28:43.192Z",
 "updated": "2012-09-25T23:28:43.192Z",
 "etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
 "text": "Hello there!"
}
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: response-TIMELINE_INSERT_USER_3

HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 304

{
 "kind": "glass#timelineItem",
 "id": "5432109876",
 "selfLink": "https://www.googleapis.com/mirror/v1/timeline/5432109876",
 "created": "2012-09-25T23:28:43.192Z",
 "updated": "2012-09-25T23:28:43.192Z",
 "etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
 "text": "Hello there!"
}
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=--

I have a similar looking response with my batch request, but I'm having trouble getting each batch's kind. How can I retrieve each batch's 'kind??

Edit

I'm getting a similar response using Googl's Batch Request.

gclient = Google::APIClient.new(....)

batch = Google::APIClient::BatchRequest.new
batch.add(list_salesman_one).add(list_salesman_two)
batch = gclient.execute(batch).to_json
batch_decoded = ActiveSupport::JSON.decode(batch)  OR    batch_decoded = JSON.parse(batch)
batch_decoded_body = batch_decoded["response"]["body"]

Where batch_decoded_body gives me the response above. The OR in the variable batch_decoded means that I can use either one.

È stato utile?

Soluzione

You need to handle each entry in your batch separately:

  1. Separate batch to entries:

    entries = batch_decoded_body.strip.split(/^--.*/)
    
  2. The first entry is not really an entry, but a header, so you can get rid of it:

    entries.shift
    
  3. For each entry parse to a JSON, starting from {:

    entries.map { |entry| JSON.parse entry[/{.*/m] }
    

All together:

entries = batch_decoded_body.strip.split(/^--.*/)
entries.shift
entries.map { |entry| JSON.parse(entry[/{.*/m])['kind'] }
# => ["glass#timelineItem", "glass#timelineItem", "glass#timelineItem"] 
entries.map { |entry| JSON.parse(entry[/{.*/m])['items'].map { |item| item['ipAddress'] } }
# => [["IP_ADDRESS_HERE"], ["IP_ADDRESS_HERE"]] 

Altri suggerimenti

You should probably look into the json library. Example:

require 'json'
require 'open-uri'

request = open('http://ip.jsontest.com')
response = JSON.parse(request.read)
response["ip"]

You could use JSON.parse to read your response (I'll assume it's in a variable called response):

require 'json'
response_json = JSON.parse(response)
response_json['kind']

If your response is exactly like you have it above, say stored in a variable called str and you want to exctract the kinds as string array, you can do the following:

arr = str.scan(/\{.*?kind.*?\}/m)
puts arr
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top