Skip to content

Instantly share code, notes, and snippets.

@patrykkalinowski
Last active August 29, 2015 14:21
Show Gist options
  • Save patrykkalinowski/9820504900e042dad074 to your computer and use it in GitHub Desktop.
Save patrykkalinowski/9820504900e042dad074 to your computer and use it in GitHub Desktop.
class GetData
@@logger = Logger.new('log.log')
def initialize
@yesterday = Date.today.prev_day
@token = xxx
end
def get_actions
# GET actions of all companies from previous day
response = RestClient.get("https://XXX/index.php?token_auth=#{@token}&format=json&date=#{@yesterday}&period=day&idSite=3&module=API&method=CustomVariables.getCustomVariablesValuesFromNameId&idSubtable=2")
json = JSON.parse(response)
# Open a database
db = SQLite3::Database.new "database.db"
json.each do |company|
name = company['label']
actions = company['nb_actions']
# Execute inserts with parameter markers
db.execute("INSERT INTO data (date, name, actions)
VALUES (?, ?, ?)", [@yesterday.to_s, name, actions])
puts "Saved #{@yesterday} action data of #{name}"
end
@@logger.info "Saved #{@yesterday} action data"
rescue => e
retry
else
@@logger.fatal "Error occured in GetData.get_actions: #{e.class} - #{e.message}"
end
def first_day
if Date.today.day == 1
@@logger.info "Today is first day of month."
true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment