numerics 0.2.1
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/lib/numerics/connection.rb +181 -0
- data/lib/numerics.rb +76 -0
- metadata +76 -0
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'cgi'
|
3
|
+
if RUBY_VERSION =~ /^1\.8/
|
4
|
+
require 'rubygems'
|
5
|
+
end
|
6
|
+
require 'yajl'
|
7
|
+
|
8
|
+
module Numerics
|
9
|
+
class Connection
|
10
|
+
|
11
|
+
HOST = 'api.numerics.io'
|
12
|
+
PORT = '443'
|
13
|
+
BASE_PATH = '/ts'
|
14
|
+
|
15
|
+
attr_reader :access_key, :secret_key
|
16
|
+
|
17
|
+
def initialize(access_key, secret_key, host=nil, port=nil)
|
18
|
+
@access_key, @secret_key = access_key.to_s, secret_key.to_s
|
19
|
+
@host = (host || HOST).to_s
|
20
|
+
@port = (port || PORT).to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
"#<Numerics::Connection @access_key=#{@access_key}>"
|
25
|
+
end
|
26
|
+
|
27
|
+
## Start Commands ##
|
28
|
+
|
29
|
+
def list
|
30
|
+
self.coll_get
|
31
|
+
end
|
32
|
+
|
33
|
+
def create(name, precision=nil)
|
34
|
+
self.coll_post([name, precision.to_s])
|
35
|
+
end
|
36
|
+
|
37
|
+
def about(timeseries)
|
38
|
+
self.get(timeseries, 'about')
|
39
|
+
end
|
40
|
+
|
41
|
+
def describe(timeseries, md)
|
42
|
+
self.put(timeseries, 'describe', md)
|
43
|
+
end
|
44
|
+
|
45
|
+
def erase(timeseries)
|
46
|
+
self.destroy(timeseries)
|
47
|
+
end
|
48
|
+
|
49
|
+
##@@ todo - support time words
|
50
|
+
def insert(timeseries, number='1', time=Time.now, properties={})
|
51
|
+
self.post(timeseries, 'insert', [time.to_s, number.to_s, properties])
|
52
|
+
end
|
53
|
+
|
54
|
+
##@@ todo - support time words
|
55
|
+
def remove(timeseries, number='1', time=Time.now, properties={})
|
56
|
+
self.post(timeseries, 'remove', [time.to_s, number.to_s, properties])
|
57
|
+
end
|
58
|
+
|
59
|
+
def entries(timeseries, query=nil)
|
60
|
+
self.get(timeseries, 'entries', query)
|
61
|
+
end
|
62
|
+
|
63
|
+
def series(timeseries, query=nil)
|
64
|
+
self.get(timeseries, 'series', query)
|
65
|
+
end
|
66
|
+
|
67
|
+
def stats(timeseries)
|
68
|
+
self.get(timeseries, 'stats')
|
69
|
+
end
|
70
|
+
|
71
|
+
def distribution(timeseries, width=nil, start=nil)
|
72
|
+
query = if width || start
|
73
|
+
{:w => width, :s => start}
|
74
|
+
else
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
self.get(timeseries, 'distribution', query)
|
78
|
+
end
|
79
|
+
|
80
|
+
def properties(timeseries)
|
81
|
+
self.get(timeseries, 'properties')
|
82
|
+
end
|
83
|
+
|
84
|
+
def version(timeseries)
|
85
|
+
self.get(timeseries, 'version')
|
86
|
+
end
|
87
|
+
|
88
|
+
def draw(timeseries, query=nil)
|
89
|
+
self.get(timeseries, 'draw', query)
|
90
|
+
end
|
91
|
+
|
92
|
+
def histogram(timeseries, width=nil, start=nil)
|
93
|
+
query = if width || start
|
94
|
+
{:w => width, :s => start}
|
95
|
+
else
|
96
|
+
nil
|
97
|
+
end
|
98
|
+
self.get(timeseries, 'histogram', query)
|
99
|
+
end
|
100
|
+
|
101
|
+
def headline(timeseries, query=nil)
|
102
|
+
self.get(timeseries, 'headline', query)
|
103
|
+
end
|
104
|
+
|
105
|
+
def trend(timeseries, query=nil)
|
106
|
+
self.get(timeseries, 'trend', query)
|
107
|
+
end
|
108
|
+
|
109
|
+
## End Commands
|
110
|
+
|
111
|
+
protected
|
112
|
+
|
113
|
+
def client
|
114
|
+
if !@client
|
115
|
+
@client = Net::HTTP.new(@host, @port)
|
116
|
+
@client.use_ssl = true
|
117
|
+
@client.verify_mode = OpenSSL::SSL::VERIFY_NONE ##@@remove me after alpha!
|
118
|
+
end
|
119
|
+
@client
|
120
|
+
end
|
121
|
+
|
122
|
+
def get(timeseries, command, query=nil)
|
123
|
+
path = [BASE_PATH, [*timeseries], command].flatten.join('/')
|
124
|
+
if query
|
125
|
+
path << '?'
|
126
|
+
parts = []
|
127
|
+
query.keys.each do |k|
|
128
|
+
parts << [CGI::escape(k.to_s), CGI::escape(query[k].to_s)].join('=')
|
129
|
+
end
|
130
|
+
path << parts.join('&')
|
131
|
+
end
|
132
|
+
self.do_request Net::HTTP::Get.new(path)
|
133
|
+
end
|
134
|
+
|
135
|
+
def post(timeseries, command, args)
|
136
|
+
path = [BASE_PATH, [*timeseries], command].flatten.join('/')
|
137
|
+
req = Net::HTTP::Post.new(path)
|
138
|
+
req.body = Yajl::Encoder.encode(args)
|
139
|
+
self.do_request req
|
140
|
+
end
|
141
|
+
|
142
|
+
def put(timeseries, command, args)
|
143
|
+
path = [BASE_PATH, [*timeseries], command].flatten.join('/')
|
144
|
+
req = Net::HTTP::Put.new(path)
|
145
|
+
req.body = Yajl::Encoder.encode(args)
|
146
|
+
self.do_request req
|
147
|
+
end
|
148
|
+
|
149
|
+
def destroy(timeseries)
|
150
|
+
path = [BASE_PATH, [*timeseries]].flatten.join('/')
|
151
|
+
self.do_request Net::HTTP::Delete.new(path)
|
152
|
+
end
|
153
|
+
|
154
|
+
def coll_get
|
155
|
+
self.do_request Net::HTTP::Get.new(BASE_PATH)
|
156
|
+
end
|
157
|
+
|
158
|
+
def coll_post(args)
|
159
|
+
req = Net::HTTP::Post.new(BASE_PATH)
|
160
|
+
req.body = Yajl::Encoder.encode(args)
|
161
|
+
self.do_request req
|
162
|
+
end
|
163
|
+
|
164
|
+
def do_request(req)
|
165
|
+
req['X-Access-Key'] = @access_key
|
166
|
+
req['X-Secret-Key'] = @secret_key
|
167
|
+
req['Content-Type'] = 'application/json'
|
168
|
+
response = self.client.start do |h|
|
169
|
+
h.request(req)
|
170
|
+
end
|
171
|
+
data = Yajl::Parser.parse(response.body)
|
172
|
+
if data[0]
|
173
|
+
{:error => data[0]}
|
174
|
+
else
|
175
|
+
{:success => true, :data => data[1]}
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
data/lib/numerics.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
if RUBY_VERSION =~ /^1\.8/
|
2
|
+
require 'rubygems'
|
3
|
+
end
|
4
|
+
require 'yajl'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
require 'numerics/connection'
|
8
|
+
|
9
|
+
module Numerics
|
10
|
+
|
11
|
+
VERSION = '0.2.1'
|
12
|
+
|
13
|
+
def self.connect(arg, env=nil)
|
14
|
+
config = if arg.is_a?(Hash)
|
15
|
+
arg
|
16
|
+
else
|
17
|
+
if arg.match(/\.json$/)
|
18
|
+
File.open(arg){ |f| Yajl::Parser.parse(f) }
|
19
|
+
elsif arg.match(/\.ya?ml$/)
|
20
|
+
YAML::load_file(arg)
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if !config
|
27
|
+
raise "Only json and yaml config files supported"
|
28
|
+
end
|
29
|
+
|
30
|
+
if env
|
31
|
+
config = config[env.to_s] || config[env.to_sym]
|
32
|
+
if !config
|
33
|
+
raise "No #{env} found in #{arg}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
access_key = config[:access_key] || config['access_key']
|
38
|
+
secret_key = config[:secret_key] || config['secret_key']
|
39
|
+
host = config[:host] || config['host'] # nil means use the default
|
40
|
+
port = config[:port] || config['port'] # nil means use the default
|
41
|
+
|
42
|
+
if !access_key && !secret_key
|
43
|
+
raise ArgumentError, 'Numerics.connect(config_file, env=nil) or Numerics.connect(:access_key => access_key, :secret_key => :secret_key)'
|
44
|
+
end
|
45
|
+
|
46
|
+
Numerics::Connection.new(access_key, secret_key, host, port)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.config(arg, env=nil)
|
50
|
+
@global_connection = self.connect(arg, env)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.global_connection
|
54
|
+
@global_connection
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.global_connection=(gc)
|
58
|
+
@global_connection = gc
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.respond_to?(method)
|
62
|
+
!@global_connection.nil? && @global_connection.respond_to?(method)
|
63
|
+
end
|
64
|
+
|
65
|
+
##@@ check syntax
|
66
|
+
def self.method_missing(method, *args, &block)
|
67
|
+
if self.respond_to?(method)
|
68
|
+
@global_connection.send(method, *args, &block)
|
69
|
+
else
|
70
|
+
super
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: numerics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Lund
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-11-19 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: yajl-ruby
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: yaml
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Official Ruby client for the Numerics.io API. Numerics.io is a custom metrics and analytics service currently in private alpha.
|
38
|
+
email: code@numerics.io
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/numerics.rb
|
47
|
+
- lib/numerics/connection.rb
|
48
|
+
homepage: http://github.com/frequalize/numerics-ruby
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.8.11
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Official Ruby client for the Numerics.io API
|
75
|
+
test_files: []
|
76
|
+
|