class RetroCasts::RailsCasts
Attributes
episodes[R]
filter[R]
host[R]
page[R]
search[R]
url[R]
website[R]
Public Class Methods
new(host: 'http://www.railscasts.com', filter: '.episode', page: 1, search: nil, website: RetroCasts::Website.new)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 5 def initialize(host: 'http://www.railscasts.com', filter: '.episode', page: 1, search: nil, website: RetroCasts::Website.new) @host = host @filter = filter @page = page @search = search @website = website update end
Public Instance Methods
episode(number)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 24 def episode(number) if episode?(number) episodes[number - 1] else nil end end
episode?(number)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 20 def episode?(number) number > 0 && number <= episodes.length end
get_search(search_term)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 32 def get_search(search_term) @search = search_term update end
list_episodes()
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 51 def list_episodes episodes.each_with_index do |episode, i| RetroCasts.display "#{i +1}. #{episode.title} - #{episode.date}" end end
next_page()
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 37 def next_page @page += 1 update end
prev_page()
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 42 def prev_page if page > 1 @page -= 1 update else self end end
show_episode_detail(episode_number)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 57 def show_episode_detail(episode_number) current_episode = episode(episode_number) [:title, :number, :date, :length, :description, :link].each do |attribute| label = attribute.to_s.capitalize message = "#{label}: #{current_episode.send(attribute)}" RetroCasts.display(message) end end
Private Instance Methods
build_url()
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 72 def build_url attributes = {} attributes[:search] = search if search attributes[:page] = page if page != 1 if !attributes.empty? query = URI.encode_www_form(attributes) end if attributes.has_key?(:search) "#{host}\/episodes?#{query}" elsif attributes.has_key?(:page) "#{host}\/?#{query}" elsif host != "" "#{host}\/" else "" end end
create_episode(node)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 99 def create_episode(node) Episode.new({ title: node.css(".main h2 a").text, number: extract_integer(node.css(".number")), date: Date.parse(node.css(".published_at").text), length: extract_integer(node.css(".stats")), link: host + node.css(".screenshot a").first.attributes["href"].value, description: node.css(".description") .text.sub(/\(\d+ minutes\)/, '') .delete("\n").strip }) end
extract_integer(data)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 112 def extract_integer(data) data.text.match(/\d+/).to_s.to_i end
parse_episodes(nodeset)
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 93 def parse_episodes(nodeset) nodeset.collect do |node| create_episode(node) end end
update()
click to toggle source
# File lib/retro_casts/rails_casts.rb, line 67 def update @url = build_url @episodes = parse_episodes(website.get_list(url, filter)) end