DeliciousRuby 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-02-17
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,24 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/DeliciousRuby.rb
9
+ lib/DeliciousRuby/version.rb
10
+ log/debug.log
11
+ script/destroy
12
+ script/generate
13
+ script/txt2html
14
+ setup.rb
15
+ tasks/deployment.rake
16
+ tasks/environment.rake
17
+ tasks/website.rake
18
+ test/test_DeliciousRuby.rb
19
+ test/test_helper.rb
20
+ website/index.html
21
+ website/index.txt
22
+ website/javascripts/rounded_corners_lite.inc.js
23
+ website/stylesheets/screen.css
24
+ website/template.rhtml
@@ -0,0 +1 @@
1
+ README
@@ -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,70 @@
1
+ require 'DeliciousRuby/version'
2
+
3
+ AUTHOR = 'ichiroc' # can also be an array of Authors
4
+ EMAIL = "nobitahouse@gmail.com"
5
+ DESCRIPTION = "Delicious API wrapper for Ruby.You can use all posts medhods. And access data of Array."
6
+ GEM_NAME = 'DeliciousRuby' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'DeliciousRuby' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = DeliciousRuby::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'DeliciousRuby documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.developer(AUTHOR, EMAIL)
52
+ p.description = DESCRIPTION
53
+ p.summary = DESCRIPTION
54
+ p.url = HOMEPATH
55
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
56
+ p.test_globs = ["test/**/test_*.rb"]
57
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
58
+
59
+ # == Optional
60
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
61
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
62
+
63
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
64
+
65
+ end
66
+
67
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
68
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
69
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
70
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
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]))
16
+
17
+ require 'DeliciousRuby'
@@ -0,0 +1,190 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require'rexml/document'
4
+ require'net/https'
5
+ require'kconv'
6
+ require'uri'
7
+ require'cgi'
8
+
9
+ # Author:: Ichiro Nakatani (mailto:nobitahouse@gmail.com)
10
+ # Copyright:: Copyright (c) 2008 Ichiro Nakatani
11
+ # If you want know detail delicious API , see http://del.icio.us/help/api/posts.
12
+ class DeliciousRuby
13
+ Delicious_Post_API = 'https://api.del.icio.us/v1/posts/'
14
+ User_agent = 'DeliciousRuby'
15
+ API_URI = URI.parse(Delicious_Post_API)
16
+
17
+ #Delicious user name , and password.
18
+ def initialize(user ,password)
19
+ Net::HTTP.version_1_2
20
+ @user = user
21
+ @password = password
22
+ end
23
+
24
+ def update
25
+ use_api_of :update
26
+ end
27
+
28
+ # You can put some parameters by hash.
29
+ # see http://del.icio.us/help/api/posts
30
+ def all(tag ={ } ,headers={ })
31
+ tag = { :tag => tag } if tag.class.to_s == 'String'
32
+ use_api_of :all , tag ,headers
33
+ end
34
+
35
+ # You can put some parameters by hash.
36
+ # see http://del.icio.us/help/api/posts
37
+ def get(parameter={} ,headers = {})
38
+ use_api_of :get , parameter ,headers
39
+ end
40
+
41
+ # You can put parameters by hash.
42
+ # see http://del.icio.us/help/api/posts
43
+ def recent(parameter={},headers={})
44
+ use_api_of :recent , parameter ,headers
45
+ end
46
+
47
+ # You can put url , hash or string.
48
+ def delete(url , headers={})
49
+ url = { :url => url } if url.class.to_s == 'String'
50
+ use_api_of :delete , url , headers
51
+ end
52
+
53
+
54
+
55
+ def add(parameters,headers={ })
56
+ # dt format is "CCYY-MM-DDThh:mm:ssZ"
57
+ # for example: "1984-09-01T14:21:31Z"
58
+
59
+
60
+ raise 'Url and Description args are required.' if parameters[:url].nil? || parameters[:description].nil?
61
+ url = parameters[:url]
62
+ description = parameters[:description]
63
+ #change parameters.
64
+ replace = if parameters[:replace] then "yes" else "no" end
65
+ if parameters[:shared].nil?
66
+ shared = 'yes'
67
+ else
68
+ shared = if parameters[:shared] then "yes" else "no" end
69
+ end
70
+
71
+ extended = parameters[:extended].to_s
72
+ dt = convert_time(dt) unless parameters[:dt].class.to_s == 'Time'
73
+ tags = parameters[:tag]
74
+ tags = tags.join(' ') if tags.class.to_s == 'Array'
75
+
76
+
77
+ parameters = {
78
+ :url => url ,
79
+ :description => description ,
80
+ :extended => extended ,
81
+ :tags => tags,
82
+ :dt => dt.strftime("%Y-%m-%d%%H:%M:%SZ"),
83
+ :shared => shared,
84
+ :replace => replace
85
+ }
86
+ use_api_of :add , parameters
87
+
88
+ end
89
+
90
+ private
91
+
92
+ def escape_url(url)
93
+ # escape url. exclude '&'. cause CGI.escape doesn't escape '&'.
94
+ url.split('&').map{ |u| CGI.escape(u) }.join('%26')
95
+ end
96
+
97
+ def use_api_of(method , parameters={ } ,headers={ } )
98
+ make_delicious_http
99
+ make_header_of method, parameters ,headers
100
+ send_request
101
+ convert_array_from_response
102
+ end
103
+
104
+ def send_request( request = @request)
105
+ @http.start{ |http|
106
+ @response = http.request(request)
107
+ }
108
+ return @response
109
+ end
110
+
111
+ def convert_array_from_response(response = @response)
112
+ result = []
113
+ xml = REXML::Document.new(response.body)
114
+ case REXML::XPath.first(xml).name
115
+ when 'posts'
116
+ xml.elements.each('posts/post'){ |node|
117
+ node.attributes['tag'] = '' if node.attributes['tag'] == 'system:unfiled'
118
+ result << {
119
+ :url => node.attributes['href'].to_s,
120
+ :description => node.attributes['description'].to_s,
121
+ :extended => node.attributes['extended'].to_s ,
122
+ :tags => node.attributes['tag'].split(' ') ,
123
+ :time => convert_time(node.attributes['time'])
124
+ }
125
+ }
126
+ when 'result'
127
+ result = xml.root.attributes['code']
128
+ when 'dates'
129
+ xml.elements.each('dates/date'){ |node|
130
+ result << {
131
+ :date => node.attributes['date'] ,
132
+ :count => node.attributes['count']
133
+ }
134
+ }
135
+ when 'update'
136
+ convert_time(xml.root.attributes['time'])
137
+ #result = Time.local(year , month , day , hour , month , second)
138
+ end
139
+ return result
140
+ end
141
+
142
+ def convert_time(time)
143
+ return Time.new if time.nil?
144
+
145
+ year = time[0,4]
146
+ month = time[5,2]
147
+ day = time[8,2]
148
+ hour = time[11,2]
149
+ minite = time[14,2]
150
+ second = time[17,2]
151
+ result = Time.local(year , month , day , hour , month , second)
152
+ end
153
+
154
+ def make_delicious_http
155
+ # make http object for ssl.
156
+ @http = Net::HTTP.new(API_URI.host,API_URI.port)
157
+ @http.use_ssl = true
158
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
159
+ return @http
160
+ end
161
+
162
+
163
+ def make_header_of (method=:get , parameters = { } , headers = { })
164
+ # include paramters to "url".
165
+ param=[]
166
+ parameters.each{ | key , value |
167
+ if key == :url
168
+ param << 'url' + '=' + escape_url(value)
169
+ else
170
+ param << CGI.escape(key.to_s) + '=' + CGI.escape(value.to_s.toutf8)
171
+ end
172
+ }
173
+
174
+
175
+ url = API_URI.path + method.to_s
176
+ url += "?" + param.join('&') unless param.empty?
177
+ # authentication
178
+ @request = Net::HTTP::Get.new(url)
179
+ @request.basic_auth(@user , @password)
180
+
181
+ # set default ua , if ua is provieded.
182
+ headers['User-Agent'] = User_agent if headers['User-Agent'].nil?
183
+ headers.each{ |header,value|
184
+ @request.add_field( header , value)
185
+ }
186
+ return @request
187
+ end
188
+
189
+ end
190
+
@@ -0,0 +1,9 @@
1
+ class DeliciousRuby #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
File without changes
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = 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.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,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ begin
5
+ require 'newgem'
6
+ rescue LoadError
7
+ puts "\n\nGenerating the website requires the newgem RubyGem"
8
+ puts "Install: gem install newgem\n\n"
9
+ exit(1)
10
+ end
11
+ require 'redcloth'
12
+ require 'syntax/convertors/html'
13
+ require 'erb'
14
+ require File.dirname(__FILE__) + '/../lib/DeliciousRuby/version.rb'
15
+
16
+ version = DeliciousRuby::VERSION::STRING
17
+ download = 'http://rubyforge.org/projects/DeliciousRuby'
18
+
19
+ class Fixnum
20
+ def ordinal
21
+ # teens
22
+ return 'th' if (10..19).include?(self % 100)
23
+ # others
24
+ case self % 10
25
+ when 1: return 'st'
26
+ when 2: return 'nd'
27
+ when 3: return 'rd'
28
+ else return 'th'
29
+ end
30
+ end
31
+ end
32
+
33
+ class Time
34
+ def pretty
35
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
36
+ end
37
+ end
38
+
39
+ def convert_syntax(syntax, source)
40
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
41
+ end
42
+
43
+ if ARGV.length >= 1
44
+ src, template = ARGV
45
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.rhtml')
46
+
47
+ else
48
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
49
+ exit!
50
+ end
51
+
52
+ template = ERB.new(File.open(template).read)
53
+
54
+ title = nil
55
+ body = nil
56
+ File.open(src) do |fsrc|
57
+ title_text = fsrc.readline
58
+ body_text = fsrc.read
59
+ syntax_items = []
60
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
61
+ ident = syntax_items.length
62
+ element, syntax, source = $1, $2, $3
63
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
64
+ "syntax-temp-#{ident}"
65
+ }
66
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
67
+ body = RedCloth.new(body_text).to_html
68
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
69
+ end
70
+ stat = File.stat(src)
71
+ created = stat.ctime
72
+ modified = stat.mtime
73
+
74
+ $stdout << template.result(binding)