Class: Hatena::API::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/hatena/api/graph.rb

Constant Summary collapse

DATE_FORMAT =
'%Y-%m-%d'
GRAPH_API_URL =
'http://graph.hatena.ne.jp/api/'
GRAPH_API_DATA_URI =
URI.parse GRAPH_API_URL + 'data'
GRAPH_API_CONFIG_URI =
URI.parse GRAPH_API_URL + 'config'
CONFIG_BOOLEAN_KEYS =
%w[showdata stack reverse formula nolabel]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Graph

Returns a new instance of Graph.



21
22
23
24
25
# File 'lib/hatena/api/graph.rb', line 21

def initialize(username, password)
  @username = username
  @password = password
  @proxy = nil
end

Instance Attribute Details

#proxyObject

Returns the value of attribute proxy.



26
27
28
# File 'lib/hatena/api/graph.rb', line 26

def proxy
  @proxy
end

Instance Method Details

#get_config(graphname) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
# File 'lib/hatena/api/graph.rb', line 67

def get_config(graphname)
  params = {
    :graphname => graphname,
    :type => 'yaml',
  }
  res = http_get GRAPH_API_CONFIG_URI, params
  raise GraphError.new("request not successed: #{res}") if res.code != '200'
  config = YAML::load res.body
  config_booleanize config
end

#get_data(graphname, options = {}) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
# File 'lib/hatena/api/graph.rb', line 57

def get_data(graphname, options = {})
  params = {
    :graphname => graphname,
    :type => 'yaml',
  }.merge options
  res = http_get GRAPH_API_DATA_URI, params
  raise GraphError.new("request not successed: #{res}") if res.code != '200'
  YAML::load res.body
end

#post_config(graphname, options) ⇒ Object

Raises:



78
79
80
81
82
83
84
85
86
# File 'lib/hatena/api/graph.rb', line 78

def post_config(graphname, options)
  params = {
    :graphname => graphname,
  }.merge options
  params = boolean2queryparam(params)
  res = http_post GRAPH_API_CONFIG_URI, params
  raise GraphError.new("request not successed: #{res}") if res.code != '201'
  res
end

#post_data(graphname, options, value = nil) ⇒ Object Also known as: post

graph.post ‘graphname’, :value => 10 graph.post ‘graphname’, :value => 10, :date => Time.now

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hatena/api/graph.rb', line 30

def post_data(graphname, options, value = nil)
  if value.nil?
    params = {
      :graphname => graphname,
    }.merge options
  else
    # obsolute arguments.
    date = options
    params = {
      :graphname => graphname,
      :date => date,
      :value => value,
    }
  end

  if params[:value] != nil
    params[:value] = params[:value].to_f
  end

  params[:date] = params[:date].strftime(DATE_FORMAT) if params[:date]

  res = http_post GRAPH_API_DATA_URI, params
  raise GraphError.new("request not successed: #{res}") if res.code != '201'
  res
end