| Class | HatenaBM |
| In: |
lib/hatenabm.rb
|
| Parent: | Object |
| HATENA_URL | = | "b.hatena.ne.jp" |
| FEED_PATH | = | "/atom/feed" |
| POST_PATH | = | "/atom/post" |
| EDIT_PATH | = | "/atom/edit/" |
# File lib/hatenabm.rb, line 31
31: def initialize(options)
32: user = options[:user]
33: pass = options[:pass]
34: @wsse = get_wsse(user, pass)
35: end
Delete specified bookmark
# File lib/hatenabm.rb, line 120
120: def delete(options)
121: eid = options[:eid] || nil
122: raise "Delete Error : Invalid eid" if eid.nil?
123: api_path = EDIT_PATH + eid
124: header = { "X-WSSE" => @wsse }
125:
126: Net::HTTP.version_1_2
127: Net::HTTP.start(HATENA_URL){|http|
128: response = http.delete(api_path, header)
129: raise "Delete Error : #{response.code} - #{response.message}" unless response.code == "200"
130: return true
131: }
132: end
Get specified bookmark
# File lib/hatenabm.rb, line 78
78: def get(options)
79: eid = options[:eid] || nil
80: raise "Get Error : Invalid eid" if eid.nil?
81: api_path = EDIT_PATH + eid
82: header = { "X-WSSE"=> @wsse }
83:
84: Net::HTTP.version_1_2
85: Net::HTTP.start(HATENA_URL){|http|
86: response = http.get(api_path, header)
87: return response.body
88: }
89: end
Modify specified bookmark
# File lib/hatenabm.rb, line 92
92: def modify(options)
93: eid = options[:eid] || nil
94: title = options[:title] || nil
95: tags = options[:tags] || nil
96: summary = options[:summary] || nil
97:
98: raise "Edit Error : need eid" if eid.nil?
99:
100: api_path = EDIT_PATH + eid
101: header = { "X-WSSE" => @wsse }
102: tags = "[#{tags.split(/\s/).join('][')}]" unless tags.nil?
103:
104: data = "<?xml version=\"1.0\"?>"
105: data << "<entry xmlns=\"http://purl.org/atom/ns#\">"
106: data << "<title>#{title}</title>" unless title.nil?
107: data << "<summary type=\"text/plain\">#{tags}#{summary}</summary>" unless summary.nil?
108: data << "</entry>"
109:
110: Net::HTTP.version_1_2
111: Net::HTTP.start(HATENA_URL){|http|
112: response = http.put(api_path, toutf8(data), header)
113: raise "Edit Error : #{response.code} - #{response.message}" unless response.code == "200"
114: return true
115: }
116:
117: end
Post new bookmark
# File lib/hatenabm.rb, line 38
38: def post(options)
39: title = options[:title]
40: link = options[:link]
41: summary = options[:summary] || nil
42: tags = options[:tags] || nil
43: header = {
44: "Content-Type" => 'application/x.atom+xml; charset="utf-8"',
45: "X-WSSE" => @wsse
46: }
47: tags = "[#{tags.split(/\s/).join('][')}]" unless tags.nil?
48:
49: data ="<?xml version=\"1.0\"?>\n<entry xmlns=\"http://purl.org/atom/ns#\">\n<title>\#{title}</title>\n<link rel=\"related\" type=\"text/html\" href=\"\#{link}\" />\n<summary type=\"text/plain\">\#{tags}\#{summary}</summary>\n</entry>\n"
50:
51: Net::HTTP.version_1_2
52: Net::HTTP.start(HATENA_URL){|http|
53: response = http.post(POST_PATH, toutf8(data), header)
54: raise "Post Error : #{response.code} - #{response.message}" unless response.code == "201"
55: return true
56: }
57: end