Created
February 20, 2009 05:33
-
-
Save josephholsten/67328 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
#!/usr/bin/env ruby | |
# Copyright (c) 2009 Joseph Anthony Pasquale Holsten <joseph@josephholsten.com> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# homepage: http://tracker.216brew.com/ | |
# Options | |
# $0 -m "216 weight" | |
# $0 -k "03724E57-E596-B87B-7CD1-585B3FD3AE99" -m "216 weight" | |
# echo "216 weight" | $0 | |
require 'rubygems' | |
module Tracker | |
Version = [0,1,0] | |
class Options | |
def initialize | |
begin | |
@options = {} | |
load_defaults | |
load_configuration | |
load_arguments | |
rescue StandardError => e | |
puts e | |
usage | |
exit | |
end | |
end | |
def load_configuration | |
config = {} | |
begin | |
raw_config = File.read(@options[:config]) | |
config = YAML.load(raw_config) | |
rescue StandardError => e | |
raise ArgumentError, "Couldn't read config file: #{@options[:config]}", [e] | |
end | |
@options.merge! config | |
end | |
def option_parser(args = {}) | |
require 'optparse' | |
options = OptionParser.new do |opts| | |
opts.on("-f", "--file [FILE]", "File containing the measurement") do |opt| | |
args[:file] = File.new(opt) | |
end | |
opts.on("-k", "--api-key [ARG]", "Tracker API key") do |opt| | |
args[:api_key] = opt | |
end | |
opts.on("-m", "--message [MESSAGE]", "Measurement to track") do |opt| | |
args[:message] = opt | |
end | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
opts.on_tail("-V","--version", "Show version") do | |
puts Tracker::Version.join('.') | |
exit | |
end | |
end | |
end | |
def load_arguments | |
args = {} | |
parser = option_parser(args) | |
begin | |
parser.parse!(ARGV) | |
@options.merge! args | |
load_message | |
rescue OptionParser::ParseError => e | |
raise ArgumentError, "Couldn't parse arguments", [e] | |
end | |
end | |
def load_message | |
begin | |
@options[:message] ||= @options[:file].read | |
rescue StandardError => e | |
raise ArgumentError, "Coundn't read input file: #{@options[:file]}", [e] | |
end | |
end | |
def load_defaults | |
defaults = { | |
:uri => "http://tracker.216brew.com/api/quick/", | |
:file => $stdin, | |
:config => ENV['HOME'] + "/.trackerrc" | |
} | |
@options.merge! defaults | |
end | |
def [](key) | |
@options[key] | |
end | |
def usage | |
puts option_parser | |
end | |
end | |
class Controller | |
def initialize | |
@options = Options.new | |
end | |
def track | |
srv = Service.new(@options) | |
srv.create(@options[:message].strip) | |
end | |
end | |
class Service | |
attr_accessor :base_uri, :api_key | |
# takes a hash of options, including | |
# uri: the uri string for the API endpoint | |
# api_key: the unique API key GUID to authenticate the user | |
def initialize(options) | |
require 'uri' | |
@base_uri = URI.parse(options[:uri]) | |
@api_key = options[:api_key] | |
end | |
# creates a new measurement in the tracker | |
# takes a message string describing the measurement | |
# examples: | |
# 216.5 lbs weight yesterday: records measure, statistic, and time | |
# 216.5 lbs weight: implies time is now | |
# 216.5 weight: implies default unit of measure | |
# drink: implies a quantity | |
def create(message) | |
require 'net/http' | |
data = {:message => message} | |
uri = @base_uri + @api_key | |
uri.query = data.urlencode | |
Net::HTTP.get_print uri | |
end | |
end | |
end | |
class Hash | |
def urlencode | |
require 'cgi' | |
to_a.map do |name_value| | |
name_value.map {|e| CGI.escape e.to_s }.join '=' | |
end.join '&' | |
end | |
end | |
Tracker::Controller.new.track |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment