Sha256: e6b7f976338a3f7ab9aba252f85b554d910138b76715ecb748bfba9c7f628aab

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

module VimGolfFinder
  class Parser
    BASE_URL = 'http://vimgolf.com'

    def fetch_challenges
      challenges = []

      doc = Nokogiri::HTML(open(BASE_URL))
      doc.css('.grid_7 > div').each do |div|
        challengeDOM = div.at_css('h5.challenge')
        aTAG = challengeDOM.at_css('a')
        challenge = VimGolfFinder::Challenge.new
        challenge.id = aTAG['href'][12..-1]
        challenge.title = aTAG.content
        challenge.entries = challengeDOM.content.match(/- (\d*) entries/).captures.first.to_i
        challenges << challenge
      end

      challenges
    end

    def get_challenge(id)
      challenge = VimGolfFinder::Challenge.new

      doc = Nokogiri::HTML(open("#{BASE_URL}/challenges/#{id}", 'Accept' => 'text/html'))

      doc.css('.grid_7:not(#about)').each do |dom|
        challenge.id = id
        challenge.title = dom.at_css('h3 b').content
        challenge.description = dom.at_css('p').content
        challenge.start_file = dom.at_css('.prettyprint').content
        challenge.end_file = dom.css('.prettyprint')[1].content
        challenge.view_diff = dom.at_css('pre.diff').content
      end

      challenge
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vimgolf-finder-0.1.7 lib/vimgolf_finder/parser.rb
vimgolf-finder-0.1.6 lib/vimgolf_finder/parser.rb
vimgolf-finder-0.1.5 lib/vimgolf_finder/parser.rb
vimgolf-finder-0.1.4 lib/vimgolf_finder/parser.rb
vimgolf-finder-0.1.3 lib/vimgolf_finder/parser.rb
vimgolf-finder-0.1.2 lib/vimgolf_finder/parser.rb
vimgolf-finder-0.1.1 lib/vimgolf_finder/parser.rb