hatenadiary 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/ChangeLog +9 -0
  2. data/README +3 -4
  3. data/lib/hatenadiary.rb +35 -35
  4. data/test/test_hatenadiary.rb +29 -24
  5. metadata +34 -12
data/ChangeLog CHANGED
@@ -1,3 +1,12 @@
1
+ 2011-02-24 arikui <arikui.ruby@gmail.com>
2
+
3
+ * lib/hatenadiary.rb: use constant Mechanize instead of WWW::Mechanize because
4
+ it is going to be deprecated.
5
+
6
+ * test/test_hatenadiary.rb: add #test_default_mechanizer to touch a constant Mechanize.
7
+
8
+ * Rakefile: version up to 0.0.6
9
+
1
10
  2009-12-18 arikui <arikui.ruby@gmail.com>
2
11
 
3
12
  * lib/hatenadiary.rb: remove gem() version requirement.
data/README CHANGED
@@ -4,11 +4,10 @@ It is a library provides
4
4
  a client for Hatena Diary to post and delete blog entries.
5
5
 
6
6
  Developed on:
7
- - ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mswin32]
8
- - ruby 1.8.7 (2009-06-12 patchlevel 174) [i386-mswin32]
7
+ - ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-mswin32]
9
8
 
10
- - Gem mechanize 0.9.3
11
- - Gem nokogiri 1.3.3
9
+ - Gem mechanize 1.0.0
10
+ - Gem nokogiri 1.4.4.1
12
11
 
13
12
  This list means these environments are enable to make all of /test pass.
14
13
 
data/lib/hatenadiary.rb CHANGED
@@ -1,9 +1,9 @@
1
- #
1
+ #
2
2
  # Distributes under The modified BSD license.
3
- #
3
+ #
4
4
  # Copyright (c) 2009 arikui <http://d.hatena.ne.jp/arikui1911/>
5
5
  # All rights reserved.
6
- #
6
+ #
7
7
 
8
8
  require 'rubygems'
9
9
  require 'nokogiri'
@@ -18,67 +18,67 @@ module HatenaDiary
18
18
  end
19
19
  else
20
20
  require 'kconv'
21
-
21
+
22
22
  def encode_to_utf8(str)
23
23
  Kconv.toutf8(str)
24
24
  end
25
25
  end
26
-
26
+
27
27
  module_function :encode_to_utf8
28
28
  end
29
29
  end
30
30
 
31
31
 
32
32
  module HatenaDiary
33
- #
33
+ #
34
34
  # Allocates Client object and makes it login, execute a received block,
35
35
  # and then logout.
36
- #
36
+ #
37
37
  # :call-seq:
38
38
  # login(username, password, proxy = nil){|client| ... }
39
- #
39
+ #
40
40
  def login(*args, &block)
41
41
  Client.login(*args, &block)
42
42
  end
43
43
  module_function :login
44
-
44
+
45
45
  class LoginError < RuntimeError
46
46
  def set_account(username, password)
47
47
  @username = username
48
48
  @password = password
49
49
  self
50
50
  end
51
-
51
+
52
52
  attr_reader :username
53
53
  attr_reader :password
54
54
  end
55
-
55
+
56
56
  class Client
57
57
  def self.mechanizer
58
- @mechanizer ||= WWW::Mechanize
58
+ @mechanizer ||= Mechanize
59
59
  end
60
-
60
+
61
61
  def self.mechanizer=(klass)
62
62
  @mechanizer = klass
63
63
  end
64
-
64
+
65
65
  # Allocates Client object.
66
- #
66
+ #
67
67
  # If block given, login and execute a received block, and then logout ensurely.
68
- #
68
+ #
69
69
  # [username] Hatena ID
70
70
  # [password] Password for _username_
71
71
  # [proxy] Proxy configuration; [proxy_url, port_no] | nil
72
- #
72
+ #
73
73
  def self.login(username, password, proxy = nil, &block)
74
74
  client = new(username, password)
75
75
  client.set_proxy(*proxy) if proxy
76
76
  return client unless block_given?
