Here is an example hash (maximum 10 per query)
parsed_response = [{"id"=>3, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/mobile-4", "name"=>"Mobile", "type"=>"MarketTag"}, {"id"=>12, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/enterprise-software", "name"=>"Enterprise Software", "type"=>"MarketTag"}, {"id"=>10, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/saas", "name"=>"SaaS", "type"=>"MarketTag"}, {"id"=>841, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/software", "name"=>"Software", "type"=>"MarketTag"}, {"id"=>263, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/restaurants-2", "name"=>"Restaurants", "type"=>"MarketTag"}, {"id"=>376, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/productivity-software", "name"=>"Productivity Software", "type"=>"MarketTag"}, {"id"=>942, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/embedded-hardware-and-software", "name"=>"Embedded Hardware and Software", "type"=>"MarketTag"}, {"id"=>291, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/meeting-software", "name"=>"Meeting Software", "type"=>"MarketTag"}, {"id"=>13764, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/hardware-software", "name"=>"Hardware + Software", "type"=>"MarketTag"}, {"id"=>2983, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/software-compliance", "name"=>"Software Compliance", "type"=>"MarketTag"}]
I’ve tried many methods inside of the gem I’ve been creating.
The first method was to create a special class and map each array element to that class:
class IDSearch
attr_accessor :id, :thumbnail, :url, :name, :type
def initialize(the_hash)
@id = the_hash['id']
@url = the_hash['url']
@pic = the_hash['pic']
@name = the_hash['name']
@type = the_hash['type']
end
end
I’ll list them according to attempt:
Attempt 1:
IDSearch.new(parsed_response)
Attempt 2:
parsed_response.map {|t| IDSearch.new(t)}
Attempt 3:
parsed_response.each do |hsh|
SimpleAngel::IDSearch.new(hsh)
end
Attempt 4:
10.times do |i|
instance_variable_set "@response_#{i}", parsed_response[i]
end
Attempt 5 (getting desperate)
@response0 = parsed_response[0]
@response1 = parsed_response[1]
@response2 = parsed_response[2]
@response3 = parsed_response[3]
@response4 = parsed_response[4]
@response5 = parsed_response[5]
@response6 = parsed_response[6]
@response7 = parsed_response[7]
@response8 = parsed_response[8]
@response9 = parsed_response[9]
Attempt6: (this time inside of a rails controller)
search_object = SimpleAngel::Search.new
responses = search_object.id_search(params[:query], params[:type])
responses.each_with_index do |response, i|
u = Idquery.new
u.name = response[i]['name']
u.id = response[i]['id']
u.url = response[i]['url']
u.type = response[i]['type']
u.pic = response[i]['pic']
u.save
end
None of these methods has produced a predictable, coherent, workable response.
More than an actual solution, I’m looking for a clue as to how my thinking has been flawed.
In Attempt1 you are expecting a hash in the
IDSearch.newand actually passing an array.If you instead do:
it works just fine.