Class: Yohoushi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri = 'http://127.0.0.1:4804') ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_uri (String) (defaults to: 'http://127.0.0.1:4804')

    The base uri of Yohoushi



19
20
21
# File 'lib/yohoushi/client.rb', line 19

def initialize(base_uri = 'http://127.0.0.1:4804')
  @base_uri = base_uri
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



16
17
18
# File 'lib/yohoushi/client.rb', line 16

def base_uri
  @base_uri
end

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/yohoushi/client.rb', line 14

def client
  @client
end

#debugObject

Returns the value of attribute debug.



13
14
15
# File 'lib/yohoushi/client.rb', line 13

def debug
  @debug
end

#debug_devObject

Returns the value of attribute debug_dev.



15
16
17
# File 'lib/yohoushi/client.rb', line 15

def debug_dev
  @debug_dev
end

Instance Method Details

#get_graph(path) ⇒ Hash

Get parameters from a graph, GET /api/graphs/:path

"number":"1",
"llimit":"-1000000000",
"mode":"gauge",
"stype":"AREA",
"adjustval":"1",
"meta":"",
"service_name":"multiforecast",
"gmode":"gauge",
"color":"#cc6633",
"created_at":"2013/08/29 11:58:15",
"section_name":"foo%2Fbar%2Ffoo",
"ulimit":"1000000000",
"id":"2",
"graph_name":"bar",
"description":"",
"sulimit":"100000",
"unit":"","sort":"0",
"updated_at":"2013/08/29 11:58:15",
"adjust":"*",
"path":"foo/bar/foo/bar",
"type":"AREA",
"sllimit":"-100000",
"md5":"c81e728d9d4c2f636f067f89cc14862c"

Parameters:

  • path (String)

Returns:

  • (Hash)

    the graph property



119
120
121
# File 'lib/yohoushi/client.rb', line 119

def get_graph(path)
  get_json("/api/graphs/#{escape path}")
end

#get_json(path) ⇒ Hash

GET the JSON API

Parameters:

  • path (String)

Returns:

  • (Hash)

    response body



40
41
42
43
44
# File 'lib/yohoushi/client.rb', line 40

def get_json(path)
  @res = client.get("#{@base_uri}#{path}")
  handle_error(@res)
  JSON.parse(@res.body)
end

#last_responseObject



33
34
35
# File 'lib/yohoushi/client.rb', line 33

def last_response
  @res
end

#post_graph(path, params) ⇒ Hash

Post parameters to a graph, POST /api/graphs/:path “data”:{

"number":"1",
"llimit":"-1000000000",
"mode":"gauge",
"stype":"AREA",
"adjustval":"1",
"meta":"",
"service_name":"multiforecast",
"gmode":"gauge",
"color":"#cc6633",
"created_at":"2013/08/29 11:58:15",
"section_name":"foo%2Fbar%2Ffoo",
"ulimit":"1000000000",
"id":"2",
"graph_name":"bar",
"description":"",
"sulimit":"100000",
"unit":"","sort":"0",
"updated_at":"2013/08/29 11:58:15",
"adjust":"*",
"path":"foo/bar/foo/bar",
"type":"AREA",
"sllimit":"-100000",
"md5":"c81e728d9d4c2f636f067f89cc14862c"

Parameters:

  • path (String)
  • params (Hash)

    The POST parameters.

Returns:

  • (Hash)

    the error code and graph property



87
88
89
# File 'lib/yohoushi/client.rb', line 87

def post_graph(path, params)
  post_json("/api/graphs/#{escape path}", params)
end

#post_json(path, data = {}) ⇒ Hash

POST the JSON API

Parameters:

  • path (String)
  • data (Hash) (defaults to: {})

Returns:

  • (Hash)

    response body



50
51
52
53
54
55
# File 'lib/yohoushi/client.rb', line 50

def post_json(path, data = {})
  pp data if @debug
  @res = client.post("#{@base_uri}#{path}", data)
  handle_error(@res)
  JSON.parse(@res.body)
end