Skip to content

Instantly share code, notes, and snippets.

@toretore
Forked from patrykkalinowski/data.rb
Last active August 29, 2015 14:21
Show Gist options
  • Save toretore/b6a9e8c25dd1fcdd93e9 to your computer and use it in GitHub Desktop.
Save toretore/b6a9e8c25dd1fcdd93e9 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 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