I wrote a small program to interact with the Indeed Ruby API.
I can print a single job title, but would like to loop through them all.
Here is the code:
require 'nokogiri'
require 'indeed-ruby'
client = Indeed::Client.new ("PUBLISHER_KEY_GOES_HERE")
params = {
:q => 'python',
:l => 'vancouver',
:userip => '1.2.3.4',
:useragent => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)',
:jobkeys => ["JOB_KEY_A", "JOB_KEY_B"]
}
data = client.search(params)
# loop through all results and print the job title
data.each do |d|
puts d["results"]["jobtitle"]
end
each
i.rb:23:in `[]': no implicit conversion of String into Integer (TypeError)
from i.rb:23:in `block in <main>'
from i.rb:22:in `each'
from i.rb:22:in `<main>'
["results"]
["jobtitle"]
puts data["results"][0]["jobtitle"]
puts data["results"][0]["jobtitle"]
puts data["results"][1]["jobtitle"]
puts data["results"][2]["jobtitle"]
[i]
{"version"=>2,
"query"=>"python",
"location"=>"vancouver",
"paginationPayload"=>"",
"radius"=>25,
"dupefilter"=>true,
"highlight"=>true,
"totalResults"=>483,
"start"=>1,
"end"=>10,
"pageNumber"=>0,
"results"=>
[{"jobtitle"=>"Junior Software Developer",
"company"=>"LaunchCode",
"city"=>"Portland",
"state"=>"OR",
"country"=>"US",
"formattedLocation"=>"Portland, OR",
"source"=>"LaunchCode",
"date"=>"Fri, 03 Feb 2017 04:10:27 GMT",
"snippet"=>
"Familiarity with an at least one imperative (Java, JavaScript, PHP, C#, Objective-C, C/C++, <b>Python</b>, Ruby, etc.). Don’t have a CS degree?...",
"url"=>
"http://www.indeed.com/viewjob?jk=9f75f0ea8825e3a8&qd=X0KuMlb--hp3Z0o2UU7dJOXoIlOcgm8VSZO61KKa0UOtGpLfFk1WY111OhfFWzZjMBRv9LrdGhB8olLNQGabmQRFit3-lRPP9j12GNvnf88&indpubnum=4334069173238194&atk=1b87s51b1a0kqb7s",
"onmousedown"=>"indeed_clk(this,'782');",
"jobkey"=>"9f75f0ea8825e3a8",
"sponsored"=>false,
"expired"=>false,
"indeedApply"=>false,
"formattedLocationFull"=>"Portland, OR",
"formattedRelativeTime"=>"2 days ago",
"stations"=>""},
for i in 1...10
puts data["results"][i]["jobtitle"]
end
Junior Software Developer
Data Scientist
Python Developer
Python Automation Developer - Hillsboro, OR
Computer Vision Engineer
Python Web Engineer
EMS Network Applications Engineer II
Software Engineer
Python Developer (full-stack)
Electrical Engineer, EMS Network Applications
So, data["results"]
is an array. In ruby, you don't need an index to go through an array. You can do something like
[1, 2, 3].each do |i|
puts i
end
In your case,
data["results"].each do |item|
puts item["jobtitle"]
end