Class: HatenaDiary::Client
- Inherits:
-
Object
- Object
- HatenaDiary::Client
- Defined in:
- lib/hatenadiary.rb
Class Method Summary collapse
-
.login(username, password, proxy = nil, &block) ⇒ Object
Allocates Client object.
- .mechanizer ⇒ Object
- .mechanizer=(klass) ⇒ Object
Instance Method Summary collapse
-
#delete(yyyy, mm, dd) ⇒ Object
Deletes an entry from Hatena diary service.
-
#initialize(username, password) ⇒ Client
constructor
Allocates Client object.
-
#login(username = nil, password = nil) ⇒ Object
Does login.
-
#login? ⇒ Boolean
Returns a client itself was logined or not.
-
#logout ⇒ Object
Does logout if already logined.
-
#post(yyyy, mm, dd, title, body, trivial_p = false) ⇒ Object
Posts an entry to Hatena diary service.
-
#set_proxy(url, port) ⇒ Object
Configure proxy.
-
#transaction(username = nil, password = nil) ⇒ Object
Login and execute a received block, and then logout ensurely.
Constructor Details
#initialize(username, password) ⇒ Client
Allocates Client object.
- username
-
Hatena ID
- password
-
Password for username
80 81 82 83 84 85 |
# File 'lib/hatenadiary.rb', line 80 def initialize(username, password) @agent = self.class.mechanizer.new @username = username @password = password @current_account = nil end |
Class Method Details
.login(username, password, proxy = nil, &block) ⇒ Object
Allocates Client object.
If block given, login and execute a received block, and then logout ensurely.
- username
-
Hatena ID
- password
-
Password for username
- proxy
-
Proxy configuration; [proxy_url, port_no] | nil
69 70 71 72 73 74 |
# File 'lib/hatenadiary.rb', line 69 def self.login(username, password, proxy = nil, &block) client = new(username, password) client.set_proxy(*proxy) if proxy return client unless block_given? client.transaction(&block) end |
.mechanizer ⇒ Object
53 54 55 |
# File 'lib/hatenadiary.rb', line 53 def self.mechanizer @mechanizer ||= WWW::Mechanize end |
.mechanizer=(klass) ⇒ Object
57 58 59 |
# File 'lib/hatenadiary.rb', line 57 def self.mechanizer=(klass) @mechanizer = klass end |
Instance Method Details
#delete(yyyy, mm, dd) ⇒ Object
Deletes an entry from Hatena diary service.
Raises HatenaDiary::LoginError unless logined.
152 153 154 |
# File 'lib/hatenadiary.rb', line 152 def delete(yyyy, mm, dd) edit_page(yyyy, mm, dd, -1) end |
#login(username = nil, password = nil) ⇒ Object
Does login.
If username or password are invalid, raises HatenaDiary::LoginError .
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/hatenadiary.rb', line 113 def login(username = nil, password = nil) username ||= @username password ||= @password form = @agent.get("https://www.hatena.ne.jp/login").forms.first form["name"] = username form["password"] = password form["persistent"] = "true" response = form.submit @current_account = [username, password] case response.title when "Hatena" then response when "Login - Hatena" then raise LoginError.new("login failure").set_account(username, password) else raise Exception, '[BUG] must not happen (maybe cannot follow hatena spec)' end end |
#login? ⇒ Boolean
Returns a client itself was logined or not.
-> true | false
106 107 108 |
# File 'lib/hatenadiary.rb', line 106 def login? @current_account ? true : false end |
#logout ⇒ Object
Does logout if already logined.
130 131 132 133 134 135 136 |
# File 'lib/hatenadiary.rb', line 130 def logout return unless login? @agent.get("https://www.hatena.ne.jp/logout") account = @current_account @current_account = nil account end |
#post(yyyy, mm, dd, title, body, trivial_p = false) ⇒ Object
Posts an entry to Hatena diary service.
Raises HatenaDiary::LoginError unless logined.
141 142 143 144 145 146 147 |
# File 'lib/hatenadiary.rb', line 141 def post(yyyy, mm, dd, title, body, trivial_p = false) edit_page(yyyy, mm, dd, 0) do |form| form["title"] = title form["body"] = body form["trivial"] = "true" if trivial_p end end |
#set_proxy(url, port) ⇒ Object
Configure proxy.
88 89 90 |
# File 'lib/hatenadiary.rb', line 88 def set_proxy(url, port) @agent.set_proxy(url, port) end |
#transaction(username = nil, password = nil) ⇒ Object
Login and execute a received block, and then logout ensurely.
93 94 95 96 97 98 99 100 101 |
# File 'lib/hatenadiary.rb', line 93 def transaction(username = nil, password = nil) raise LocalJumpError, "no block given" unless block_given? login(username, password) begin yield(self) ensure logout end end |