rjmetrics-client 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/lib/rjmetrics-client/client.rb +101 -95
- data/lib/rjmetrics_client.rb +12 -6
- metadata +14 -13
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
ZGRhNjNjMTUzNTk3MDJkOGVkZjlhNDkzZWNhNzU5ZjA4YjRkOTlhMg==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 17d4ec2cdea59b39c5945c6e960a6cf20f50609a
|
4
|
+
data.tar.gz: 31942dc32da373d613f89d9166fc46a16003dea5
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
YmRiOTkyYjY2YzJiOGFhODczOTc0ODM3MTJiNTAxZmRlNWEwMTkzN2MzMzUz
|
11
|
-
YTY0NzQ5YmE1ZDcwZjIxZjFjNjQyNGQ3NWVjNzQxNDc0Y2JiNzU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
YTUyMGZjNTYyOGQ0MTE3YmU4YzNiMTM3NDI4NTJhOGQ4OTQyNDYxN2ExMmMy
|
14
|
-
Nzk2ODQyMTYyNzY0ZDE0NWI3MjI0OTgyZjk1NzA1N2RjZDU3NjFlNzVjYzRh
|
15
|
-
MzNiM2YyM2NhNWZkYTc4ZmRkN2RkNWMyZjY5MTE4Y2I0ZmRmYjE=
|
6
|
+
metadata.gz: b1d848013b20153c79260f14bf0588cbc9c61a90c07297d240ded14da18eeaceb9d966cfdb9d5eaade0a6e9b4eb45d5a6065546f8b6a519869c3b7a63f2dcf79
|
7
|
+
data.tar.gz: 94c14efb7709725ab6470e027c86a9dfb2d8e80f5f70734090888d55ec9724fd49a8cfda3de8f38499d767bb44e638b61a0a882a31562db47e16a590bfeb0214
|
@@ -4,119 +4,125 @@ require 'rest_client'
|
|
4
4
|
require 'json'
|
5
5
|
require 'enumerator'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
@client_id
|
24
|
-
@api_key
|
25
|
-
@timeout_in_seconds
|
26
|
-
|
7
|
+
module RJMetrics
|
8
|
+
|
9
|
+
class Client
|
10
|
+
|
11
|
+
|
12
|
+
# default RJMetrics Data Import API url
|
13
|
+
API_BASE = "https://connect.rjmetrics.com/v2"
|
14
|
+
# RJMetrics Sandbox API url
|
15
|
+
SANDBOX_BASE = "https://sandbox-connect.rjmetrics.com/v2"
|
16
|
+
# Datapoints to push at a time
|
17
|
+
BATCH_SIZE = 100
|
18
|
+
# Timout Seconds
|
19
|
+
DEFAULT_TIMEOUT_SECONDS = 10
|
20
|
+
|
21
|
+
# Constructs a Client instance if it receives valid arguments or will raise an ArgumentError.
|
22
|
+
#
|
23
|
+
# @param client_id [Integer] your RJMetrics Client ID
|
24
|
+
# @param api_key [String] your RJMetrics API Key
|
25
|
+
# @param timeout_in_seconds [Integer] seconds to wait for API responses
|
26
|
+
def initialize(client_id, api_key, timeout_in_seconds = DEFAULT_TIMEOUT_SECONDS)
|
27
|
+
validateConstructorArgs(client_id, api_key, timeout_in_seconds)
|
28
|
+
@client_id = client_id
|
29
|
+
@api_key = api_key
|
30
|
+
@timeout_in_seconds = timeout_in_seconds
|
31
|
+
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
# Checks if the provided Client ID and API Key are valid credentials by requesting from the RJMetrics API Sandbox.
|
34
|
+
def authenticated?
|
35
|
+
test_data = {:keys => [:id], :id => 1}
|
36
|
+
begin
|
37
|
+
pushData("test", test_data, SANDBOX_BASE)
|
38
|
+
rescue InvalidRequestException
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
return true
|
35
42
|
end
|
36
|
-
return true
|
37
|
-
end
|
38
43
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
# Sends data to RJMetrics Data Import API in batches of 100.
|
45
|
+
#
|
46
|
+
# @param table_name [String] the table name you wish to store the data
|
47
|
+
# @param data [Hashamp] or Array of Hashmaps of data points that will get sent
|
48
|
+
# @param url [String] Import API url
|
49
|
+
# @return [Array] results of each request to RJMetrics Data Import API
|
50
|
+
def pushData(table_name, data, url = API_BASE)
|
51
|
+
responses = Array.new
|
52
|
+
validatePushDataArgs(table_name, data, url)
|
53
|
+
|
54
|
+
if not data.is_a? Array
|
55
|
+
data = Array.[](data)
|
56
|
+
end
|
57
|
+
|
58
|
+
data.each_slice(BATCH_SIZE) {|batch_data|
|
59
|
+
responses << makePushDataAPICall(table_name, batch_data, url)
|
60
|
+
}
|
61
|
+
return responses
|
51
62
|
end
|
52
63
|
|
53
|
-
|
54
|
-
puts "pushing batch of #{batch_data.count} data points"
|
55
|
-
responses << makePushDataAPICall(table_name, batch_data, url)
|
56
|
-
}
|
57
|
-
return responses
|
58
|
-
end
|
64
|
+
private
|
59
65
|
|
60
|
-
|
66
|
+
def validateConstructorArgs(client_id, api_key, timeout_in_seconds)
|
67
|
+
if not client_id.is_a? Integer or client_id <= 0
|
68
|
+
raise ArgumentError, "Invalid client ID: #{client_id} -- must be a positive integer."
|
69
|
+
end
|
61
70
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
end
|
71
|
+
if not timeout_in_seconds.is_a? Integer or timeout_in_seconds <= 0
|
72
|
+
raise ArgumentError, "Invalid timeout: #{timeout_in_seconds} -- must be a positive integer."
|
73
|
+
end
|
66
74
|
|
67
|
-
|
68
|
-
|
75
|
+
if not api_key.is_a? String
|
76
|
+
raise ArgumentError, "Invalid API key: #{api_key} -- must be a string."
|
77
|
+
end
|
69
78
|
end
|
70
79
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
80
|
+
def validatePushDataArgs(table_name, data, url)
|
81
|
+
if not data.is_a? Hash and not data.is_a? Array
|
82
|
+
raise ArgumentError, "Invalid data -- must be a valid Ruby Hash or Array."
|
83
|
+
end
|
75
84
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
85
|
+
if not table_name.is_a? String
|
86
|
+
raise ArgumentError, "Invalid table name: '#{table_name}' -- must be a string."
|
87
|
+
end
|
80
88
|
|
81
|
-
|
82
|
-
|
89
|
+
if not url.is_a? String
|
90
|
+
raise ArgumentError, "Invalid url: '#{url}' -- must be a string."
|
91
|
+
end
|
83
92
|
end
|
84
93
|
|
85
|
-
|
86
|
-
|
87
|
-
end
|
88
|
-
end
|
94
|
+
def makePushDataAPICall(table_name, data, url = API_BASE)
|
95
|
+
request_url = "#{url}/client/#{@client_id}/table/#{table_name}/data?apikey=#{@api_key}"
|
89
96
|
|
90
|
-
def makePushDataAPICall(table_name, data, url = API_BASE)
|
91
|
-
request_url = "#{url}/client/#{@client_id}/table/#{table_name}/data?apikey=#{@api_key}"
|
92
|
-
|
93
|
-
begin
|
94
|
-
response = RestClient.post(
|
95
|
-
request_url,
|
96
|
-
data.to_json,
|
97
|
-
{
|
98
|
-
:content_type => :json,
|
99
|
-
:accept => :json,
|
100
|
-
:timeout => @timeout_in_seconds
|
101
|
-
}
|
102
|
-
)
|
103
|
-
return response
|
104
|
-
rescue RestClient::Exception => error
|
105
97
|
begin
|
106
|
-
response =
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
98
|
+
response = RestClient.post(
|
99
|
+
request_url,
|
100
|
+
data.to_json,
|
101
|
+
{
|
102
|
+
:content_type => :json,
|
103
|
+
:accept => :json,
|
104
|
+
:timeout => @timeout_in_seconds
|
105
|
+
}
|
106
|
+
)
|
107
|
+
return response
|
108
|
+
rescue RestClient::Exception => error
|
109
|
+
begin
|
110
|
+
response = JSON.parse(error.response)
|
111
|
+
raise InvalidRequestException,
|
112
|
+
"The Import API returned: #{response['code']} #{response['message']}. Reasons: #{response['reasons']}"
|
113
|
+
rescue JSON::ParserError, TypeError => json_parse_error
|
114
|
+
raise InvalidResponseException,
|
115
|
+
"RestClientError: #{error.class}, With response: #{error.response}"
|
116
|
+
end
|
112
117
|
end
|
113
118
|
end
|
114
|
-
end
|
115
119
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
120
|
+
class UnableToConnectException < RuntimeError
|
121
|
+
end
|
122
|
+
class InvalidRequestException < RuntimeError
|
123
|
+
end
|
124
|
+
class InvalidResponseException < RuntimeError
|
125
|
+
end
|
126
|
+
|
121
127
|
end
|
122
128
|
end
|
data/lib/rjmetrics_client.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
|
+
require_relative 'rjmetrics-client/client'
|
2
|
+
|
1
3
|
class RJMetricsClient
|
2
4
|
|
5
|
+
# Constructs a RJMetricsClient instance if it receives valid arguments or will raise an ArgumentError.
|
6
|
+
#
|
7
|
+
# @param client_id [Integer] your RJMetrics Client ID
|
8
|
+
# @param api_key [String] your RJMetrics API Key
|
9
|
+
# @param timeout_in_seconds [Integer] seconds to wait for API responses
|
3
10
|
def initialize(client_id, api_key, timeout_in_seconds = 10)
|
4
|
-
@client = Client.new(client_id, api_key, timeout_in_seconds)
|
11
|
+
@client = RJMetrics::Client.new(client_id, api_key, timeout_in_seconds)
|
5
12
|
|
6
13
|
if not authenticated?
|
7
|
-
raise Client::UnableToConnectException, "Connection failed. Please double check your credentials."
|
14
|
+
raise RJMetrics::Client::UnableToConnectException, "Connection failed. Please double check your credentials."
|
8
15
|
end
|
9
16
|
end
|
10
17
|
|
@@ -17,10 +24,9 @@ class RJMetricsClient
|
|
17
24
|
#
|
18
25
|
# @param table_name [String] the table name you wish to store the data
|
19
26
|
# @param data [Hashamp] or Array of Hashmaps of data points that will get sent
|
27
|
+
# @param url [String] Import API url
|
20
28
|
# @return [Array] results of each request to RJMetrics Data Import API
|
21
|
-
def pushData(table_name, data)
|
22
|
-
@client.pushData(table_name, data)
|
29
|
+
def pushData(table_name, data, url = RJMetrics::Client::API_BASE)
|
30
|
+
@client.pushData(table_name, data, url)
|
23
31
|
end
|
24
32
|
end
|
25
|
-
|
26
|
-
require 'rjmetrics-client/client'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rjmetrics-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen Jones
|
@@ -14,56 +14,56 @@ dependencies:
|
|
14
14
|
name: rest-client
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 1.6.7
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.6.7
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: json
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.7.7
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 1.7.7
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>'
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>'
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: yard
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>'
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>'
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
description: RJMetrics Data Import API Client Library
|
@@ -72,8 +72,8 @@ executables: []
|
|
72
72
|
extensions: []
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
|
-
- lib/rjmetrics-client/client.rb
|
76
75
|
- lib/rjmetrics_client.rb
|
76
|
+
- lib/rjmetrics-client/client.rb
|
77
77
|
homepage: http://rjmetrics.com
|
78
78
|
licenses:
|
79
79
|
- Apache-2.0
|
@@ -84,18 +84,19 @@ require_paths:
|
|
84
84
|
- lib
|
85
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
91
|
requirements:
|
92
|
-
- -
|
92
|
+
- - '>='
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
96
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.0.3
|
98
98
|
signing_key:
|
99
99
|
specification_version: 4
|
100
100
|
summary: RJMetrics Data Import API Client Library
|
101
101
|
test_files: []
|
102
|
+
has_rdoc:
|