rme2day 1.0.1 → 1.0.2

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.
data/History.txt CHANGED
@@ -1,3 +1,5 @@
1
+ == 1.0.2
2
+ * added missing lib/rme2day (oops)
1
3
  == 1.0.1
2
4
  * Started to manage source codes in the repository of rubyforge
3
5
 
data/Manifest.txt CHANGED
@@ -3,6 +3,14 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  lib/rme2day.rb
6
+ lib/rme2day/api.rb
7
+ lib/rme2day/comment.rb
8
+ lib/rme2day/person.rb
9
+ lib/rme2day/post.rb
10
+ lib/rme2day/record.rb
11
+ lib/rme2day/settings.rb
12
+ lib/rme2day/tag.rb
13
+ lib/rme2day/util.rb
6
14
  spec/api_spec.rb
7
15
  spec/person_spec.rb
8
16
  spec/post_spec.rb
@@ -0,0 +1,147 @@
1
+ require 'rubygems'
2
+ require 'cgi'
3
+ require 'digest/md5'
4
+ #require 'sha1'
5
+ require 'hpricot'
6
+ #require 'rest-open-uri'
7
+ require 'open-uri'
8
+ require 'builder'
9
+
10
+ module Rme2day
11
+ class API
12
+ DOMAIN = 'me2day.net'
13
+
14
+ @@config = nil
15
+ @@username = nil
16
+ @@password = nil
17
+
18
+ class << self
19
+ # set credential information to access private data
20
+ def setup(me2day_id, user_key, app_key, encoding = :utf8)
21
+ set_credential(me2day_id, user_key, app_key, encoding )
22
+ end
23
+
24
+ def set_credential(me2day_id, user_key, app_key, encoding = :utf8 )
25
+ @@config = {
26
+ 'me2day_id' => me2day_id,
27
+ 'user_key' => user_key,
28
+ 'app_key' => app_key,
29
+ 'encoding' => encoding
30
+ }
31
+ @@username = nil
32
+ @@password = nil
33
+ end
34
+
35
+ def config
36
+ raise "Call Rspringnote::set_credentail to set your id and keys " unless @@config
37
+ @@config
38
+ end
39
+
40
+ def username
41
+ @@username || @@username = set_username
42
+ end
43
+
44
+ def password
45
+ @@password || @@password = set_password
46
+ end
47
+
48
+ def set_username
49
+ @@username = CGI.escape(config['me2day_id'])
50
+ end
51
+
52
+ def set_password
53
+ @@password = CGI.escape(digestkey)
54
+ end
55
+
56
+ def digestkey
57
+ nonce = sprintf("%08x", rand(16 ** 8))
58
+ nonce + Digest::MD5.hexdigest(nonce + config['user_key'].to_s)
59
+ end
60
+
61
+
62
+
63
+ #
64
+ # me2day read/write methods
65
+ #
66
+ def get_latests(person_id)
67
+ do_open latests_post_url(person_id)
68
+ end
69
+
70
+ def get_comments(permalink)
71
+ do_open get_comments_url(permalink)
72
+ end
73
+
74
+ def get_person(person_id)
75
+ do_open get_person_url(person_id)
76
+ end
77
+
78
+ def get_friends(person_id, scope = 'all')
79
+ do_open get_friends_url(person_id, scope)
80
+ end
81
+
82
+ def get_settings(person_id)
83
+ do_open(get_settings_url , :auth_needed => true)
84
+ end
85
+
86
+ def create_post(body, tags, icon)
87
+ url = create_post_url + create_post_params(body, tags, icon)
88
+ do_open(url , :auth_needed => true)
89
+ end
90
+
91
+ def create_comment(permalink, body)
92
+ url = create_comment_url + create_comment_params(permalink, body)
93
+ do_open(url, :auth_needed => true)
94
+ end
95
+
96
+ # get/put real http message
97
+ def do_open(url, options = {:auth_needed => false})
98
+
99
+ auth_needed = options.delete :auth_needed
100
+ options = options.merge({:http_basic_authentication => [username, password]}) if auth_needed
101
+ options = options.merge({'me2_application_key'=> config['app_key']}) if auth_needed
102
+
103
+ open(url, options)
104
+ end
105
+
106
+ #
107
+ # url build methods
108
+ #
109
+
110
+ def latests_post_url(person_id)
111
+ "http://#{DOMAIN}/api/get_latests/#{person_id}.xml"
112
+ end
113
+
114
+ def get_comments_url(permalink)
115
+ "http://#{DOMAIN}/api/get_comments.xml?post_id=#{URI.encode(permalink)}"
116
+ end
117
+
118
+ def get_person_url(person_id)
119
+ "http://#{DOMAIN}/api/get_person/#{person_id}.xml"
120
+ end
121
+
122
+ def get_friends_url(person_id, scope)
123
+ "http://#{DOMAIN}/api/get_friends/#{person_id}.xml?scope=#{scope}"
124
+ end
125
+
126
+ def get_settings_url
127
+ "http://#{DOMAIN}/api/get_settings.xml"
128
+ end
129
+
130
+ def create_post_url
131
+ "http://#{DOMAIN}/api/create_post/#{username}.xml"
132
+ end
133
+
134
+ def create_post_params(body, tags, icon)
135
+ URI.encode("?post[body]=#{body}&post[tags]=#{tags}&post[icon]=#{icon}")
136
+ end
137
+
138
+ def create_comment_url
139
+ "http://#{DOMAIN}/api/create_comment.xml"
140
+ end
141
+
142
+ def create_comment_params(permalink, body)
143
+ URI.encode("?post_id=#{permalink}&body=#{body}")
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,16 @@
1
+ module Rme2day
2
+ class Comment < Record
3
+ # def self.parse(data)
4
+ # doc = Hpricot data
5
+ # doc.to_s
6
+ # end
7
+
8
+ def person
9
+ @person || @person = Person.parse(API.get_person(person_id))[0]
10
+ end
11
+
12
+ def to_s
13
+ "#{body} #{datetime.strftime "%Y-%m-%d %H:%M:%S"} #{person_id}: "
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ module Rme2day
2
+ class Person < Record
3
+ def self.get(person_id)
4
+ parse(API.get_person(person_id))[0]
5
+ end
6
+
7
+ def friends(scope = 'all')
8
+ @friends || reload_friends(scope)
9
+ end
10
+
11
+ def reload_friends(scope)
12
+ @friends = Person.parse(API.get_friends(person_id, scope))
13
+ end
14
+
15
+ def newest_post
16
+ recent_posts.first
17
+ end
18
+
19
+ def recent_posts
20
+ @recent_posts || reload_recent_posts
21
+ end
22
+
23
+ def reload_recent_posts
24
+ @recent_posts = Post.parse API.get_latests(person_id)
25
+ end
26
+
27
+ def settings
28
+ raise "Can't get other user's setting" if person_id != API.username
29
+ @settings || reload_settings
30
+ end
31
+
32
+ def reload_settings
33
+ @settings = Settings.parse(API.get_settings(person_id))[0]
34
+ end
35
+
36
+ alias reload reload_recent_posts
37
+
38
+ def to_s
39
+ person_id
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ module Rme2day
2
+ class Post < Record
3
+ attr_writer :body, :kind, :permalink, :icon
4
+ attr_reader :api
5
+
6
+ def self.find_latests(person_id)
7
+ parse API.get_latests(person_id)
8
+ end
9
+
10
+ def self.create(body, tags, icon)
11
+ parse(API.create_post(body, tags, icon))[0]
12
+ end
13
+
14
+ def add_comment(body)
15
+ Comment.parse API.create_comment(permalink, body)
16
+ end
17
+
18
+ def comments
19
+ @comments || reload_comments
20
+ end
21
+
22
+ def reload_comments
23
+ @comments = Comment.parse API.get_comments(permalink)
24
+ end
25
+
26
+ alias reload reload_comments
27
+
28
+ def person
29
+ @person || @person = Person.parse(API.get_person(person_id))[0]
30
+ end
31
+
32
+ def to_s
33
+ #"#{person_id}: #{body} {#{tags.map{|t|t.to_s}.join(',')}} #{datetime.strftime "%Y-%m-%d %H:%M:%S"}"
34
+ "#{person_id}: #{datetime.strftime "%Y-%m-%d %H:%M:%S"}" + "\n" +
35
+ " #{body} " + "\n"+
36
+ " {#{tags.map{|t|t.to_s}.join(',')}}"
37
+ end
38
+
39
+ def print
40
+ puts self
41
+ puts
42
+ puts comments.map {|c| ' - ' + c.to_s}.join("\r\n")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,62 @@
1
+ module Rme2day
2
+ class Record
3
+ attr_accessor :person_id, :datetime, :tags
4
+
5
+ def initialize(doc=nil)
6
+ @doc = doc
7
+ end
8
+
9
+ def self.parse(data)
10
+ doc = Hpricot data
11
+
12
+ # this part is implemented very generally,
13
+ # so that this method can be reused in the inherited class Comment
14
+ #
15
+ class_name = self.name.split('::')[1] # Post or Comment
16
+ elem_docs = doc.search "//#{class_name.downcase}" # doc.search '//post' or doc.search '//commnet'
17
+
18
+ elems = elem_docs.map do |elem_doc|
19
+ elem = self.new(elem_doc) # self.new will be Post.new or Comment.new
20
+
21
+ # set person_id
22
+ elem.person_id = elem_doc.at('id').inner_html if elem_doc.at('id')
23
+
24
+
25
+ # set datetime
26
+ # datetime will use 'updated' or 'pubdate'
27
+ # if both exist, pubdate will be used
28
+ timestr = elem_doc.at('updated').inner_html if elem_doc.at('updated')
29
+ timestr = elem_doc.at('pubdate').inner_html if elem_doc.at('pubdate')
30
+ elem.datetime = Time.gm( * timestr.split(/[-T:Z]/) ) if timestr
31
+
32
+
33
+ # set tags
34
+ tags_doc = elem_doc.at('tags')
35
+ elem.tags = tags_doc.search("//tag").map do |tag_doc|
36
+ if API.config['encoding']=='euckr'
37
+ Tag.new(tag_doc.at('name').inner_html.ncr_euckr, tag_doc.at('url').inner_html.ncr_euckr)
38
+ else
39
+ Tag.new(tag_doc.at('name').inner_html.ncr_utf8, tag_doc.at('url').inner_html.ncr_utf8)
40
+ end
41
+ end if tags_doc
42
+
43
+ elem
44
+ end
45
+ end
46
+
47
+
48
+
49
+ # reading of attributes like 'kind', 'body' are implemented through method_missing
50
+ # because there is no need for special parsing algorithm.
51
+ #
52
+ def method_missing(name, *args)
53
+ if @doc and v = @doc.at(name) then
54
+ str = v.inner_text
55
+ API.config['encoding']=='euckr' ? str.ncr_euckr : str.ncr_utf8
56
+ else
57
+ raise NoMethodError, "#{name} is not defined as method nor as xml element"
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ module Rme2day
2
+ class Settings < Record
3
+ def self.get(person_id)
4
+ parse(API.get_settings(person_id))[0]
5
+ end
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,13 @@
1
+ module Rme2day
2
+ class Tag
3
+ attr_accessor :name, :url
4
+ def initialize(name, url)
5
+ @name = name
6
+ @url = url
7
+ end
8
+
9
+ def to_s
10
+ @name
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'iconv'
2
+
3
+ class String
4
+ def ncr_utf8
5
+ copy = self.gsub(/&\#(\d+);/n) { [$1.to_i].pack("U*") }
6
+ copy.gsub(/&\#x([0-9a-f]+);/ni) { [$1.hex].pack("U*") }
7
+ end
8
+
9
+ def utf8_euckr
10
+ begin
11
+ Iconv.iconv('euckr', 'utf-8', self)[0]
12
+ rescue
13
+ self
14
+ end
15
+ end
16
+
17
+ def ncr_euckr
18
+ self.ncr_utf8.utf8_euckr
19
+ end
20
+ end
data/lib/rme2day.rb CHANGED
@@ -70,7 +70,7 @@
70
70
 
71
71
 
72
72
  module Rme2day
73
- VERSION = '1.0.1'
73
+ VERSION = '1.0.2'
74
74
  end
75
75
 
76
76
  $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: rme2day
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.1
7
- date: 2007-08-28 00:00:00 +09:00
6
+ version: 1.0.2
7
+ date: 2007-10-04 00:00:00 +09:00
8
8
  summary: Open API library for Me2day service (http://me2day.net)
9
9
  require_paths:
10
10
  - lib
@@ -34,6 +34,14 @@ files:
34
34
  - README.txt
35
35
  - Rakefile
36
36
  - lib/rme2day.rb
37
+ - lib/rme2day/api.rb
38
+ - lib/rme2day/comment.rb
39
+ - lib/rme2day/person.rb
40
+ - lib/rme2day/post.rb
41
+ - lib/rme2day/record.rb
42
+ - lib/rme2day/settings.rb
43
+ - lib/rme2day/tag.rb
44
+ - lib/rme2day/util.rb
37
45
  - spec/api_spec.rb
38
46
  - spec/person_spec.rb
39
47
  - spec/post_spec.rb