Class: HatenaBM

Inherits:
Object
  • Object
show all
Defined in:
lib/hatenabm.rb

Constant Summary collapse

HATENA_URL =
"b.hatena.ne.jp"
FEED_PATH =
"/atom/feed"
POST_PATH =
"/atom/post"
EDIT_PATH =
"/atom/edit/"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HatenaBM

Returns a new instance of HatenaBM.



31
32
33
34
35
# File 'lib/hatenabm.rb', line 31

def initialize(options)
  user = options[:user]
  pass = options[:pass]
  @wsse = get_wsse(user, pass)
end

Instance Method Details

#delete(options) ⇒ Object

Delete specified bookmark



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hatenabm.rb', line 119

def delete(options)
  eid = options[:eid] || nil
  raise "Delete Error : Invalid eid" if eid.nil?
  api_path = EDIT_PATH + eid
  header = { "X-WSSE" => @wsse }

  Net::HTTP.version_1_2
  Net::HTTP.start(HATENA_URL){|http|
    response = http.delete(api_path, header)
    raise "Delete Error : #{response.code} - #{response.message}" unless response.code == "200"
    return true
  }
end

#get(options) ⇒ Object

Get specified bookmark



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hatenabm.rb', line 77

def get(options)
  eid = options[:eid] || nil
  raise "Get Error : Invalid eid" if eid.nil?
  api_path = EDIT_PATH + eid
  header = { "X-WSSE"=> @wsse }

  Net::HTTP.version_1_2
  Net::HTTP.start(HATENA_URL){|http|
    response = http.get(api_path, header)
    return response.body
  }
end

#modify(options) ⇒ Object

Modify specified bookmark



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hatenabm.rb', line 91

def modify(options)
  eid = options[:eid] || nil
  title = options[:title] || nil
  tags = options[:tags] || nil
  summary = options[:summary] || nil

  raise "Edit Error : need eid" if eid.nil?

  api_path = EDIT_PATH + eid
  header = { "X-WSSE" => @wsse }
  tags = "[#{tags.split(/\s/).join('][')}]" unless tags.nil?

  data  = "<?xml version=\"1.0\"?>"
  data << "<entry xmlns=\"http://purl.org/atom/ns#\">"
  data << "<title>#{title}</title>" unless title.nil?
  data << "<summary type=\"text/plain\">#{tags}#{summary}</summary>" unless summary.nil?
  data << "</entry>"

  Net::HTTP.version_1_2
  Net::HTTP.start(HATENA_URL){|http|
    response = http.put(api_path, toutf8(data), header)
    raise "Edit Error : #{response.code} - #{response.message}" unless response.code == "200"
    return true
  }

end

#post(options) ⇒ Object

Post new bookmark



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hatenabm.rb', line 38

def post(options)
  title = options[:title]
  link = options[:link]
  summary = options[:summary] || nil 
  tags = options[:tags] || nil
  header = {
    "Content-Type" => 'application/x.atom+xml; charset="utf-8"', 
    "X-WSSE" => @wsse
  }
  tags = "[#{tags.split(/\s/).join('][')}]" unless tags.nil?

  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  EOF\n\n  Net::HTTP.version_1_2\n  Net::HTTP.start(HATENA_URL){|http|\n    response = http.post(POST_PATH, toutf8(data), header)\n    raise \"Post Error : \#{response.code} - \#{response.message}\" unless response.code == \"201\"\n    return true\n  }\nend\n"

#recent(option = "") ⇒ Object

Get recent bookmarks



67
68
69
70
71
72
73
74
# File 'lib/hatenabm.rb', line 67

def recent(option = "")
  header = { "X-WSSE" => @wsse }
  Net::HTTP.version_1_2
  Net::HTTP.start(HATENA_URL){|http|
    response = http.get(FEED_PATH + option, header)
    return response.body
  }
end