Module: Nanowrimo

Defined in:
lib/nanowrimo.rb,
lib/nanowrimo/core.rb,
lib/nanowrimo/site.rb,
lib/nanowrimo/user.rb,
lib/nanowrimo/cache.rb,
lib/nanowrimo/genre.rb,
lib/nanowrimo/region.rb

Overview

! /usr/bin/env ruby -w

Defined Under Namespace

Classes: Cache, Core, Genre, Region, Site, User

Constant Summary collapse

VERSION =

Current API version

'0.8.0'
API_URI =

Current static root WCAPI uri

'http://www.nanowrimo.org/wordcount_api'
GOAL =

Current individual user word count goal. For fun!

50_000

Class Method Summary collapse

Class Method Details

.data_from_cache(path, key) ⇒ Object

Finds the data in the cache and returns it, or nil, taking into account age of cache data.



40
41
42
43
# File 'lib/nanowrimo.rb', line 40

def self.data_from_cache(path, key)
  type = path.split('/').first
  Nanowrimo::Cache.find_data(type, key)
end

.data_from_internets(path, key, attribs) ⇒ Object

Parses XML from the WCAPI and returns an array of hashes with the data. Caches it, too.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nanowrimo.rb', line 46

def self.data_from_internets(path, key, attribs)
  type = path.split('/').first
  uri = "#{API_URI}/#{type}"
  uri = "#{uri}/#{key}" unless key.nil?
  result = []
  begin
    timeout(2) {
      doc = Nokogiri::XML(open(uri))
      doc.xpath("#{type}/error").each {|e|
        result << {:error => e.content}
      }
      return result unless result.empty?
      doc.xpath(path).each {|n|
        node = {}
        attribs.each {|d|
          node[d.intern] = n.at(d).content unless n.at(d).nil?
        }
        result << node
      }
    }
  rescue Timeout::Error
    throw NanowrimoError, "Timed out attempting to connect to Nanowrimo.org"
  end
  key ||= type # kinda hackish, but for the site stats it's needed
  Nanowrimo::Cache.add_to_cache("#{type}","#{key}",result)
  result
end

.parse(path, key, attribs, options = {:force => false}) ⇒ Object

Pull requested data from cache or from the WCAPI



34
35
36
37
# File 'lib/nanowrimo.rb', line 34

def self.parse(path, key, attribs, options={:force => false})
  result = options[:force] == true ? data_from_internets(path, key, attribs) : data_from_cache(path, key) ||
    data_from_internets(path, key, attribs)
end