rjmetrics-client 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGM2ZDMwYjRlZTljZWUzZTllMzdjMmVlMDIzNTc5NzdlNDYzMDRlYw==
5
+ data.tar.gz: !binary |-
6
+ YzMzMmFiNTIzYzZjNGNhYWNlNjBhN2M3NzY0YTJkYzVkM2U5ZTg3OA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjQ3NzhmNjkxMzUwMjBjODQ4MzJkODM3NWM3Mzc0MWJmMjJlY2M2ZThiOTA4
10
+ NDg0NGEyYTk5M2QyNmQ1MzA5YWEyNzJkYTA2NzA2NjE1OTZlNjI1ODAzYTc4
11
+ YTAzODY4ZTg3ZmYwMWNlMzlkMGY2YTdjZTNmYjlkNTY2NTRmYTk=
12
+ data.tar.gz: !binary |-
13
+ M2E3ZTViMjZmMzAxNTIxOTY4MTVlMDU5NzM1MDcwNDkwNTdkOGVlMDA4Nzdl
14
+ ZGNlZjBmOWI0NDNmZGJmMjAwYjQ3MTI1N2NmNWEwMWYyZmI5MTlkNjM4YzEz
15
+ OTI4YzI1OGNhYmUwMGNjNTAwMGNjZGJjMTM2NTdiNzZjYjY5OTc=
@@ -0,0 +1,105 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ class Client
5
+
6
+ # default RJMetrics Data Import API url
7
+ API_BASE = "https://connect.rjmetrics.com/v2"
8
+ # RJMetrics Sandbox API url
9
+ SANDBOX_BASE = "https://sandbox-connect.rjmetrics.com/v2"
10
+
11
+ # Constructs a Client instance if it receives valid arguments or will raise an ArgumentError.
12
+ #
13
+ # @param client_id [Integer] your RJMetrics Client ID
14
+ # @param api_key [String] your RJMetrics API Key
15
+ # @param timeout_in_seconds [Integer] seconds to wait for API responses or nil
16
+ def initialize(client_id, api_key, timeout_in_seconds = 10)
17
+ validateConstructorArgs(client_id, api_key, timeout_in_seconds)
18
+ @client_id = client_id
19
+ @api_key = api_key
20
+ @timeout_in_seconds = timeout_in_seconds
21
+ end
22
+
23
+ # Checks if the provided Client ID and API Key are valid credentials by requestin from the RJMetrics API Sandbox.
24
+ def authenticated?
25
+ test_data = {:keys => [:id], :id => 1}
26
+ begin
27
+ pushData("test", test_data, SANDBOX_BASE)
28
+ rescue InvalidRequestException
29
+ return false
30
+ end
31
+ return true
32
+ end
33
+
34
+ # Sends data to RJMetrics Data Import API.
35
+ #
36
+ # @param table_name [String] the table name you wish to store the data
37
+ # @param data [Hashamp] or Array of Hashmaps of data points that will get sent
38
+ # @param url [String] Import API url or nil
39
+ # @return [Array] results of each request to RJMetrics Data Import API
40
+ def pushData(table_name, data, url = API_BASE)
41
+ validatePushDataArgs(table_name, data, url)
42
+
43
+ if not data.is_a? Array
44
+ data = Array.[](data)
45
+ end
46
+
47
+ return data.map{ |data_point| makePushDataAPICall(table_name, data_point, url) }
48
+ end
49
+
50
+ private
51
+
52
+ def validateConstructorArgs(client_id, api_key, timeout_in_seconds)
53
+ if not client_id.is_a? Integer or client_id <= 0
54
+ raise ArgumentError, "Invalid client ID: #{client_id} -- must be a positive integer."
55
+ end
56
+
57
+ if not timeout_in_seconds.is_a? Integer or timeout_in_seconds <= 0
58
+ raise ArgumentError, "Invalid timeout: #{timeout_in_seconds} -- must be a positive integer."
59
+ end
60
+
61
+ if not api_key.is_a? String
62
+ raise ArgumentError, "Invalid API key: #{api_key} -- must be a string."
63
+ end
64
+ end
65
+
66
+ def validatePushDataArgs(table_name, data, url)
67
+ if not data.is_a? Hash and not data.is_a? Array
68
+ raise ArgumentError, "Invalid data -- must be a valid Ruby Hash or Array."
69
+ end
70
+
71
+ if not table_name.is_a? String
72
+ raise ArgumentError, "Invalid table name: '#{table_name}' -- must be a string."
73
+ end
74
+
75
+ if not url.is_a? String
76
+ raise ArgumentError, "Invalid url: '#{url}' -- must be a string."
77
+ end
78
+ end
79
+
80
+ def makePushDataAPICall(table_name, data, url = API_BASE)
81
+ request_url = "#{url}/client/#{@client_id}/table/#{table_name}/data?apikey=#{@api_key}"
82
+
83
+ begin
84
+ response = RestClient.post(
85
+ request_url,
86
+ data.to_json,
87
+ {
88
+ :content_type => :json,
89
+ :accept => :json,
90
+ :timeout => @timeout_in_seconds
91
+ }
92
+ )
93
+ return response
94
+ rescue RestClient::Exception => error
95
+ response = JSON.parse(error.response)
96
+ raise InvalidRequestException,
97
+ "The Import API returned: #{response['code']} #{response['message']}. Reasons: #{response['reasons']}"
98
+ end
99
+ end
100
+
101
+ class UnableToConnectException < RuntimeError
102
+ end
103
+ class InvalidRequestException < RuntimeError
104
+ end
105
+ end
@@ -0,0 +1,21 @@
1
+ class RJMetricsClient
2
+
3
+ def initialize(client_id, api_key, timeout_in_seconds = 10)
4
+ @client = Client.new(client_id, api_key, timeout_in_seconds)
5
+
6
+ if not @client.authenticated?
7
+ raise Client::UnableToConnectException, "Connection failed. Please double check your credentials."
8
+ end
9
+ end
10
+
11
+ # Sends data to RJMetrics Data Import API.
12
+ #
13
+ # @param table_name [String] the table name you wish to store the data
14
+ # @param data [Hashamp] or Array of Hashmaps of data points that will get sent
15
+ # @return [Array] results of each request to RJMetrics Data Import API
16
+ def pushData(table_name, data)
17
+ @client.pushData(table_name, data)
18
+ end
19
+ end
20
+
21
+ require 'rjmetrics-client/client'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rjmetrics-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Owen Jones
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>'
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>'
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>'
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>'
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: RJMetrics Data Import API Client Library
70
+ email: ojones@rjmetrics.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/rjmetrics_client.rb
76
+ - lib/rjmetrics-client/client.rb
77
+ homepage: http://rjmetrics.com
78
+ licenses:
79
+ - Apache-2.0
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.7
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: RJMetrics Data Import API Client Library
101
+ test_files: []