<commit_before>#!/usr/bin/env ruby

# see: http://fitbitclient.com/guide/playing-with-the-fitgem-api

require 'rubygems'
require 'bundler/setup'
require "fitgem"
require "yaml"

# Load the existing yml config
config = begin
  Fitgem::Client.symbolize_keys(YAML.load(File.open("config.yml")))
rescue ArgumentError => e
  puts "Could not parse YAML: #{e.message}"
  exit
end

client = Fitgem::Client.new(config[:oauth])

# With the token and secret, we will try to use them
# to reconstitute a usable Fitgem::Client
if config[:oauth][:token] && config[:oauth][:secret]
  begin
    access_token = client.reconnect(config[:oauth][:token], config[:oauth][:secret])
  rescue Exception => e
    puts "Error: Could not reconnect Fitgem::Client due to invalid keys in .fitgem.yml"
    exit
  end
# Without the secret and token, initialize the Fitgem::Client
# and send the user to login and get a verifier token
else
  request_token = client.request_token
  token = request_token.token
  secret = request_token.secret

  puts "Go to http://www.fitbit.com/oauth/authorize?oauth_token=#{token} and then enter the verifier code below"
  verifier = gets.chomp

  begin
    access_token = client.authorize(token, secret, { :oauth_verifier => verifier })
  rescue Exception => e
    puts "Error: Could not authorize Fitgem::Client with supplied oauth verifier"
    exit
  end

  puts 'Verifier is: '+verifier
  puts "Token is:    "+access_token.token
  puts "Secret is:   "+access_token.secret

  user_id = client.user_info['user']['encodedId']
  puts "Current User is: "+user_id

  config[:oauth].merge!(:token => access_token.token, :secret => access_token.secret, :user_id => user_id)

  # Write the whole oauth token set back to the config file
  File.open(".fitgem.yml", "w") {|f| f.write(config.to_yaml) }
end

# ============================================================
# Add Fitgem API calls on the client object below this line

def beers_from_steps(steps)
  return 0 if steps < 10000
  return 1 if steps < 17500
  return 2 if steps < 22000
  3
end

current_date = Date.new(2015,1,26)

while current_date <= Date.today
  data = client.activities_on_date current_date
  steps = data["summary"]["steps"]
  puts "#{current_date.to_s} had #{steps} steps, for #{beers_from_steps(steps)} beers."<commit_msg>continue unifying config file<commit_after>#!/usr/bin/env ruby

# see: http://fitbitclient.com/guide/playing-with-the-fitgem-api

require 'rubygems'
require 'bundler/setup'
require "fitgem"
require "yaml"

# Load the existing yml config
config = begin
  Fitgem::Client.symbolize_keys(YAML.load(File.open("config.yml")))
rescue ArgumentError => e
  puts "Could not parse YAML: #{e.message}"
  exit
end

client = Fitgem::Client.new(config[:fitbit])

# With the token and secret, we will try to use them
# to reconstitute a usable Fitgem::Client
if config[:oauth][:token] && config[:fitbit][:secret]
  begin
    access_token = client.reconnect(config[:fitbit][:token], config[:fitbit][:secret])
  rescue Exception => e
    puts "Error: Could not reconnect Fitgem::Client due to invalid keys in .fitgem.yml"
    exit
  end
# Without the secret and token, initialize the Fitgem::Client
# and send the user to login and get a verifier token
else
  request_token = client.request_token
  token = request_token.token
  secret = request_token.secret

  puts "Go to http://www.fitbit.com/oauth/authorize?oauth_token=#{token} and then enter the verifier code below"
  verifier = gets.chomp

  begin
    access_token = client.authorize(token, secret, { :oauth_verifier => verifier })
  rescue Exception => e
    puts "Error: Could not authorize Fitgem::Client with supplied oauth verifier"
    exit
  end

  puts 'Verifier is: '+verifier
  puts "Token is:    "+access_token.token
  puts "Secret is:   "+access_token.secret

  user_id = client.user_info['user']['encodedId']
  puts "Current User is: "+user_id

  config[:fitbit].merge!(:token => access_token.token, :secret => access_token.secret, :user_id => user_id)

# TODO: need to merge in the rest of the configuration here... ?

  # Write the whole oauth token set back to the config file
  File.open("config.yml", "w") {|f| f.write(config.to_yaml) }
end

# ============================================================
# Add Fitgem API calls on the client object below this line

def beers_from_steps(steps)
  return 0 if steps < 10000
  return 1 if steps < 17500
  return 2 if steps < 22000
  3
end

current_date = Date.new(2015,1,26)

while current_date <= Date.today
  data = client.activities_on_date current_date
  steps = data["summary"]["steps"]
  puts "#{current_date.to_s} had #{steps} steps, for #{beers_from_steps(steps)} beers."
{'fraction_non_alphanumeric': 0.13904314395557454, 'fraction_numerical': 0.011106364801366937, 'mean_word_length': 4.588305489260144, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7008676', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1494, 'n_words': 457}