How to get information from custom fields within SuperTags pulled from Batchbook CRM
I'm usi开发者_开发知识库ng the batchbook ruby gem to build a simple listing page pulling various fields out of my batchbook crm. If I pull all the attributes for a given company, I end up with something like this:
{"id"=>13, "name"=>"John Deer", "notes"=>nil, "small_image"=>nil, "large_image"=>nil, "tags"=>[#<BatchBook::Tag:0x00000001df6438 @attributes={"id"=>"1002", "name"=>"portfolio", "supertag"=>"true", "fields"=>#<BatchBook::Tag::Fields:0x00000001dee5a8 @attributes={"logo"=>"img/fr-logo-button-sm.png", "description"=>"We make tractors."}, @prefix_options={}>}, @prefix_options={}>], "locations"=>[#<BatchBook::Location:0x00000001dea048 @attributes={"id"=>14, "label"=>"main", "primary"=>true, "email"=>"***@johndeer.com", "website"=>"http://johndeer.com", "phone"=>nil, "cell"=>nil, "fax"=>nil, "street_1"=>nil, "street_2"=>nil, "city"=>nil, "state"=>nil, "postal_code"=>nil, "country"=>nil}, @prefix_options={}>], "mega_comments"=>[], "created_at"=>"Thu Jun 02 22:32:16 UTC 2011", "updated_at"=>"Thu Jun 02 22:40:03 UTC 2011"}
How can I parse this to pull just the "logo" or just the "description" from within my "portfolio" SuperTag?
Maybe it's simpler to use just the @company.supertag
object, which gives me this:
[{"id"=>15, "name"=>"portfolio", "fields"=>{"logo"=>"img/fr-logo-button-sm.png", "description"=>"We make tractors."}}]
but again, how can I pull out the individual fields "logo" or "description"?
I feel like this should be a simple process, and maybe I'm either struggling with the syntax or making it more complicated than it needs to be, but could you please help me out?
I'm not sure what your first hash-like string is all about so we'll look at the @company.supertag
output value:
[
{
"id" => 15,
"name" => "portfolio",
"fields" => {
"logo" => "img/fr-logo-button-sm.png",
"description" => "We make tractors."
}
}
]
I took the liberty of reformatting it into something readable. The square brackets on the outside indicate that it is an Array, the braces come in at the next level so the Array contains a Hash. Both Array and Hash use []
to access their elements so, if the whole data structure is in a
and we remember that an Array is indexed starting at 0, we start with this:
a[0]['fields']
And that gives us this inner hash:
"fields" => {
"logo" => "img/fr-logo-button-sm.png",
"description" => "We make tractors."
}
And taking it one step further:
logo = a[0]['fields']['logo']
desc = a[0]['fields']['description']
精彩评论