Class: Hatena::API::Graph
- Inherits:
-
Object
- Object
- Hatena::API::Graph
- 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
-
#proxy ⇒ Object
Returns the value of attribute proxy.
Instance Method Summary collapse
- #get_config(graphname) ⇒ Object
- #get_data(graphname, options = {}) ⇒ Object
-
#initialize(username, password) ⇒ Graph
constructor
A new instance of Graph.
- #post_config(graphname, options) ⇒ Object
-
#post_data(graphname, options, value = nil) ⇒ Object
(also: #post)
graph.post ‘graphname’, :value => 10 graph.post ‘graphname’, :value => 10, :date => Time.now.
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
#proxy ⇒ Object
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
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
57 58 59 60 61 62 63 64 65 |
# File 'lib/hatena/api/graph.rb', line 57 def get_data(graphname, = {}) params = { :graphname => graphname, :type => 'yaml', }.merge 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
78 79 80 81 82 83 84 85 86 |
# File 'lib/hatena/api/graph.rb', line 78 def post_config(graphname, ) params = { :graphname => graphname, }.merge 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
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, , value = nil) if value.nil? params = { :graphname => graphname, }.merge else # obsolute arguments. date = 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 |