/
fletcher
69 lines (62 loc) · 2.04 KB
/
fletcher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
require 'rubygems'
require 'commander/import'
require 'yaml'
require File.expand_path("../../lib/fletcher", __FILE__)
#require 'commander-foo'
program :version, Fletcher.version #Commander-foo::VERSION
program :description, 'A cross-website product information fetcher'
program :help, 'Author', 'Dave Hulihan <dhulihan@gmail.com>'
#default_command :fetch
command :fetch do |c|
c.syntax = 'fletcher fetch [URL]'
c.summary = 'Fetch product information'
c.description = ''
c.example 'Fetch an amazon product', 'fletcher "http://www.example.com/product-id"'
#c.option '--format [yaml|json]', String, 'The format of the product details'
#c.option '--timeout SECONDS', Integer, 'Timeout in seconds'
c.option '--only [name|description|price|image]', String, 'Fetch a single attribute from the product.'
c.action do |args, options|
# Do something or c.when_called Commander-foo::Commands::Hello
options.default :format => :yaml
url = args.first
if url
product = Fletcher.fetch url
# Prep output
output_hash = Hash.new
output_hash["name"] = product.name
output_hash["description"] = product.description if product.description
output_hash["price"] = product.price.format if product.price
if product.image
output_hash["image"] = {
"src" => product.image.src
}
end
output_hash["url"] = product.url
# Get single attribute
if options.only
case options.only
when "image"
value = output_hash["image"]["src"]
else
value = output_hash[options.only]
end
value ? say(value.to_s) : say("Unknown attribute: #{options.only}")
else
say output_hash.to_yaml
end
else
puts c.syntax
end
end
end
command :websites do |c|
c.syntax = 'fletcher websites'
c.summary = 'Get a list of supported websites'
c.action do |args, options|
say "Supported Websites:"
for model in Fletcher.models.sort
say "\t#{model.to_s.capitalize}"
end
end
end