hatenaapigraph 0.1.2 → 0.2.0
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/CHANGELOG +3 -0
- data/README +10 -3
- data/lib/hatena/api/graph.rb +94 -15
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -6,12 +6,19 @@
|
|
6
6
|
require 'hatena/api/graph'
|
7
7
|
|
8
8
|
graph = Hatena::API::Graph.new('username', 'password')
|
9
|
-
graph.post('graphname', Time.now, 10.5)
|
10
|
-
graph.
|
9
|
+
# graph.post('graphname', Time.now, 10.5) # obsolute
|
10
|
+
graph.post_data(graphname, :date => Date.today, :value => 10)
|
11
|
+
data = graph.get_data(graphname)
|
12
|
+
p data[Date.today]
|
13
|
+
data2 = graph.get_data('日記数', :username => 'hatenadiary')
|
14
|
+
|
15
|
+
config = graph.get_config(graphname)
|
16
|
+
config['reverse'] = true
|
17
|
+
graph.post_config(graphname, config)
|
11
18
|
|
12
19
|
# use proxy
|
13
20
|
graph.proxy = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
|
14
|
-
graph.
|
21
|
+
graph.post_data(graphname, :value => 10)
|
15
22
|
|
16
23
|
== Installation
|
17
24
|
|
data/lib/hatena/api/graph.rb
CHANGED
@@ -5,13 +5,18 @@ require 'sha1'
|
|
5
5
|
require 'net/http'
|
6
6
|
require 'uri'
|
7
7
|
require 'time'
|
8
|
+
require 'yaml'
|
8
9
|
|
9
10
|
module Hatena
|
10
11
|
module API
|
11
12
|
class GraphError < StandardError; end
|
12
13
|
class Graph
|
13
14
|
DATE_FORMAT = '%Y-%m-%d'
|
14
|
-
|
15
|
+
GRAPH_API_URL = 'http://graph.hatena.ne.jp/api/'
|
16
|
+
GRAPH_API_DATA_URI = URI.parse GRAPH_API_URL + 'data'
|
17
|
+
GRAPH_API_CONFIG_URI = URI.parse GRAPH_API_URL + 'config'
|
18
|
+
|
19
|
+
CONFIG_BOOLEAN_KEYS = %w[showdata stack reverse formula nolabel]
|
15
20
|
|
16
21
|
def initialize(username, password)
|
17
22
|
@username = username
|
@@ -20,27 +25,101 @@ module Hatena
|
|
20
25
|
end
|
21
26
|
attr_accessor :proxy
|
22
27
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
# graph.post 'graphname', :value => 10
|
29
|
+
# graph.post 'graphname', :value => 10, :date => Time.now
|
30
|
+
def post_data(graphname, options, value = nil)
|
31
|
+
if value.nil?
|
32
|
+
params = {
|
33
|
+
:graphname => graphname,
|
34
|
+
}.merge options
|
35
|
+
else
|
36
|
+
# obsolute arguments.
|
37
|
+
date = options
|
38
|
+
params = {
|
39
|
+
:graphname => graphname,
|
40
|
+
:date => date,
|
41
|
+
:value => value,
|
42
|
+
}
|
43
|
+
end
|
44
|
+
params[:value] = params[:value].to_f
|
45
|
+
params[:date] = params[:date].strftime(DATE_FORMAT) if params[:date]
|
46
|
+
|
47
|
+
res = http_post GRAPH_API_DATA_URI, params
|
48
|
+
raise GraphError.new("request not successed: #{res}") if res.code != '201'
|
49
|
+
res
|
50
|
+
end
|
51
|
+
alias_method :post, :post_data
|
52
|
+
|
53
|
+
def get_data(graphname, options = {})
|
30
54
|
params = {
|
31
55
|
:graphname => graphname,
|
32
|
-
:
|
33
|
-
|
56
|
+
:type => 'yaml',
|
57
|
+
}.merge options
|
58
|
+
res = http_get GRAPH_API_DATA_URI, params
|
59
|
+
p GRAPH_API_DATA_URI, params
|
60
|
+
raise GraphError.new("request not successed: #{res}") if res.code != '200'
|
61
|
+
YAML::load res.body
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_config(graphname)
|
65
|
+
params = {
|
66
|
+
:graphname => graphname,
|
67
|
+
:type => 'yaml',
|
34
68
|
}
|
35
|
-
res =
|
69
|
+
res = http_get GRAPH_API_CONFIG_URI, params
|
70
|
+
raise GraphError.new("request not successed: #{res}") if res.code != '200'
|
71
|
+
config = YAML::load res.body
|
72
|
+
config_booleanize config
|
73
|
+
end
|
74
|
+
|
75
|
+
def post_config(graphname, options)
|
76
|
+
params = {
|
77
|
+
:graphname => graphname,
|
78
|
+
}.merge options
|
79
|
+
params = boolean2queryparam(params)
|
80
|
+
res = http_post GRAPH_API_CONFIG_URI, params
|
36
81
|
raise GraphError.new("request not successed: #{res}") if res.code != '201'
|
37
82
|
res
|
38
83
|
end
|
39
84
|
|
40
|
-
private
|
41
|
-
def
|
42
|
-
|
43
|
-
|
85
|
+
private
|
86
|
+
def config_booleanize(config)
|
87
|
+
CONFIG_BOOLEAN_KEYS.each do |key|
|
88
|
+
config[key] = (config[key] == 1 ? true : false) if config.has_key? key
|
89
|
+
end
|
90
|
+
config
|
91
|
+
end
|
92
|
+
|
93
|
+
def boolean2queryparam(config)
|
94
|
+
CONFIG_BOOLEAN_KEYS.each do |key|
|
95
|
+
config[key] = (config[key] ? 1 : 0) if(config.has_key?(key) || config.has_key?(key.to_sym))
|
96
|
+
end
|
97
|
+
config
|
98
|
+
end
|
99
|
+
|
100
|
+
def http_post(url, params)
|
101
|
+
http url, params, :post
|
102
|
+
end
|
103
|
+
|
104
|
+
def http_get(url, params)
|
105
|
+
http url, params, :get
|
106
|
+
end
|
107
|
+
|
108
|
+
def http(url, params, type = :post)
|
109
|
+
headers = {
|
110
|
+
'Access' => 'application/x.atom+xml, application/xml, text/xml, */*',
|
111
|
+
'X-WSSE' => wsse(@username, @password),
|
112
|
+
}
|
113
|
+
case type
|
114
|
+
when :post
|
115
|
+
req = ::Net::HTTP::Post.new(url.path, headers)
|
116
|
+
req.form_data = params
|
117
|
+
when :get
|
118
|
+
url.query = params.map {|k,v| "#{URI::encode(k.to_s)}=#{URI::encode(v.to_s)}" }.join('&')
|
119
|
+
req = ::Net::HTTP::Get.new(url.request_uri, headers)
|
120
|
+
else
|
121
|
+
raise ArgumentsError.new('type must be :post or :get')
|
122
|
+
end
|
44
123
|
req.basic_auth url.user, url.password if url.user
|
45
124
|
if @proxy
|
46
125
|
proxy = @proxy
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.1
|
|
3
3
|
specification_version: 1
|
4
4
|
name: hatenaapigraph
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2007-06-12 00:00:00 +09:00
|
8
8
|
summary: description of gem
|
9
9
|
require_paths:
|
10
10
|
- lib
|