Permalink
Browse files

- initial import of plugin

git-svn-id: http://wikipedia-client.googlecode.com/svn/trunk/wikipedia@2 0d0b5543-3947-0410-9bc2-57c48d3fcb10
  • Loading branch information...
0 parents commit c2d7d6bf9ba56d84edc0f3abcfe1cb53d2d00fca cyx.ucron committed Feb 28, 2008
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+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.
41 README
@@ -0,0 +1,41 @@
+== Wikipedia
+
+Allows you to get wikipedia content through their API. This uses the
+alpha API, not the deprecated query.php API type
+
+
+== Examples
+
+page = Wikipedia.find( 'Getting Things Done' )
+
+=> #<Wikipedia:Page>
+
+page.title
+
+=> 'Getting Things Done'
+
+page.content
+
+=> # all the wiki markup appears here...
+
+== Configuration
+
+This is by default configured like this
+
+Wikipedia::Configure {
+ domain 'en.wikipedia.org'
+ path 'w/api.php'
+ action 'query'
+ properties 'content'
+}
+
+
+== Running specs
+
+if you have rspec >= 1.1.3 installed just type in
+
+rake spec
+
+Thanks!
+
+Copyright (c) 2008 [Cyril David], released under the MIT license
@@ -0,0 +1,20 @@
+require 'rake'
+require 'rake/rdoctask'
+
+desc 'Default: run specifications.'
+task :default => :spec
+
+desc 'Test the wikipedia plugin.'
+task :spec do
+ spec_path = File.expand_path(File.dirname(__FILE__) + '/spec/**/*.rb')
+ system("spec -cfs #{spec_path}")
+end
+
+desc 'Generate documentation for the wikipedia plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Wikipedia'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + '/lib/wikipedia'
@@ -0,0 +1 @@
+puts File.read(File.dirname(__FILE__) + '/README')
@@ -0,0 +1,29 @@
+Dir[File.dirname(__FILE__) + '/wikipedia/**/*.rb'].each { |f| require f }
+
+module Wikipedia
+ # Examples :
+ # page = Wikipedia.find('Rails')
+ # => #<Wikipedia:0x123102>
+ # page.content
+ # => wiki content appears here
+
+ # basically just a wrapper for doing
+ # client = Wikipedia::Client.new
+ # client.find('Rails')
+ #
+ def self.find( page )
+ @client ||= Wikipedia::Client.new
+ @client.find( page )
+ end
+
+ def self.Configure(&block)
+ Configuration.instance.instance_eval(&block)
+ end
+
+ Configure {
+ domain 'en.wikipedia.org'
+ path 'w/api.php'
+ action 'query'
+ properties 'content'
+ }
+end
@@ -0,0 +1,44 @@
+module Wikipedia
+ class Client
+ @@url = "http://:domain/:path?action=:action&prop=revisions&titles=:page&rvprop=:properties&format=json"
+
+ attr_accessor :follow_redirects
+
+ def initialize
+ self.follow_redirects = true
+ end
+
+ def find( title )
+ title = Url.new(title).title rescue title
+ page = Page.new( request( title ) )
+ while follow_redirects and page.redirect?
+ page = Page.new( request( page.redirect_title ))
+ end
+ page
+ end
+
+ def request( page )
+ require 'open-uri'
+ URI.parse( url_for(page) ).read
+ end
+
+ protected
+ def url_keys_for( page )
+ {
+ :domain => Configuration[:domain],
+ :path => Configuration[:path],
+ :action => Configuration[:action],
+ :page => URI.encode(page),
+ :properties => [Configuration[:properties]].flatten.join('|')
+ }
+ end
+
+ def url_for( page )
+ ret = @@url.dup
+ url_keys_for( page ).each do |key, value|
+ ret.sub! ":#{key}", value
+ end
+ ret
+ end
+ end
+end
@@ -0,0 +1,25 @@
+require 'singleton'
+
+module Wikipedia
+ class Configuration
+ include Singleton
+
+ def self.directives(*directives)
+ directives.each do |directive|
+ define_method directive do |*args|
+ if args.empty?
+ return instance_variable_get("@#{directive}")
+ else
+ instance_variable_set("@#{directive}", args.first)
+ end
+ end
+ end
+ end
+
+ def self.[](directive)
+ instance.send(directive)
+ end
+
+ directives :domain, :path, :action, :properties
+ end
+end
@@ -0,0 +1,27 @@
+module Wikipedia
+ class Page
+ def initialize(json)
+ require 'json'
+ @data = JSON::load(json)
+ end
+
+ def content
+ @data['query']['pages'].values.first['revisions'].first.values.first
+ end
+
+ def redirect?
+ content.match(/\#REDIRECT\s+\[\[(.*?)\]\]/)
+ end
+
+ def redirect_title
+ if matches = redirect?
+ matches[1]
+ end
+ end
+
+ def title
+ @data['query']['pages'].values.first['title']
+ end
+ end
+
+end
@@ -0,0 +1,14 @@
+module Wikipedia
+ class Url
+ def initialize(wiki_url)
+ @wiki_url = wiki_url
+ end
+
+ def title
+ return @title if @title
+
+ uri = URI.parse( @wiki_url )
+ @title = uri.path.split('/').last
+ end
+ end
+end

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit c2d7d6b

Please sign in to comment.