module TrickOrTreat
Constants
- VERSION
Attributes
treat_file[RW]
uri[RW]
Public Instance Methods
ask(args=[])
click to toggle source
# File lib/trick_or_treat.rb, line 17 def ask(args=[]) printf "\e[1;38;05;202mTrick or Treat? " no_candy if Gem.win_platform? return trick if args.delete('trick') return treat if args.delete('treat') no_candy end
get_links()
click to toggle source
# File lib/trick_or_treat.rb, line 37 def get_links nodes=Nokogiri::HTML(open(uri)).css('a') nodes.map{|n| n['href'] if n.attributes['class'].value.match /trick/i}.compact end
no_candy()
click to toggle source
# File lib/trick_or_treat.rb, line 66 def no_candy return "\e[1;38;05;165mNo candy for you\e[0m" end
tempfile(headers={})
click to toggle source
# File lib/trick_or_treat.rb, line 25 def tempfile(headers={}) stream = open([uri,treat_file].join('/'), "rb", headers) return stream if stream.respond_to?(:path) Tempfile.new.tap do |file| file.binmode IO.copy_stream(stream, file) stream.close file.rewind end end
treat()
click to toggle source
# File lib/trick_or_treat.rb, line 55 def treat printf "\e[1;38;05;165mTreat\e[0m\n" headers={} headers={'user-agent' => 'treat'} treats=YAML.load_file(tempfile(headers)) treats[treats.keys.sample].values.sample rescue OpenURI::HTTPError => e response=e.io response.string end
trick()
click to toggle source
# File lib/trick_or_treat.rb, line 42 def trick puts "\e[1;38;05;165mTrick\e[0m" links=get_links link=links.sample f=open([uri,link].join('/')) html=Nokogiri::HTML(f) puts title=html.css('h1').text.gsub(/ +|\n/, '') content=html.css('.post-content') IO.popen("less", "w") do |f| f.puts "\e[1m#{title}\e[0m\n#{content.text}" end end