-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,46 @@ | ||
class GetNetZeroData | ||
require 'net/http' | ||
require 'json' | ||
require 'csv' | ||
module NetZero | ||
class GetNetZeroData | ||
require 'net/http' | ||
require 'json' | ||
require 'csv' | ||
|
||
def self.perform | ||
url = URI("https://zerotracker.net/api/v1/companies") | ||
response = Net::HTTP.get_response(url) | ||
def self.perform | ||
url = URI("https://zerotracker.net/api/v1/companies") | ||
response = Net::HTTP.get_response(url) | ||
|
||
if response.is_a?(Net::HTTPSuccess) | ||
data = JSON.parse(response.body) | ||
companies_data = [] | ||
if response.is_a?(Net::HTTPSuccess) | ||
data = JSON.parse(response.body) | ||
companies_data = [] | ||
|
||
data.first(10).each do |company| | ||
company_url = URI(company['api_url']) | ||
p company_url | ||
company_response = Net::HTTP.get_response(company_url) | ||
data.first(10).each do |company| | ||
company_url = URI(company['api_url']) | ||
p company_url | ||
company_response = Net::HTTP.get_response(company_url) | ||
|
||
if company_response.is_a?(Net::HTTPSuccess) | ||
company_data = JSON.parse(company_response.body) | ||
p company_data | ||
companies_data << company_data | ||
else | ||
puts "Failed to retrieve data for #{company['name']}. Error: #{company_response.code} - #{company_response.message}" | ||
if company_response.is_a?(Net::HTTPSuccess) | ||
company_data = JSON.parse(company_response.body) | ||
p company_data | ||
companies_data << company_data | ||
else | ||
puts "Failed to retrieve data for #{company['name']}. Error: #{company_response.code} - #{company_response.message}" | ||
end | ||
end | ||
end | ||
|
||
p "save" | ||
save_to_csv(companies_data) | ||
else | ||
puts "Failed to retrieve net zero data. Error: #{response.code} - #{response.message}" | ||
p "save" | ||
save_to_csv(companies_data) | ||
else | ||
puts "Failed to retrieve net zero data. Error: #{response.code} - #{response.message}" | ||
end | ||
end | ||
end | ||
|
||
def self.save_to_csv(data) | ||
CSV.open("storage/csv/net_zero_data.csv", "wb") do |csv| | ||
csv << data.first.keys | ||
data.each do |company| | ||
csv << company.values | ||
def self.save_to_csv(data) | ||
CSV.open("storage/csv/net_zero_data.csv", "wb") do |csv| | ||
csv << data.first.keys | ||
data.each do |company| | ||
csv << company.values | ||
end | ||
end | ||
puts "Data saved to net_zero_data.csv" | ||
end | ||
puts "Data saved to net_zero_data.csv" | ||
end | ||
end |