I'm trying to get to data that I found in a binding.pry --- Here is is
[#<Stripe::Card:0x3fc1da530e18 id=card_1A4KjTLcGwfBVD0DjTHzDqsO> JSON: {
"id": "card_1A4KjTLcGwfBVD0DjTHzDqsO",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": "42424",
"address_zip_check": "pass",
"brand": "Visa",
"country": "US",
"customer": "cus_AP91mImLV1GIrS",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 4,
"exp_year": 2024,
"fingerprint": "U2Lh3jtN9G5jgtxm",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
}]
I get all that data when I type this in the console: context.customer.sources.data
I've tried getting the object like this: context.customer.sources.data[:object] but that gives me this error => TypeError: no implicit conversion of Symbol into Integer
How do I get to that data?
Because it's an array. so you should visit like context.customer.sources.data[0][:object] or context.customer.sources.data[0].object
Related
I have a searchkick implemented in a Film model in rails. I want an autocomplete search and I have the autocomplete method in the FilmControlelrs class. I don't know how to define the fields in the autocomplete method for a jsonb attribute (in this case the title).
The Film model is
class Film < ApplicationRecord
searchkick
after_commit :reindex_film
end
The controller is:
class FilmsController < ApplicationController
def autocomplete
render json: Film.search(params[:query], {
fields: [???],
match: :word_start,
limit: 10,
load: false,
misspellings: {below: 5}
})
end
def index
search = params[:query].present? ? params[:query] : nil
#films = if search
self.autocomplete
else
render json: Film.all
end
end
end
The films data json reponse looks like:
{
"id": 1,
"budget": null,
"desription": null,
"external_ids": null,
"length": null,
"movie_type": "FILM",
"premiere_bluray": null,
"premiere_digital": null,
"premiere_dvd": null,
"premiere_world": null,
"rating": null,
"rating_age_limits": null,
"rating_mpaa": null,
"remote_images": null,
"solgan": null,
"slug": null,
"source": null,
"source_lang": "ru",
"title": {
"name": "Gadi",
"type": "extended"
},
"year": null,
"created_at": "2022-06-09T11:28:02.185Z",
"updated_at": "2022-06-09T11:28:02.185Z"
},
{
"id": 2,
"budget": null,
"desription": null,
"external_ids": null,
"length": null,
"movie_type": "FILM",
"premiere_bluray": null,
"premiere_digital": null,
"premiere_dvd": null,
"premiere_world": null,
"rating": null,
"rating_age_limits": null,
"rating_mpaa": null,
"remote_images": null,
"solgan": null,
"slug": null,
"source": null,
"source_lang": "ru",
"title": {
"name": "Gadi",
"type": "extended"
},
"year": null,
"created_at": "2022-06-09T11:31:50.743Z",
"updated_at": "2022-06-09T11:31:50.743Z"
}
]
I need to check the id in a array list , id has been generated previously and just I need to compare with the response generated later, For example: take "defaultcardid" is the previous generated id, so I need to compare the "defaultcard" with later response.
I am getting a "cardlist" as a "Anyobject" response. so need to check "defaultcardid" inside "cardlist" array.
if let xlist = (cardlist.value(forKey: "data")as? NSArray)
{
for 0 .. i in xlist
{
cardfinder.adding((xlist.object(at: id)as AnyObject).value(forKey: "id")as! String)
}
}
cardfinder is a NSMUTABLEARRAY to store the particular id into an array.
My son response is below
{
"status": "success",
"data": [
{
"id": "card_1D42hlLJSnYRR6BMULHyeobB",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "FwLfSpHMDz1b3PTH",
"funding": "debit",
"last4": "5556",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_1D441WLJSnYRR6BMNpOsFgg0",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "dvLGAxROCikDisQx",
"funding": "credit",
"last4": "0093",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_1D42RjLJSnYRR6BMgSELUUut",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "NnWxCq6FamUKl226",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
}
]
}
All the objects have field called "id" I already have a default id = card_1D42RjLJSnYRR6BMgSELUUut , I need to exactly take in which object my card is .
so I need a for loop for checking all the objects in array.
You can simply call contains method on array to check whether a value exist in it. Implementation depends upon the type of array you are checking.
Example:
Let's assume "123" is the default id.
var cardList1 = ["123", "abc", "xyz"]
cardList1.contains("123") //true
cardList1.contains("456") //false
var cardList2 = [["id": "123"], ["id": "abc"], ["id": "xyz"]]
cardList2.contains { //true
return $0["id"] == "123"
}
cardList2.contains { //false
return $0["id"] == "564"
}
Let me know in case you face any issue with this.
#ride.passengers
Gives me something like this:
[
{
"id": null,
"salutation": "Sr",
"first_name": "fsd",
"last_name": "sfd",
"email": "sf#sdf.sdf",
"mobile": "123234345",
"company_id": 7,
"cost_center_id": null,
"created_at": null,
"updated_at": null
},
{
"id": null,
"salutation": null,
"first_name": null,
"last_name": null,
"email": null,
"mobile": null,
"company_id": 12,
"cost_center_id": null,
"created_at": null,
"updated_at": null
}
]
I want to create an array with all company_ids:
[7,12]
I tried:
#ride.passengers.pluck(:company_id)
But this returns an empty error.
pluck is an active record method. You are trying to manipulate the data returned from an active record query, try
#rides.passengers.map{ |object| object["company_id"} }
i have the being fetched, i need to read whats inside data under sources
"id": "cus_7nDkZw63KvTuy5",
"object": "customer",
"account_balance": 0,
"created": 1453839669,
"currency": "usd",
"default_source": "card_17XdJE2eZvKYlo2CBNnle4YM",
"delinquent": false,
"description": null,
"discount": null,
"email": "someone#example.com",
"livemode": false,
"metadata": {
},
"shipping": null,
"sources": {
"object": "list",
"data": [
{
"id": "card_17XdJE2eZvKYlo2CBNnle4YM",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_7nDkZw63KvTuy5",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2016,
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": "someone#example.com",
"tokenization_method": null
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/customers/cus_7nDkZw63KvTuy5/sources"
},
"subscriptions": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/cus_7nDkZw63KvTuy5/subscriptions"
}
}
Try this code you will get array
[[[[result objectForKey:#"sources"] objectForKey:#"data"] objectAtIndex:0] objectForKey:#"country"];
you will get US as answer.
I have an array of hashes (or atleast I think they are hashes) and I need to pull out the ID for each of them. I'm sure ruby has some quick way of doing this ... I just can't figure it out.
I don't want to iterate through the arrays and build a new one.
[
[
{
"bio": "I am a tech geek who loves starting up companies. While I was in college, I founded Squeeze My Tees",
"business_name": "Rounded Development",
"city": "",
"created_at": "2012-04-22T18:07:44Z",
"first_name": "Brian",
"id": 1,
"industry": "Entertainment",
"last_name": "Weinreich",
"lat": null
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:35Z",
"email": "test#jambo.com",
"first_name": "asdad",
"id": 2,
"industry": null,
"last_name": "ddfs",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:35Z"
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:39Z",
"email": "tesasdat#jambo.com",
"first_name": "fdsd",
"id": 3,
"industry": null,
"last_name": "asdgw",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:39Z"
},
{
"access_token": null,
"bio": null,
"business_name": null,
"city": null,
"created_at": "2012-04-23T13:56:44Z",
"email": "asdsad#jambo.com",
"first_name": "ewtrwef",
"id": 4,
"industry": null,
"last_name": "dfd",
"lat": null,
"linkedin_id": null,
"linkedin_url": null,
"lng": null,
"position": null,
"professional_headline": null,
"state": null,
"street": null,
"updated_at": "2012-04-23T13:56:44Z"
}
]
]
To pull out just the IDs you can do this:
the_IDs = array_of_hashes.collect { |single_array| single_array["id"] }
Obviously you can use less verbose variable names, they're just for illustration. But the idea is that you can loop through an array and collect whatever the block returns. In this case, you keep getting an ID returned, and the_IDs will just be an array of what was collected.