Permalink
Browse files

0.2.0 Rails support

  • Loading branch information...
1 parent a8e8dd1 commit d0f61ccebce13fa429a53c4753602a7133464739 @drush drush committed Aug 20, 2010
Showing with 157 additions and 37 deletions.
  1. +18 −7 README.rdoc
  2. +1 −1 Rakefile
  3. +1 −1 VERSION
  4. +124 −21 lib/nocms.rb
  5. +4 −4 nocms.gemspec
  6. +9 −3 test/test_nocms.rb
View
@@ -1,18 +1,29 @@
-= nocms
+= NOCMS
-NOCMS is a web service that allows you to add basic Content Management to any website. It provides wiki-style editing with a
-few lines of Javascript, and server-sdie rendering and user authentication support with just some simple server-side integration.
+NOCMS is a web service that allows you to add basic Content Management to any website.
+It provides wiki-style editing with a few lines of Javascript, and server-side rendering
+and user authentication support with just some simple server-side integration.
-If you've ever been involved in hand-to-hand combat with a big, ugly, hard to customize Content Management System (CMS), then NOCMS might be right for your.
+If you've ever been involved in hand-to-hand combat with a big, ugly, hard to customize
+Content Management System (CMS), then NOCMS might be right for you.
+
+Website: http://www.nocms.org
+Mailing List: http://groups.google.com/group/nocms
+Twitter: http://twitter.com/powcloud
+
+== Changes
+
+* 0.2.0
+ * Added Rails support incl. caching
+ * First stab at unit tests
+* 0.1.0 First prototype of the ruby gem - Sinatra support
-http://www.nocms.org/
== Note on Patches/Pull Requests
* Fork the project.
* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
+* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.
View
@@ -10,7 +10,7 @@ begin
gem.email = "nocms@powcloud.com"
gem.homepage = "http://github.com/powcloud/nocms"
gem.authors = ["Darren Rush"]
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
+ gem.add_development_dependency "shoulda"
gem.add_dependency "activesupport"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
View
@@ -1 +1 @@
-0.1.4
+0.2.0
View
@@ -3,38 +3,141 @@
require 'uri'
require 'cgi'
-@@nocms_pages = nil
# This is a default, that might be override by the app
-@@nocms_lang_default = 'en'
-def nocms_can_edit?()
- settings.environment != :production
+module NoCMS
+ class Cache < Hash
+ def read(key)
+ self[key]
+ end
+
+ def write(key, value)
+ self[key] = value.to_s
+ end
+ end
+
+ class Base
+ class << self
+ attr_accessor :site
+ attr_accessor :lang_default
+
+ NoCMS::Base.lang_default = 'en'
+
+ #@@nocms_lang_default = 'en'
+ @@nocms_cache = NoCMS::Cache.new
+
+ def environment
+ return RAILS_ENV if rails?
+ return (!!defined? settings && !settings.environment.nil?) ? settings.environment : 'development-nocms-test'
+ end
+
+ def rails?
+ !!defined? Rails
+ end
+
+ def diagnostic
+ buffer = ''
+ buffer += "NoCMS Diagnostics\n"
+ buffer += " Rails: #{rails?}\n"
+ if rails?
+ buffer += " Cache Store: #{ActionController::Base.cache_store}\n"
+ buffer += " Cache (Object): #{Rails.cache}\n"
+ end
+
+ buffer += " Cache: #{cache.class}\n"
+ buffer += " Environment: #{environment}\n"
+ buffer += " Site Key: #{site}\n"
+ buffer += " Default Language: #{lang_default}\n"
+ buffer
+ end
+
+ def build_key(path, xpath)
+ "NOCMS:#{path}:#{xpath}"
+ end
+
+ def cache()
+ rails? ? Rails.cache : @@nocms_cache
+ end
+
+ def ensure_cache()
+ tmp = cache.read("NOCMS:#{site}:updated_at")
+ updated_at = Marshal.load(tmp) if !tmp.nil?
+ load_cache if updated_at.nil? || updated_at < Time.now - 5*60
+ end
+
+ def load_cache
+ start_time = Time.now
+ raise '[NOCMS] Must specify a site' if NoCMS::Base.site.nil?
+
+ cache.write("NOCMS:#{site}:updated_at", Marshal.dump(Time.now))
+
+ # save this for a bit
+ tmp = cache.read("NOCMS:#{site}:keys")
+ old_keys = !tmp.nil? ? ActiveSupport::JSON.decode(tmp) : {}
+
+ # load the new stuff
+ rq = "http://api.nocms.org/nocms/1.0/export?site=#{site}"
+ json = Net::HTTP.get(URI.parse(rq))
+ new_blocks = ActiveSupport::JSON.decode(json)
+ #Rails.cache["NOCMS:#{site}:json"] = json
+ cache.write("NOCMS:#{site}:keys", new_blocks.collect {|block| build_key(block['block']['path'], block['block']['xpath'])} )
+ # add new keys and update existing
+ new_blocks.each do |block|
+ p = block['block']
+ #@@nocms_pages["#{p['path']}:#{p['xpath']}"] = p['content']
+ key = build_key(p['path'], p['xpath'])
+ cache.write(key, p['content'])
+ old_keys.delete(key)
+ end
+
+ # purge keys that have been deleted
+ old_keys.each {|key| cache.delete(key)}
+
+ updated_count = new_blocks.length
+ deleted_count = old_keys.length
+
+ puts "Loaded #{updated_count} blocks and deleted #{deleted_count} in #{Time.now-start_time}"
+ end
+
+ def can_edit?()
+ environment != :production
+ end
+
+ end
+
+ end
+
end
+#module ActionView
+# class Base
+
+# def nocms_block2(id, options = {})
+# "NOCMS WAS HERE!"
+# end
+# end
+#end
+
+
+
def nocms_block(id, options = {})
raise 'Must specify an element id as the first parameter. :tag and :content are optional params' if id.nil?
-
+
+ #request = ActionController::Base.request ||= Sinatra::Base.request
+
tag = options[:tag] ||= 'div'
- site = options[:site] ||= settings.nocms_site ||= request.host_with_port
- path = options[:path] ||= request.path_info
+ site = options[:site] ||= NoCMS::Base.site # ||= request.host_with_port
+ path = options[:path] ||= request.path ||= request.path_info
lang = options[:lang] ||= request.env['HTTP_ACCEPT_LANGUAGE'].nil? ? nil : request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first ||= 'en'
+ lang_default = NoCMS::Base.lang_default
- if @@nocms_pages.nil? || @@nocms_pages_updated_at < Time.now - 3*60
- @@nocms_pages_updated_at = Time.now
- rq = "http://api.nocms.org/nocms/1.0/export?site=#{site}"
- json = Net::HTTP.get(URI.parse(rq))
- results = ActiveSupport::JSON.decode(json)
- @@nocms_pages = Hash.new
- results.each do |block|
- p = block['block']
- @@nocms_pages["#{p['path']}:#{p['xpath']}"] = p['content']
- end
- puts "Loaded #{results.length} blocks"
- end
+ NoCMS::Base.ensure_cache()
- content = @@nocms_pages["/#{lang}#{path}:#{id}"] ||= @@nocms_pages["/#{@@nocms_lang_default}:#{path}:#{id}"] ||= options[:default] ||= 'Lorem ipsum'
+ ## TODO: exception throw when these are consolidated????
+ content = NoCMS::Base.cache.read(NoCMS::Base.build_key("/#{lang}#{path}", id))
+ content = content ||= NoCMS::Base.cache.read(NoCMS::Base.build_key("/#{lang_default}#{path}", id)) if lang != lang_default
+ content = content ||= options[:default] ||= 'Lorem ipsum'
- # "%#{tag}\##{id}.edit #{content}"
"<#{tag} id='#{id}' class='nocms_edit'>#{content}</#{tag}>"
end
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Darren Rush"]
- s.date = %q{2010-08-18}
+ s.date = %q{2010-08-20}
s.description = %q{A Ruby client for the NOCMS.org web service API. Provides server-side helpers for Rails/Sinatra/Rack applications. keywords: ruby, rails, sinatra, rack, CMS, content management systems, search, cloud, SAAS, JSON, web service}
s.email = %q{nocms@powcloud.com}
s.extra_rdoc_files = [
@@ -43,14 +43,14 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
else
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ s.add_dependency(%q<shoulda>, [">= 0"])
s.add_dependency(%q<activesupport>, [">= 0"])
end
else
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ s.add_dependency(%q<shoulda>, [">= 0"])
s.add_dependency(%q<activesupport>, [">= 0"])
end
end
View
@@ -1,7 +1,13 @@
require 'helper'
-class TestNocms < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+class NocmsTests < Test::Unit::TestCase
+ NoCMS::Base.site = 'powcloud.com:not-so-secret'
+
+ should "run diagnostics" do
+ puts NoCMS::Base.diagnostic
+ end
+
+ should "load cache" do
+ NoCMS::Base.ensure_cache()
end
end

0 comments on commit d0f61cc

Please sign in to comment.