77
77
  client.transaction(&block)
78
78
  end
79
-
79
+
80
80
  # Allocates Client object.
81
- #
81
+ #
82
82
  # [username] Hatena ID
83
83
  # [password] Password for _username_
84
84
  def initialize(username, password, agent = self.class.mechanizer.new)
@@ -87,12 +87,12 @@ module HatenaDiary
87
87
  @password = password
88
88
  @current_account = nil
89
89
  end
90
-
90
+
91
91
  # Configure proxy.
92
92
  def set_proxy(url, port)
93
93
  @agent.set_proxy(url, port)
94
94
  end
95
-
95
+
96
96
  # Login and execute a received block, and then logout ensurely.
97
97
  def transaction(username = nil, password = nil)
98
98
  raise LocalJumpError, "no block given" unless block_given?
@@ -103,16 +103,16 @@ module HatenaDiary
103
103
  logout
104
104
  end
105
105
  end
106
-
106
+
107
107
  # Returns a client itself was logined or not.
108
- #
108
+ #
109
109
  # -> true | false
110
110
  def login?
111
111
  @current_account ? true : false
112
112
  end
113
-
113
+
114
114
  # Does login.
115
- #
115
+ #
116
116
  # If _username_ or _password_ are invalid, raises HatenaDiary::LoginError .
117
117
  def login(username = nil, password = nil)
118
118
  username ||= @username
@@ -129,7 +129,7 @@ module HatenaDiary
129
129
  else raise Exception, '[BUG] must not happen (maybe cannot follow hatena spec)'
130
130
  end
131
131
  end
132
-
132
+
133
133
  # Does logout if already logined.
134
134
  def logout
135
135
  return unless login?
@@ -138,11 +138,11 @@ module HatenaDiary
138
138
  @current_account = nil
139
139
  account
140
140
  end
141
-
141
+
142
142
  # Posts an entry to Hatena diary service.
143
- #
143
+ #
144
144
  # Raises HatenaDiary::LoginError unless logined.
145
- #
145
+ #
146
146
  # options
147
147
  # [:trivial] check a checkbox of trivial updating.
148
148
  # [:group] assign hatena-group name. edit group diary.
@@ -160,21 +160,21 @@ module HatenaDiary
160
160
  form["trivial"] = "true" if options[:trivial]
161
161
  @agent.submit form, form.button_with(:name => 'edit')
162
162
  end
163
-
163
+
164
164
  # Deletes an entry from Hatena diary service.
165
- #
165
+ #
166
166
  # Raises HatenaDiary::LoginError unless logined.
167
- #
167
+ #
168
168
  # options
169
169
  # [:group] assign hatena-group name. edit group diary.
170
- #
170
+ #
171
171
  # Invalid options were ignored.
172
172
  def delete(yyyy, mm, dd, options = {})
173
173
  get_form(yyyy, mm, dd, options[:group]){|r| r.forms.last }.submit
174
174
  end
175
-
175
+
176
176
  private
177
-
177
+
178
178
  def get_form(yyyy, mm, dd, group = nil)
179
179
  raise LoginError, "not login yet" unless login?
