namelessjon-wiki_lib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jonathan Stott
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,25 @@
1
+ wiki-lib
2
+ ========
3
+
4
+ A simple library for easing interaction with wikis (currently just PMWiki) in ruby,
5
+ in essence, a wrapper around the mechanize gem.
6
+
7
+ require 'rubygems'
8
+ gem 'namelessjon-wiki_lib'
9
+ require 'wiki_lib'
10
+
11
+ @pm_wiki = WikiLib::PMWiki.new('http://www.pmwiki.org/wiki', 'namelessjon', 'apassword')
12
+
13
+ # get the page
14
+ @page = @pm_wiki.get_page('PmWiki/FAQ')
15
+
16
+ # get the current sandbox text
17
+ @sandbox = @pm_wiki.get_edit_text('Main/WikiSandbox')
18
+
19
+ # upload the reversed sandbox text back to the sandbox
20
+ @pm_wiki.upload_page('Main/WikiSandbox', @sandbox.reverse, "You're in backwards land, today".reverse)
21
+
22
+ COPYRIGHT
23
+ =========
24
+
25
+ Copyright (c) 2008 Jonathan Stott. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rcov/rcovtask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |s|
9
+ s.name = "wiki_lib"
10
+ s.summary = "A simple library to wrap around some common wiki operations"
11
+ s.email = "jonathan.stott@gmail.com"
12
+ s.homepage = "http://github.com/namelessjon/wiki_lib"
13
+ s.description = "A simple library to wrap around some common wiki operations\nCurrently only PMWiki is supported, but more will be added as and when."
14
+ s.authors = ["Jonathan Stott"]
15
+ s.add_dependency 'mechanize', '~>0.8'
16
+ s.autorequire = 'wiki_lib'
17
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install namelessjon-jeweler -s http://gems.github.com"
21
+ end
22
+
23
+ Rake::TestTask.new do |t|
24
+ t.libs << 'lib'
25
+ t.pattern = 'spec/**/*_spec.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ Rake::RDocTask.new do |rdoc|
30
+ rdoc.rdoc_dir = 'rdoc'
31
+ rdoc.title = 'wiki-lib'
32
+ rdoc.options << '--line-numbers' << '--inline-source'
33
+ rdoc.rdoc_files.include('README*')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
36
+
37
+ Rcov::RcovTask.new do |t|
38
+ t.libs << "spec"
39
+ t.test_files = FileList['spec/**/*_spec.rb']
40
+ t.verbose = true
41
+ end
42
+
43
+ task :default => :rcov
data/lib/wiki_lib.rb ADDED
@@ -0,0 +1,5 @@
1
+ module WikiLib
2
+ end
3
+ gem('mechanize', '~>0.8')
4
+ require 'mechanize'
5
+ require 'wiki_lib/pm_wiki'
@@ -0,0 +1,118 @@
1
+ module WikiLib
2
+ ##
3
+ # Class for interacting with the PMWiki
4
+ class PMWiki
5
+ ##
6
+ # Initializes a new PMWiki instance
7
+ #
8
+ # @param [String] base_page The base page of the wiki. Probably it's address, but could be a subset of the namespace
9
+ #
10
+ # @param [String] username Username used to sign off the edits
11
+ #
12
+ # @param [String] password Password used to make edits
13
+ def initialize(base_page, username, password)
14
+ @base = base_page
15
+ @pass = password
16
+ @user = username
17
+ @agent = ::WWW::Mechanize.new
18
+ end
19
+
20
+ ##
21
+ # returns a PMWiki page url
22
+ #
23
+ # @param [String] page The name of the page, relative to the base page
24
+ #
25
+ # @return [String] The URL
26
+ def page_url(page)
27
+ "#{@base}/#{page}"
28
+ end
29
+
30
+ ##
31
+ # returns a PMWiki page edit url
32
+ #
33
+ # @param [String] page The name of the page, relative to the base page
34
+ #
35
+ # @return [String] The URL
36
+ def edit_url(page)
37
+ "#{page_url(page)}?action=edit"
38
+ end
39
+
40
+ ##
41
+ # Returns the edit text of a PMWiki page
42
+ #
43
+ # @param [String] page The page to get the edit text for
44
+ #
45
+ # @return [String] The edit text
46
+ def get_edit_text(page)
47
+ # get the form, logging in if nessessary
48
+ form = get_edit_form_with_login(edit_url(page))
49
+
50
+ # return the text
51
+ form['text']
52
+ end
53
+
54
+ ##
55
+ # Returns the full page, parsed by hpricot
56
+ #
57
+ # @param [String] page Page to retrieve the raw text for
58
+ #
59
+ # @return [Hpricot::Doc] The parsed page
60
+ def get_page(page)
61
+ @agent.get(page_url(page))
62
+ end
63
+
64
+ ##
65
+ # Uploads a page to the PM wiki, creating it if needed
66
+ #
67
+ # @param [String] page Page to create/update
68
+ #
69
+ # @param [String] text Text to place on the new page
70
+ #
71
+ # @param [String] summary Edit summary for the edit
72
+ #
73
+ # @param [TrueClass, FalseClass] minor tick the minor edit checkbox?
74
+ def upload_page(page, text, summary, minor=false)
75
+ # get the form, logging in if nessessary
76
+ form = get_edit_form_with_login(edit_url(page))
77
+
78
+ # fill out our form
79
+ form['text'] = text
80
+ form['csum'] = summary
81
+ form['author'] = @user
82
+
83
+ edit.checkboxes.first.check if minor
84
+
85
+ # upload
86
+ @agent.submit(form, form.buttons.first)
87
+ end
88
+
89
+ ##
90
+ # Deletes a PMWiki page (sets text to delete)
91
+ #
92
+ # @param [String] page The page to delete
93
+ #
94
+ # @param [String] summary The reason for deletion
95
+ def delete_page(page, summary)
96
+ upload_page(page, 'delete', summary)
97
+ end
98
+
99
+ protected
100
+ def get_edit_form_with_login(url)
101
+ # fetch the page
102
+ page = @agent.get(url)
103
+
104
+ # if we find an authform, we are not logged in.
105
+ if form = page.forms.find { |f| f.name == 'authform' }
106
+ # fill in the password and submit
107
+ form['authpw'] = @pass
108
+ page = @agent.submit(form, form.buttons.first)
109
+
110
+ # if we still have an auth form, we're in trouble
111
+ raise(StandardError, "Authentication Failed") if page.forms.find { |f| f.name == 'authform' }
112
+ end
113
+
114
+ # by now, this should be the right page, so find the last form on it!
115
+ page.forms.last
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bacon'
3
+
4
+ # get a summary of errors raised and such
5
+ Bacon.summary_on_exit
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "WikiLib" do
4
+ it "fails" do
5
+ should.flunk "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namelessjon-wiki_lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Stott
8
+ autorequire: wiki_lib
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-16 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mechanize
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: "0.8"
23
+ version:
24
+ description: A simple library to wrap around some common wiki operations Currently only PMWiki is supported, but more will be added as and when.
25
+ email: jonathan.stott@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - LICENSE
34
+ - README
35
+ - Rakefile
36
+ - lib/wiki_lib
37
+ - lib/wiki_lib/pm_wiki.rb
38
+ - lib/wiki_lib.rb
39
+ - spec/spec_helper.rb
40
+ - spec/wiki_lib_spec.rb
41
+ has_rdoc: false
42
+ homepage: http://github.com/namelessjon/wiki_lib
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: A simple library to wrap around some common wiki operations
67
+ test_files: []
68
+