-
-
Save toretore/b6a9e8c25dd1fcdd93e9 to your computer and use it in GitHub Desktop.
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
class GetData | |
@@logger = Logger.new('log.log') | |
def initialize | |
@yesterday = Date.today.prev_day | |
@token = xxx | |
end | |
def save_actions | |
with_database do |db| | |
actions.each do |company| | |
name, actions = company['label'], company['nb_actions'] | |
db.execute("INSERT INTO data (date, name, actions) VALUES (?, ?, ?)", [@yesterday.to_s, name, actions]) | |
end | |
end | |
@@logger.info "Saved #{@yesterday} action data" | |
end | |
def actions | |
response_json(period: 'day', method: 'CustomVariables.getCustomVariablesValuesFromNameId', SubTable: '2') | |
end | |
def response(params) | |
RestClient.get("https://XXX/index.php?#{format_params(params)}") | |
end | |
def response_json(p) | |
JSON.parse(response(p)) | |
end | |
def format_params(params) | |
{token_auth: @token, date: @yesterday.strftime('%Y-%m-%d'), :module => 'API'}.merge(params).map{|k,v| "#{k}=#{v}" }.join('&') | |
end | |
def with_database | |
db = SQLite3::Database.new "database.db" | |
yield db | |
db.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment