Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Created June 5, 2012 23:45
Show Gist options
  • Select an option

  • Save stefanoverna/2878916 to your computer and use it in GitHub Desktop.

Select an option

Save stefanoverna/2878916 to your computer and use it in GitHub Desktop.
Seconda implementazione
-
ChangeLog.md
LICENSE.txt
Gemfile.lock
doc/
pkg/
vendor/cache/*.gem
--colour --format documentation
--markup markdown --title "redditor Documentation" --protected

Seconda implementazione BDD

0.1.0 / 2012-06-04

  • Initial release:
require 'thor'
require 'redditor/story'
require 'redditor/stories_printer'
module Redditor
class CLI < Thor
desc "hot", "Shows the hottest stories on Reddit"
def hot
stories = Story.find(type: :hot)
StoriesPrinter.output(stories)
end
end
end
require 'redditor/cli'
require 'stringio'
module CLIMacros
attr_reader :output, :error
def redditor(command)
out = StringIO.new
err = StringIO.new
begin
$stdout = out
$stderr = err
cmd = cmd.kind_of?(Array) ? command : shell_split(command)
Redditor::CLI.start(cmd)
ensure
$stdout = STDOUT
$stderr = STDERR
end
raise RuntimeError, @error if @error.present?
end
def shell_split(line)
words = []
field = ''
line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
|word, sq, dq, esc, garbage, sep|
raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
field << (word || sq || (dq || esc).gsub(/\\(.)/, '\\1'))
if sep
words << field
field = ''
end
end
words
end
end
RSpec.configuration.include CLIMacros
require 'spec_helper'
require 'redditor/cli'
describe Redditor::CLI do
describe ".hot" do
it "fetches /hot.json stories, and prints them out" do
stories = double("stories array")
Redditor::Story.stub(:find).with(type: :hot).and_return(stories)
Redditor::StoriesPrinter.should_receive(:output).with(stories)
subject.hot
end
end
end
module FixtureMacros
def fixture(path)
File.expand_path("../../fixtures/#{path}", __FILE__)
end
def read_fixture(path)
File.read(fixture(path)).strip
end
end
RSpec.configuration.include FixtureMacros
source :rubygems
gemspec
group :development do
gem 'kramdown'
gem 'rubygems-tasks', '~> 0.2'
gem 'rspec', '~> 2.4'
gem 'yard', '~> 0.7'
gem 'bundler', '~> 1.0'
gem 'rake', '~> 0.8'
end
Copyright (c) 2012 Stefano Verna
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 above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
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.
# encoding: utf-8
require 'rubygems'
begin
require 'bundler'
rescue LoadError => e
warn e.message
warn "Run `gem install bundler` to install Bundler."
exit e.status_code
end
begin
Bundler.setup(:development)
rescue Bundler::BundlerError => e
warn e.message
warn "Run `bundle install` to install missing gems."
exit e.status_code
end
require 'rake'
require 'rubygems/tasks'
Gem::Tasks.new
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :test => :spec
task :default => :spec
require 'yard'
YARD::Rake::YardocTask.new
task :doc => :yard
#!/usr/bin/env ruby
require 'redditor/cli'
Redditor::CLI.start
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/redditor/version', __FILE__)
Gem::Specification.new do |gem|
gem.name = "redditor"
gem.version = Redditor::VERSION
gem.summary = %q{TODO: Summary}
gem.description = %q{Browse Reddit from CLI}
gem.license = "MIT"
gem.authors = ["Stefano Verna"]
gem.email = "stefano.verna@welaika.com"
gem.homepage = "https://github.com/stefanoverna/redditor#readme"
gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']
gem.add_dependency 'activesupport'
gem.add_dependency 'thor'
gem.add_dependency 'command_line_reporter'
gem.add_dependency 'json'
gem.add_dependency 'googl'
gem.add_dependency 'rest-client'
gem.add_dependency 'launchy'
gem.add_development_dependency 'bundler', '~> 1.0'
gem.add_development_dependency 'yard', '~> 0.7'
end
require 'redditor/version'
module Redditor
end
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┓
┃ # ┃ Story ┃ Pts/Comms. ┃ URL ┃
β”£β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”«
┃ 1 ┃ E3, We want games and gameplay, not this ┃ 3536/1835 ┃ http://goo.gl/86i6k ┃
β”£β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”«
┃ 2 ┃ TIL over 99% of birds "cleaned" after an oil spill die within weeks. C ┃ 2129/422 ┃ http://goo.gl/yHag ┃
┃ ┃ leaning birds is seen by many as merely a PR stunt by oil companies. ┃ ┃ ┃
β”£β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”«
┃ 3 ┃ Teen mom: their kids [first] world problem ┃ 2442/267 ┃ http://goo.gl/Pz3tN ┃
β”£β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”«
┃ 4 ┃ Norway goes secular, removes Lutheran Church as state religion ┃ 2379/542 ┃ http://goo.gl/ekMXJ ┃
β”£β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β•Šβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”β”«
┃ 5 ┃ Over-Educated Problems ┃ 1625/343 ┃ http://goo.gl/134ay ┃
┗━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━┛
require 'spec_helper'
describe "CLI usage" do
describe "redditor hot" do
it "returns the hottest news on Reddit" do
redditor "hot"
output.should == read_fixture("output/redditor_hot.txt")
end
end
end
require 'spec_helper'
require 'redditor/version'
describe Redditor do
it "should have a VERSION constant" do
subject.const_get('VERSION').should_not be_empty
end
end
require 'rspec'
require 'active_support/core_ext/object'
require 'support/cli_macros'
require 'support/fixture_macros'
module Redditor
class StoriesPrinter
end
end
module Redditor
class Story
def self.find(filters)
get(url_for(filters)).map do |raw_story|
Story.new(raw_story)
end
end
end
end
require 'spec_helper'
require 'redditor/story'
describe Redditor::Story do
describe "#find" do
it "gets the URL for the specified filters, and returns an array of Stories" do
url = double("Reddit URL")
raw_story = double("story hash")
story = double("story")
Redditor::Story.stub(:url_for).with(foo: :bar).and_return(url)
Redditor::Story.stub(:get).with(url).and_return([ raw_story ])
Redditor::Story.stub(:new).with(raw_story).and_return(story)
result = Redditor::Story.find(foo: :bar)
result.should have(1).story
result.first.should == story
end
end
end
module Redditor
# redditor version
VERSION = "0.1.0"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment