Permalink
Browse files

added mirrored gem that i've created to wrap the delicious and magnol…

…ia mirrored apis

git-svn-id: http://svn.addictedtonew.com/public/gems/mirrored@77 fe7eae16-9a24-0410-a59d-9e59979e88be
  • Loading branch information...
0 parents commit 7eb8c9e034111c4a677258b8369c7fc6caf3e235 jnunemaker committed Oct 6, 2007
Showing with 7,982 additions and 0 deletions.
  1. +2 −0 History.txt
  2. +20 −0 License.txt
  3. +27 −0 Manifest.txt
  4. +1 −0 README.txt
  5. +4 −0 Rakefile
  6. +71 −0 config/hoe.rb
  7. +17 −0 config/requirements.rb
  8. +25 −0 lib/mirrored.rb
  9. +44 −0 lib/mirrored/base.rb
  10. +39 −0 lib/mirrored/connection.rb
  11. +28 −0 lib/mirrored/date.rb
  12. +122 −0 lib/mirrored/post.rb
  13. +34 −0 lib/mirrored/tag.rb
  14. +16 −0 lib/mirrored/update.rb
  15. +9 −0 lib/mirrored/version.rb
  16. +14 −0 script/destroy
  17. +14 −0 script/generate
  18. +74 −0 script/txt2html
  19. +1,585 −0 setup.rb
  20. +27 −0 tasks/deployment.rake
  21. +7 −0 tasks/environment.rake
  22. +17 −0 tasks/website.rake
  23. +2 −0 test/fixtures/xml/add_post_done.xml
  24. +2,919 −0 test/fixtures/xml/all_posts.xml
  25. +30 −0 test/fixtures/xml/all_posts_by_tag.xml
  26. +739 −0 test/fixtures/xml/dates.xml
  27. +270 −0 test/fixtures/xml/dates_for_tag.xml
  28. +2 −0 test/fixtures/xml/delete_post.xml
  29. +2 −0 test/fixtures/xml/delete_post_failed.xml
  30. +6 −0 test/fixtures/xml/posts.xml
  31. +4 −0 test/fixtures/xml/posts_by_date.xml
  32. +4 −0 test/fixtures/xml/posts_by_tag.xml
  33. +4 −0 test/fixtures/xml/posts_by_url.xml
  34. +19 −0 test/fixtures/xml/recent_posts.xml
  35. +19 −0 test/fixtures/xml/recent_posts_by_tag.xml
  36. +8 −0 test/fixtures/xml/recent_posts_by_tag_and_count.xml
  37. +2 −0 test/fixtures/xml/tag_rename.xml
  38. +1,368 −0 test/fixtures/xml/tags.xml
  39. +33 −0 test/test_base.rb
  40. +26 −0 test/test_date.rb
  41. +12 −0 test/test_helper.rb
  42. +136 −0 test/test_post.rb
  43. +23 −0 test/test_tag.rb
  44. +18 −0 test/test_update.rb
  45. +47 −0 website/css/common.css
  46. +92 −0 website/index.html
@@ -0,0 +1,2 @@
+0.1.0 - Oct 6, 2007
+ * initial release
@@ -0,0 +1,20 @@
+Copyright (c) 2007 John Nunemaker
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
+History.txt
+License.txt
+Manifest.txt
+README.txt
+Rakefile
+config/hoe.rb
+config/requirements.rb
+lib/mirrored.rb
+lib/mirrored/base.rb
+lib/mirrored/connection.rb
+lib/mirrored/date.rb
+lib/mirrored/post.rb
+lib/mirrored/tag.rb
+lib/mirrored/update.rb
+lib/mirrored/version.rb
+script/destroy
+script/generate
+script/txt2html
+setup.rb
+tasks/deployment.rake
+tasks/environment.rake
+tasks/website.rake
+test/test_helper.rb
+test/test_mirrored.rb
+website/css/common.css
+website/images
+website/index.html
@@ -0,0 +1 @@
+README
@@ -0,0 +1,4 @@
+require 'config/requirements'
+require 'config/hoe' # setup Hoe + all gem configuration
+
+Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,71 @@
+require 'mirrored/version'
+
+AUTHOR = 'John Nunemaker' # can also be an array of Authors
+EMAIL = "nunemaker@gmail.com"
+DESCRIPTION = "Wrapper for delicious and magnolia mirrored apis"
+GEM_NAME = 'mirrored' # what ppl will type to install your gem
+RUBYFORGE_PROJECT = 'mirrored' # The unix name for your project
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
+
+@config_file = "~/.rubyforge/user-config.yml"
+@config = nil
+RUBYFORGE_USERNAME = "jnunemaker"
+
+def rubyforge_username
+ unless @config
+ begin
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
+ rescue
+ puts <<-EOS
+ERROR: No rubyforge config file found: #{@config_file}
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
+ EOS
+ exit
+ end
+ end
+ RUBYFORGE_USERNAME.replace @config["username"]
+end
+
+
+REV = nil
+# UNCOMMENT IF REQUIRED:
+# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
+VERS = Mirrored::VERSION::STRING + (REV ? ".#{REV}" : "")
+RDOC_OPTS = ['--quiet', '--title', 'mirrored documentation',
+ "--opname", "index.html",
+ "--line-numbers",
+ "--main", "README",
+ "--inline-source"]
+
+class Hoe
+ def extra_deps
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
+ @extra_deps
+ end
+end
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+hoe = Hoe.new(GEM_NAME, VERS) do |p|
+ p.author = AUTHOR
+ p.description = DESCRIPTION
+ p.email = EMAIL
+ p.summary = DESCRIPTION
+ p.url = HOMEPATH
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
+ p.test_globs = ["test/**/test_*.rb"]
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
+
+ # == Optional
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\\n\\n")
+ p.extra_deps = [['hpricot', '0.5']] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
+
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
+
+end
+
+CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
+hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
@@ -0,0 +1,17 @@
+require 'fileutils'
+include FileUtils
+
+require 'rubygems'
+%w[rake hoe newgem rubigen].each do |req_gem|
+ begin
+ require req_gem
+ rescue LoadError
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
+ puts "Installation: gem install #{req_gem} -y"
+ exit
+ end
+end
+
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
+
+require 'mirrored'
@@ -0,0 +1,25 @@
+$:.unshift File.dirname(__FILE__)
+
+class Array
+ # Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash.
+ #
+ # def options(*args)
+ # args.extract_options!
+ # end
+ #
+ # options(1, 2) # => {}
+ # options(1, 2, :a => :b) # => {:a=>:b}
+ def extract_options!
+ last.is_a?(::Hash) ? pop : {}
+ end
+end
+
+require 'rubygems'
+require 'hpricot'
+
+require 'mirrored/base'
+require 'mirrored/connection'
+require 'mirrored/date'
+require 'mirrored/post'
+require 'mirrored/tag'
+require 'mirrored/update'
@@ -0,0 +1,44 @@
+module Mirrored
+
+ class InvalidService < Exception; end
+ class ConnectionNotSet < Exception; end
+
+ API_URL = {:delicious => 'https://api.del.icio.us/v1', :magnolia => 'https://ma.gnolia.com/api/mirrord/v1'}
+
+ class Base #:nodoc:
+ class << self
+ def establish_connection(s, u, p)
+ remove_connection
+ raise InvalidService unless valid_service?(s)
+ @@service = s
+ @@connection = Connection.new(api_url, :username => u, :password => p)
+ end
+
+ def remove_connection
+ @@service, @@connection = nil, nil
+ end
+
+ def valid_service?(s)
+ s = s.nil? ? '' : s
+ API_URL.keys.include?(s)
+ end
+
+ def api_url
+ API_URL[@@service]
+ end
+
+ def service
+ @@service
+ end
+
+ def connection
+ @@connection
+ end
+ end
+
+ private
+ def ensure_connection_set
+ raise ConnectionNotSet if self.class.connection.nil? || self.class.service.nil?
+ end
+ end
+end
@@ -0,0 +1,39 @@
+require 'net/https'
+
+module Mirrored
+ class Connection
+ def initialize(base_url, args = {})
+ @base_url = base_url
+ @username = args[:username]
+ @password = args[:password]
+ end
+
+ def get(resource, args = nil)
+ request(resource, "get", args)
+ end
+
+ def post(resource, args = nil)
+ request(resource, "post", args)
+ end
+
+ def request(resource, method = "get", args = nil)
+ url = URI.join(@base_url, resource)
+ url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&") if args
+
+ case method
+ when "get"
+ req = Net::HTTP::Get.new(url.request_uri)
+ when "post"
+ req = Net::HTTP::Post.new(url.request_uri)
+ end
+
+ req.basic_auth(@username, @password) if @username && @password
+
+ http = Net::HTTP.new(url.host, url.port)
+ http.use_ssl = (url.port == 443)
+
+ res = http.start() { |conn| conn.request(req) }
+ res.body
+ end
+ end
+end
@@ -0,0 +1,28 @@
+module Mirrored
+ class Date < Base
+ attr_accessor :count, :date
+
+ class << self
+ def new_from_xml(xml) #:nodoc:
+ d = Date.new
+ d.date = ::Date.parse((xml)['date'])
+ d.count = (xml)['count']
+ d
+ end
+
+ # Does all the hard work finding the dates you have posted and how many posts on those days.
+ #
+ # Usage:
+ # Mirrored::Date.find(:all) # => finds all dates you have posted with counts for each day
+ # Mirrored::Date.find(:all, :tag => 'ruby') # => finds all dates you have posted something tagged ruby with counts for each day
+ def find(*args)
+ raise ArgumentError, "First argument must be symbol (:all)" unless args.first.kind_of?(Symbol)
+ params = args.extract_options!
+ params = params == {} ? nil : params
+
+ doc = Hpricot::XML(connection.get("posts/dates", params))
+ (doc/:date).inject([]) { |elements, el| elements << new_from_xml(el); elements }
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 7eb8c9e

Please sign in to comment.