180
180
  vals = [group ? "#{group}.g" : "d",
@@ -5,18 +5,23 @@ require 'hatenadiary'
5
5
 
6
6
 
7
7
  class TestHatenaDiaryAPI < Test::Unit::TestCase
8
+ def test_default_mechanizer
9
+ HatenaDiary::Client.mechanizer = nil
10
+ assert_equal Mechanize, HatenaDiary::Client.mechanizer
11
+ end
12
+
8
13
  def setup
9
14
  @username = 'USERNAME'
10
15
  @password = 'PASSWORD'
11
16
  @proxy_url = "PROXY_URL"
12
17
  @proxy_port = "PROXY_PORT"
13
18
  end
14
-
19
+
15
20
  def test_api_login
16
21
  HatenaDiary::Client.expects(:login)
17
22
  HatenaDiary.login
18
23
  end
19
-
24
+
20
25
  def test_login
21
26
  client = mock()
22
27
  HatenaDiary::Client.expects(:new).with(@username, @password).returns(client)
@@ -25,7 +30,7 @@ class TestHatenaDiaryAPI < Test::Unit::TestCase
25
30
  assert_equal 'block delegate check', str
26
31
  end
27
32
  end
28
-
33
+
29
34
  def test_login_with_proxy
30
35
  client = mock()
31
36
  HatenaDiary::Client.expects(:new).with(@username, @password).returns(client)
@@ -35,13 +40,13 @@ class TestHatenaDiaryAPI < Test::Unit::TestCase
35
40
  assert_equal 'block delegate check', str
36
41
  end
37
42
  end
38
-
43
+
39
44
  def test_login_without_block
40
45
  client = Object.new
41
46
  HatenaDiary::Client.expects(:new).with(@username, @password).returns(client)
42
47
  assert_equal client, HatenaDiary::Client.login(@username, @password)
43
48
  end
44
-
49
+
45
50
  def test_login_without_block_with_proxy
46
51
  client = mock()
47
52
  HatenaDiary::Client.expects(:new).with(@username, @password).returns(client)
@@ -58,18 +63,18 @@ class TestHatenaDiary < Test::Unit::TestCase
58
63
  @agent = mock()
59
64
  @client = HatenaDiary::Client.new(@username, @password, @agent)
60
65
  end
61
-
66
+
62
67
  def test_set_proxy
63
68
  proxy_url = 'PROXY_URL'
64
69
  proxy_port = 'PROXY_PORT'
65
70
  @agent.expects(:set_proxy).with(proxy_url, proxy_port)
66
71
  @client.set_proxy(proxy_url, proxy_port)
67
72
  end
68
-
73
+
69
74
  def test_logout_without_login
70
75
  @client.logout
71
76
  end
72
-
77
+
73
78
  def login_mocking(submit_response_page_title)
74
79
  login_page = mock()
75
80
  form = {}
@@ -81,11 +86,11 @@ class TestHatenaDiary < Test::Unit::TestCase
81
86
  response.expects(:title).returns(submit_response_page_title)
82
87
  form
83
88
  end
84
-
89
+
85
90
  def logout_mocking
86
91
  @agent.expects(:get).with("https://www.hatena.ne.jp/logout")
87
92
  end
88
-
93
+
89
94
  def test_login_and_logout
90
95
  # before login
91
96
  assert !@client.login?
@@ -101,7 +106,7 @@ class TestHatenaDiary < Test::Unit::TestCase
101
106
  @client.logout
102
107
  assert !@client.login?
103
108
  end
104
-
109
+
105
110
  def test_login_failure
106
111
  login_mocking "Login - Hatena"
107
112
  begin
@@ -113,7 +118,7 @@ class TestHatenaDiary < Test::Unit::TestCase
113
118
  flunk "login error must be raised."
114
119
  end
115
120
  end
116
-
121
+
117
122
  def test_login_if_hatena_changed
118
123
  login_mocking "*jumbled pagetitle*"
119
124
  begin
@@ -124,7 +129,7 @@ class TestHatenaDiary < Test::Unit::TestCase
124
129
  flunk "exception must be raised"
125
130
  end
126
131
  end
127
-
132
+
128
133
  def test_transaction
129
134
  assert !@client.login?
130
135
  login_mocking "Hatena"
@@ -135,7 +140,7 @@ class TestHatenaDiary < Test::Unit::TestCase
135
140
  end
136
141
  assert !@client.login?
137
142
  end
138
-
143
+
139
144
  def test_transaction_without_block
140
145
  assert !@client.login?
141
146
  assert_raises LocalJumpError do
@@ -143,7 +148,7 @@ class TestHatenaDiary < Test::Unit::TestCase
143
148
  end
144
149
  assert !@client.login?
145
150
  end
146
-
151
+
147
152
  def post_mocking(host, date_str)
148
153
  edit_page = mock()
149
154
  form = {}
@@ -156,7 +161,7 @@ class TestHatenaDiary < Test::Unit::TestCase
156
161
  @agent.expects(:submit).with(form, button)
157
162
  form
158
163
  end
159
-
164
+
160
165
  def test_post
161
166
  form = post_mocking("d", "12340506")
162
167
  @client.transaction do |client|
@@ -172,7 +177,7 @@ class TestHatenaDiary < Test::Unit::TestCase
172
177
  assert_equal expected, form
173
178
  assert !form["trivial"]
174
179
  end
175
-
180
+
176
181
  def test_post_trivial
177
182
  form = post_mocking("d", "20071108")
178
183
  @client.transaction do |client|
@@ -180,14 +185,14 @@ class TestHatenaDiary < Test::Unit::TestCase
180
185
  end
181
186
  assert_equal "true", form["trivial"]
182
187
  end
183
-
188
+
184
189
  def test_post_group
185
190
  post_mocking "hoge.g", "12340506"
186
191
  @client.transaction do |client|
187
192
  client.post 1234, 5, 6, 'TITLE', 'BODY', :group => 'hoge'
188
193
  end
189
194
  end
190
-
195
+
191
196
  def test_post_group_trivial
192
197
  form = post_mocking("hoge.g", "12340506")
193
198
  @client.transaction do |client|
@@ -195,13 +200,13 @@ class TestHatenaDiary < Test::Unit::TestCase
195
200
  end
196
201
  assert_equal "true", form["trivial"]
197
202
  end
198
-
203
+
199
204
  def test_post_without_login
200
205
  assert_raises HatenaDiary::LoginError do
201
206
  @client.post 1999, 5, 26, "TITLE", "BODY\n"
202
207
  end
203
208
  end
204
-
209
+
205
210
  def delete_mocking(host, date_str)
206
211
  edit_page = mock()
207
212
  form = {}
@@ -214,21 +219,21 @@ class TestHatenaDiary < Test::Unit::TestCase
214
219
  form.expects(:submit)
215
220
  form
216
221
  end
217
-
222
+
218
223
  def test_delete
219
224
  delete_mocking "d", "12340506"
220
225
  @client.transaction do |client|
221
226
  client.delete 1234, 5, 6
222
227
  end
223
228
  end
224
-
229
+
225
230
  def test_delete_group
226
231
  delete_mocking "piyo.g", "12340506"
227
232
  @client.transaction do |client|
228
233
  client.delete 1234, 5, 6, :group => 'piyo'
229
234
  end
230
235
  end
231
-
236
+
232
237
  def test_delete_without_login
233
238
  assert_raises HatenaDiary::LoginError do
234
239
  @client.delete 2009, 8, 30
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hatenadiary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
5
11
  platform: ruby
6
12
  authors:
7
13
  - arikui
@@ -14,24 +20,34 @@ default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: mechanize
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
23
32
  version: "0"
24
- version:
33
+ type: :runtime
34
+ version_requirements: *id001
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: nokogiri
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
30
40
  requirements:
31
41
  - - ">="
32
42
  - !ruby/object:Gem::Version
43
+ hash: 29
44
+ segments:
45
+ - 1
46
+ - 3
47
+ - 3
33
48
  version: 1.3.3
34
- version:
49
+ type: :runtime
50
+ version_requirements: *id002
35
51
  description: A client for Hatena Diary to post and delete blog entries.
36
52
  email: arikui.ruby@gmail.com
37
53
  executables: []
@@ -65,21 +81,27 @@ rdoc_options:
65
81
  require_paths:
66
82
  - lib
67
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
68
85
  requirements:
69
86
  - - ">="
70
87
  - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
71
91
  version: "0"
72
- version:
73
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
74
94
  requirements:
75
95
  - - ">="
76
96
  - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
77
100
  version: "0"
78
- version:
79
101
  requirements: []
80
102
 
81
103
  rubyforge_project: hatenadiary
82
- rubygems_version: 1.3.5
104
+ rubygems_version: 1.5.2
83
105
  signing_key:
84
106
  specification_version: 3
85
107
  summary: It is a library provides a client for Hatena Diary to post and delete blog entries.