Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
en: | ||
fletcher: | ||
models: | ||
amazon: | ||
name: Amazon | ||
url: http://www.amazon.com | ||
ebay: | ||
name: eBay | ||
url: http://www.ebay.com | ||
etsy: | ||
name: Etsy | ||
url: http://www.etsy.com | ||
gamecouk: | ||
name: Game.co.uk | ||
url: http://www.game.co.uk | ||
googleshopping: | ||
name: Google Shopping | ||
url: http://www.google.com/shopping | ||
playcom: | ||
name: Play.com | ||
url: http://www.play.com | ||
steam: | ||
name: Steam | ||
url: http://store.steampowered.com | ||
thinkgeek: | ||
name: ThinkGeek | ||
url: http://www.thinkgeek.com |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "spec_helper" | ||
#bin_path = File.expand_path("../../../bin/fletcher", __FILE__) | ||
load File.expand_path("../../../bin/fletcher", __FILE__) | ||
|
||
def mock_terminal | ||
@input = StringIO.new | ||
@output = StringIO.new | ||
$terminal = HighLine.new @input, @output | ||
end | ||
|
||
describe "Fletcher CLI" do | ||
before :each do | ||
mock_terminal | ||
end | ||
|
||
describe "fetch command", :vcr do | ||
before :each do | ||
@command = command :fetch | ||
end | ||
|
||
it "should return prompt without url" do | ||
@command.run Factory(:amazon).url | ||
# @output.to_s.should == @command.syntax | ||
end | ||
|
||
it "works with valid product url" do | ||
lambda{ | ||
@command.run Factory(:amazon).url | ||
}.should_not raise_error | ||
end | ||
end | ||
|
||
describe "websites command" do | ||
before :each do | ||
@command = command :websites | ||
end | ||
|
||
it "should work without any errors" do | ||
lambda{@command.run}.should_not raise_error | ||
end | ||
end | ||
end |