Skip to content

Commit

Permalink
add Net Zero module
Browse files Browse the repository at this point in the history
  • Loading branch information
Ches-ctrl committed May 26, 2024
1 parent 8b9e39f commit 750781d
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions app/services/net_zero/get_net_zero_data.rb
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

0 comments on commit 750781d

Please sign in to comment.