akitaonrails-mygist 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-08-19
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 FIXME full name
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.
@@ -0,0 +1,28 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README
6
+ README.txt
7
+ Rakefile
8
+ bin/mygist
9
+ config/hoe.rb
10
+ config/requirements.rb
11
+ lib/mygist.rb
12
+ lib/mygist/version.rb
13
+ script/console
14
+ script/destroy
15
+ script/generate
16
+ script/txt2html
17
+ setup.rb
18
+ tasks/deployment.rake
19
+ tasks/environment.rake
20
+ tasks/website.rake
21
+ test/mygist_test.rb
22
+ test/test_helper.rb
23
+ test/test_mygist.rb
24
+ website/index.html
25
+ website/index.txt
26
+ website/javascripts/rounded_corners_lite.inc.js
27
+ website/stylesheets/screen.css
28
+ website/template.html.erb
@@ -0,0 +1,7 @@
1
+
2
+ For more information on mygist, see http://mygist.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README ADDED
File without changes
@@ -0,0 +1,63 @@
1
+ = mygist
2
+
3
+ * http://www.akitaonrails.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ Encapsulates the basic Gist operations in a simple Library that uses WWW::Mechanize
8
+ while there is still no available public API
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Login
13
+ * Logout
14
+ * Create a new Gist
15
+ * Edit an existing Gist
16
+ * Delete a Gist
17
+ * List all Gists or just the logged in user gists
18
+
19
+ == SYNOPSIS:
20
+
21
+ begin
22
+ gist = Mygist::GistApi.new('username', 'password')
23
+ gist.login!
24
+ id = gist.create("Hello World", ".txt")
25
+ gist.update!(id, "Hello World Again!")
26
+ gist.delete!(id)
27
+ gist.logout!
28
+ rescue Mygist::GistApi::NotLoggedIn
29
+ # if the login fails
30
+ end
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * gem install mechanize
35
+
36
+ == INSTALL:
37
+
38
+ * gem install akitaonrails-mygist --source=http://gems.github.com
39
+
40
+ == LICENSE:
41
+
42
+ (The MIT License)
43
+
44
+ Copyright (c) 2008 Fabio Akita
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining
47
+ a copy of this software and associated documentation files (the
48
+ 'Software'), to deal in the Software without restriction, including
49
+ without limitation the rights to use, copy, modify, merge, publish,
50
+ distribute, sublicense, and/or sell copies of the Software, and to
51
+ permit persons to whom the Software is furnished to do so, subject to
52
+ the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be
55
+ included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
58
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
60
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
61
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
62
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
63
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby -wKU
2
+ require 'rubygems'
3
+ require 'mygist'
@@ -0,0 +1,73 @@
1
+ require 'mygist/version'
2
+
3
+ AUTHOR = 'Fabio Akita' # can also be an array of Authors
4
+ EMAIL = "fabioakita@gmail.com"
5
+ DESCRIPTION = "encapsulates Gist operations in a library"
6
+ GEM_NAME = 'mygist' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'mygist' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+ EXTRA_DEPENDENCIES = [
11
+ # ['activesupport', '>= 1.3.1']
12
+ ] # An array of rubygem dependencies [name, version]
13
+
14
+ @config_file = "~/.rubyforge/user-config.yml"
15
+ @config = nil
16
+ RUBYFORGE_USERNAME = "unknown"
17
+ def rubyforge_username
18
+ unless @config
19
+ begin
20
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
21
+ rescue
22
+ puts <<-EOS
23
+ ERROR: No rubyforge config file found: #{@config_file}
24
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
25
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
26
+ EOS
27
+ exit
28
+ end
29
+ end
30
+ RUBYFORGE_USERNAME.replace @config["username"]
31
+ end
32
+
33
+
34
+ REV = nil
35
+ # UNCOMMENT IF REQUIRED:
36
+ # REV = YAML.load(`svn info`)['Revision']
37
+ VERS = Mygist::VERSION::STRING + (REV ? ".#{REV}" : "")
38
+ RDOC_OPTS = ['--quiet', '--title', 'mygist documentation',
39
+ "--opname", "index.html",
40
+ "--line-numbers",
41
+ "--main", "README",
42
+ "--inline-source"]
43
+
44
+ class Hoe
45
+ def extra_deps
46
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
47
+ @extra_deps
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.developer(AUTHOR, EMAIL)
55
+ p.description = DESCRIPTION
56
+ p.summary = DESCRIPTION
57
+ p.url = HOMEPATH
58
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
59
+ p.test_globs = ["test/**/test_*.rb"]
60
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
61
+
62
+ # == Optional
63
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
64
+ #p.extra_deps = EXTRA_DEPENDENCIES
65
+
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ $hoe.rsync_args = '-av --delete --ignore-errors'
73
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,105 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ module Mygist
7
+ class GistApi
8
+ class NotLoggedIn < StandardError; end
9
+
10
+ def initialize(username = nil, password = nil)
11
+ @username = username
12
+ @password = password
13
+ @login_page = "https://gist.github.com/login?return_to=gist"
14
+ @logout_page = "http://gist.github.com/logout"
15
+ @session_page = "https://gist.github.com/session"
16
+ @post_page = "http://gist.github.com"
17
+ @list_page = "http://gist.github.com/gists"
18
+ @mine_page = "http://gist.github.com/mine"
19
+ @agent = WWW::Mechanize.new
20
+ @is_logged = false
21
+ end
22
+
23
+ def config!(config_file = "~/.gist")
24
+ config_file = File.expand_path(config_file) if config_file =~ /~/
25
+ cfg = YAML::load(File.read(config_file))
26
+ @username = cfg["username"]
27
+ @password = cfg["password"]
28
+ @login_page ||= cfg["login_page"]
29
+ @logout_page ||= cfg["logout_page"]
30
+ @session_page ||= cfg["session_page"]
31
+ @post_page ||= cfg["post_page"]
32
+ @list_page ||= cfg["list_page"]
33
+ @mine_page ||= cfg["mine_page"]
34
+ end
35
+
36
+ def login!
37
+ page = @agent.get(@login_page)
38
+ login_form = page.forms.first
39
+ login_form.login = @username
40
+ login_form.password = @password
41
+ @login_page = @agent.submit(login_form)
42
+ @is_logged = (@login_page.uri.to_s != @session_page)
43
+ end
44
+
45
+ def logout!
46
+ @agent.get(@logout_page)
47
+ nil
48
+ end
49
+
50
+ def create(snippet, file_name = nil, type = nil)
51
+ raise NotLoggedIn unless @is_logged
52
+ page = @agent.get(@post_page)
53
+ gist_form = page.forms.first
54
+ gist_form.field("file_ext[gistfile1]").value = type if type
55
+ gist_form.field("file_name[gistfile1]").value = file_name if file_name
56
+ gist_form.field("file_contents[gistfile1]").value = snippet if snippet
57
+ gist_page = @agent.submit(gist_form)
58
+ return $1 if gist_page.uri.to_s =~ /\/(\d+)$/
59
+ end
60
+
61
+ def list
62
+ page = @agent.get(@is_logged ? @mine_page : @list_page)
63
+ page.links.select { |l| l.text =~ /^gist/ }.map { |l| l.uri.to_s.gsub("\/", "") }
64
+ end
65
+
66
+ def show(gist_id, format = ".txt")
67
+ case format
68
+ when ".js"
69
+ "<script src='#{@post_page}/#{gist_id}.js'></script>"
70
+ else
71
+ page = @agent.get("#{@post_page}/#{gist_id}.txt")
72
+ page.content
73
+ end
74
+ end
75
+
76
+ def delete!(gist_id)
77
+ raise NotLoggedIn unless @is_logged
78
+ @agent.get("#{@post_page}/delete/#{gist_id}")
79
+ nil
80
+ end
81
+
82
+ def update!(gist_id, snippet, file_name = nil, type = nil)
83
+ page = @agent.get("#{@list_page}/#{gist_id}/edit")
84
+ update_form = page.forms.first
85
+
86
+ file_content_name = page.search("//textarea[@class='file-contents']").first.attributes['name']
87
+ file_extension_name = page.search("//div[@class='gist-lang']").first.search("//select").first.attributes['name']
88
+ file_name_name = page.search("//input[@class='gist-name-textbox']").first.attributes['name']
89
+
90
+ update_form.field(file_content_name).value = snippet
91
+
92
+ unless file_name.nil?
93
+ update_form.field(file_name_name).value = file_name
94
+ else
95
+ unless type.nil?
96
+ update_form.field(file_extension_name).value = type
97
+ update_form.field(file_name_name).value = ''
98
+ end
99
+ end
100
+
101
+ @agent.submit(update_form)
102
+ nil
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,9 @@
1
+ module Mygist
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/mygist.rb'}"
9
+ puts "Loading mygist gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ GEM_NAME = 'mygist' # what ppl will type to install your gem
4
+ RUBYFORGE_PROJECT = 'mygist'
5
+
6
+ require 'rubygems'
7
+ begin
8
+ require 'newgem'
9
+ require 'rubyforge'
10
+ rescue LoadError
11
+ puts "\n\nGenerating the website requires the newgem RubyGem"
12
+ puts "Install: gem install newgem\n\n"
13
+ exit(1)
14
+ end
15
+ require 'redcloth'
16
+ require 'syntax/convertors/html'
17
+ require 'erb'
18
+ require File.dirname(__FILE__) + "/../lib/#{GEM_NAME}/version.rb"
19
+
20
+ version = Mygist::VERSION::STRING
21
+ download = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
22
+
23
+ def rubyforge_project_id
24
+ RubyForge.new.autoconfig["group_ids"][RUBYFORGE_PROJECT]
25
+ end
26
+
27
+ class Fixnum
28
+ def ordinal
29
+ # teens
30
+ return 'th' if (10..19).include?(self % 100)
31
+ # others
32
+ case self % 10
33
+ when 1: return 'st'
34
+ when 2: return 'nd'
35
+ when 3: return 'rd'
36
+ else return 'th'
37
+ end
38
+ end
39
+ end
40
+
41
+ class Time
42
+ def pretty
43
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
44
+ end
45
+ end
46
+
47
+ def convert_syntax(syntax, source)
48
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
49
+ end
50
+
51
+ if ARGV.length >= 1
52
+ src, template = ARGV
53
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
54
+ else
55
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
56
+ exit!
57
+ end
58
+
59
+ template = ERB.new(File.open(template).read)
60
+
61
+ title = nil
62
+ body = nil
63
+ File.open(src) do |fsrc|
64
+ title_text = fsrc.readline
65
+ body_text_template = fsrc.read
66
+ body_text = ERB.new(body_text_template).result(binding)
67
+ syntax_items = []
68
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
69
+ ident = syntax_items.length
70
+ element, syntax, source = $1, $2, $3
71
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
72
+ "syntax-temp-#{ident}"
73
+ }
74
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
75
+ body = RedCloth.new(body_text).to_html
76
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
77
+ end
78
+ stat = File.stat(src)
79
+ created = stat.ctime
80
+ modified = stat.mtime
81
+
82
+ $stdout << template.result(binding)