m2x 1.0.0 → 2.0.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.
@@ -0,0 +1,42 @@
1
+ # Response wrapper for M2X client
2
+ class M2X::Client::Response
3
+ attr_reader :response
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ end
8
+
9
+ def raw
10
+ @response.body
11
+ end
12
+
13
+ def json
14
+ raise "#{@response.content_type} is not application/json" unless @response.content_type == "application/json"
15
+
16
+ @json ||= ::JSON.parse(raw)
17
+ end
18
+
19
+ def status
20
+ @status ||= @response.code.to_i
21
+ end
22
+
23
+ def headers
24
+ @headers ||= @response.to_hash
25
+ end
26
+
27
+ def success?
28
+ (200..299).include?(status)
29
+ end
30
+
31
+ def client_error?
32
+ (400..499).include?(status)
33
+ end
34
+
35
+ def server_error?
36
+ (500..599).include?(status)
37
+ end
38
+
39
+ def error?
40
+ client_error? || server_error?
41
+ end
42
+ end
data/lib/m2x/stream.rb ADDED
@@ -0,0 +1,111 @@
1
+ # Wrapper for AT&T M2X Data Streams API
2
+ # https://m2x.att.com/developer/documentation/v2/device
3
+ class M2X::Client::Stream < M2X::Client::Resource
4
+
5
+ class << self
6
+ # Get details of a specific data Stream associated with a device
7
+ #
8
+ # https://m2x.att.com/developer/documentation/v2/device#View-Data-Stream
9
+ def fetch(client, device, name)
10
+ res = client.get("#{device.path}/streams/#{name}")
11
+
12
+ new(client, device, res.json) if res.success?
13
+ end
14
+
15
+ # Retrieve list of data streams associated with a device.
16
+ #
17
+ # https://m2x.att.com/developer/documentation/v2/device#List-Data-Streams
18
+ def list(client, device)
19
+ res = client.get("#{device.path}/streams")
20
+
21
+ res.json["streams"].map{ |atts| new(client, device, atts) } if res.success?
22
+ end
23
+ end
24
+
25
+ def initialize(client, device, attributes)
26
+ @client = client
27
+ @device = device
28
+ @attributes = attributes
29
+ end
30
+
31
+ def path
32
+ @path ||= "#{@device.path}/streams/#{ URI.encode(@attributes.fetch("name")) }"
33
+ end
34
+
35
+ # Update stream properties
36
+ # (if the stream does not exist it gets created).
37
+ #
38
+ # https://m2x.att.com/developer/documentation/v2/device#Create-Update-Data-Stream
39
+ def update!(params)
40
+ res = @client.put(path, {}, params, "Content-Type" => "application/json")
41
+
42
+ @attributes = res.json if res.status == 201
43
+ end
44
+
45
+ # List values from the stream, sorted in reverse chronological order
46
+ # (most recent values first).
47
+ #
48
+ # https://m2x.att.com/developer/documentation/v2/device#List-Data-Stream-Values
49
+ def values(params={})
50
+ @client.get("#{path}/values", params)
51
+ end
52
+
53
+ # Sample values from the stream, sorted in reverse chronological order
54
+ # (most recent values first).
55
+ #
56
+ # This method only works for numeric streams
57
+ #
58
+ # https://m2x.att.com/developer/documentation/v2/device#Data-Stream-Sampling
59
+ def sampling(params)
60
+ @client.get("#{path}/sampling", params)
61
+ end
62
+
63
+ # Return count, min, max, average and standard deviation stats for the
64
+ # values of the stream.
65
+ #
66
+ # This method only works for numeric streams
67
+ #
68
+ # https://m2x.att.com/developer/documentation/v2/device#Data-Stream-Stats
69
+ def stats(params={})
70
+ @client.get("#{path}/stats", params)
71
+ end
72
+
73
+ # Update the current value of the stream. The timestamp
74
+ # is optional. If ommited, the current server time will be used
75
+ #
76
+ # https://m2x.att.com/developer/documentation/v2/device#Update-Data-Stream-Value
77
+ def update_value(value, timestamp=nil)
78
+ params = { value: value }
79
+
80
+ params[:at] = timestamp if timestamp
81
+
82
+ @client.put("#{path}/value", nil, params, "Content-Type" => "application/json")
83
+ end
84
+
85
+ # Post multiple values to the stream
86
+ #
87
+ # The `values` parameter is an array with the following format:
88
+ #
89
+ # [
90
+ # { "timestamp": <Time in ISO8601>, "value": x },
91
+ # { "timestamp": <Time in ISO8601>, "value": y },
92
+ # [ ... ]
93
+ # ]
94
+ #
95
+ # https://m2x.att.com/developer/documentation/v2/device#Post-Data-Stream-Values
96
+ def post_values(values)
97
+ params = { values: values }
98
+
99
+ @client.post("#{path}/values", nil, params, "Content-Type" => "application/json")
100
+ end
101
+
102
+ # Delete values in a stream by a date range
103
+ # The `start` and `stop` parameters should be ISO8601 timestamps
104
+ #
105
+ # https://m2x.com/developer/documentation/v2/device#Delete-Data-Stream-Values
106
+ def delete_values!(start, stop)
107
+ params = { from: start, end: stop }
108
+
109
+ @client.delete("#{path}/values", nil, params, "Content-Type" => "application/json")
110
+ end
111
+ end
data/lib/m2x/version.rb CHANGED
@@ -1,3 +1,5 @@
1
- class M2X
2
- VERSION = "1.0.0"
1
+ module M2X
2
+ class Client
3
+ VERSION = "2.0.0"
4
+ end
3
5
  end
data/lib/m2x.rb CHANGED
@@ -1,45 +1,10 @@
1
+ module M2X; end
2
+
1
3
  require_relative "m2x/version"
4
+ require_relative "m2x/response"
2
5
  require_relative "m2x/client"
3
- require_relative "m2x/keys"
4
- require_relative "m2x/feeds"
5
- require_relative "m2x/batches"
6
- require_relative "m2x/blueprints"
7
- require_relative "m2x/datasources"
8
-
9
- class M2X
10
- attr_reader :api_base
11
- attr_reader :api_key
12
-
13
- def initialize(api_key=nil, api_base=nil)
14
- @api_base = api_base
15
- @api_key = api_key
16
- end
17
-
18
- def client
19
- @client ||= ::M2X::Client.new(@api_key, @api_base)
20
- end
21
-
22
- def status
23
- client.get("/status")
24
- end
25
-
26
- def keys
27
- @keys ||= ::M2X::Keys.new(client)
28
- end
29
-
30
- def feeds
31
- @feeds ||= ::M2X::Feeds.new(client)
32
- end
33
-
34
- def blueprints
35
- @blueprints ||= ::M2X::Blueprints.new(client)
36
- end
37
-
38
- def datasources
39
- @datasources ||= ::M2X::Datasources.new(client)
40
- end
41
-
42
- def batches
43
- @batches ||= ::M2X::Batches.new(client)
44
- end
45
- end
6
+ require_relative "m2x/resource"
7
+ require_relative "m2x/key"
8
+ require_relative "m2x/device"
9
+ require_relative "m2x/stream"
10
+ require_relative "m2x/distribution"
data/m2x.gemspec CHANGED
@@ -4,7 +4,7 @@ require "./lib/m2x/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "m2x"
7
- s.version = ::M2X::VERSION
7
+ s.version = ::M2X::Client::VERSION
8
8
  s.summary = "Ruby client for AT&T M2X"
9
9
  s.description = "AT&T’s M2X is a cloud-based fully managed data storage service for network connected machine-to-machine (M2M) devices. From trucks and turbines to vending machines and freight containers, M2X enables the devices that power your business to connect and share valuable data."
10
10
  s.authors = ["Leandro López", "Matías Flores", "Federico Saravia"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m2x
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro López
@@ -27,12 +27,13 @@ files:
27
27
  - LICENSE
28
28
  - README.md
29
29
  - lib/m2x.rb
30
- - lib/m2x/batches.rb
31
- - lib/m2x/blueprints.rb
32
30
  - lib/m2x/client.rb
33
- - lib/m2x/datasources.rb
34
- - lib/m2x/feeds.rb
35
- - lib/m2x/keys.rb
31
+ - lib/m2x/device.rb
32
+ - lib/m2x/distribution.rb
33
+ - lib/m2x/key.rb
34
+ - lib/m2x/resource.rb
35
+ - lib/m2x/response.rb
36
+ - lib/m2x/stream.rb
36
37
  - lib/m2x/version.rb
37
38
  - lib/megatest.rb
38
39
  - m2x.gemspec
data/lib/m2x/batches.rb DELETED
@@ -1,79 +0,0 @@
1
- class M2X
2
-
3
- # Wrapper for AT&T M2X Batches API
4
- #
5
- # See https://m2x.att.com/developer/documentation/datasource
6
- class Batches
7
- # Creates a new M2X Batches API Wrapper
8
- def initialize(client)
9
- @client = client
10
- end
11
-
12
- # List/search all the data source batches that belong to the user
13
- # associated with the M2X API key supplied when initializing M2X
14
- #
15
- # The list of data source batches can be filtered by using one or
16
- # more of the following optional parameters:
17
- #
18
- # * `q` text to search, matching the name and description.
19
- # * `tags` a comma separated list of tags.
20
- # * `limit` how many results per page.
21
- # * `page` the specific results page, starting by 1.
22
- # * `latitude` and `longitude` for searching feeds geographically.
23
- # * `distance` numeric value in `distance_unit`.
24
- # * `distance_unit` either `miles`, `mi` or `km`.
25
- def list(params={})
26
- @client.get("/batches", params)
27
- end
28
- alias_method :search, :list
29
-
30
- # Create a new data source batch
31
- #
32
- # Accepts the following parameters as members of a hash:
33
- #
34
- # * `name` the name of the new data source.
35
- # * `visibility` either "public" or "private".
36
- # * `description` containing a longer description (optional).
37
- # * `tags` a comma separated string of tags (optional).
38
- def create(params={})
39
- @client.post("/batches", nil, params)
40
- end
41
-
42
- # Retrieve information about an existing data source batch
43
- def view(id)
44
- @client.get("/batches/#{URI.encode(id)}")
45
- end
46
-
47
- # Update an existing data source batch details
48
- #
49
- # Accepts the following parameters as members of a hash:
50
- #
51
- # * `name` the name of the new data source.
52
- # * `visibility` either "public" or "private".
53
- # * `description` containing a longer description (optional).
54
- # * `tags` a comma separated string of tags (optional).
55
- def update(id, params={})
56
- @client.put("/batches/#{URI.encode(id)}", nil, params)
57
- end
58
-
59
- # List/search all data sources in the batch
60
- #
61
- # See Datasources#search for search parameters description.
62
- def datasources(id, params={})
63
- @client.get("/batches/#{URI.encode(id)}/datasources", params)
64
- end
65
-
66
- # Add a new data source to an existing batch
67
- #
68
- # Accepts a `serial` parameter, that must be a unique identifier
69
- # within this batch.
70
- def add_datasource(id, serial)
71
- @client.post("/batches/#{URI.encode(id)}/datasources", nil, { serial: serial })
72
- end
73
-
74
- # Delete an existing data source batch
75
- def delete(id)
76
- @client.delete("/batches/#{URI.encode(id)}")
77
- end
78
- end
79
- end
@@ -1,64 +0,0 @@
1
- class M2X
2
-
3
- # Wrapper for AT&T M2X Blueprints API
4
- #
5
- # See https://m2x.att.com/developer/documentation/datasource
6
- class Blueprints
7
- # Creates a new M2X Blueprints API Wrapper
8
- def initialize(client)
9
- @client = client
10
- end
11
-
12
- # List/search all the blueprints that belong to the user associated
13
- # with the M2X API key supplied when initializing M2X
14
- #
15
- # The list of blueprints can be filtered by using one or more of the
16
- # following optional parameters:
17
- #
18
- # * `q` text to search, matching the name and description.
19
- # * `tags` a comma separated list of tags.
20
- # * `limit` how many results per page.
21
- # * `page` the specific results page, starting by 1.
22
- # * `latitude` and `longitude` for searching feeds geographically.
23
- # * `distance` numeric value in `distance_unit`.
24
- # * `distance_unit` either `miles`, `mi` or `km`.
25
- def list(params={})
26
- @client.get("/blueprints", params)
27
- end
28
- alias_method :search, :list
29
-
30
- # Create a new data source blueprint
31
- #
32
- # Accepts the following parameters as members of a hash:
33
- #
34
- # * `name` the name of the new data source blueprint.
35
- # * `visibility` either "public" or "private".
36
- # * `description` containing a longer description (optional).
37
- # * `tags` a comma separated string of tags (optional).
38
- def create(params={})
39
- @client.post("/blueprints", nil, params)
40
- end
41
-
42
- # Retrieve information about an existing data source blueprint
43
- def view(id)
44
- @client.get("/blueprints/#{URI.encode(id)}")
45
- end
46
-
47
- # Update an existing data source blueprint's information
48
- #
49
- # Accepts the following parameters as members of a hash:
50
- #
51
- # * `name` the name of the new data source blueprint.
52
- # * `visibility` either "public" or "private".
53
- # * `description` containing a longer description (optional).
54
- # * `tags` a comma separated string of tags (optional).
55
- def update(id, params={})
56
- @client.put("/blueprints/#{URI.encode(id)}", nil, params)
57
- end
58
-
59
- # Delete an existing data source blueprint
60
- def delete(id)
61
- @client.delete("/blueprints/#{URI.encode(id)}")
62
- end
63
- end
64
- end
@@ -1,64 +0,0 @@
1
- class M2X
2
-
3
- # Wrapper for AT&T M2X Data Sources API
4
- #
5
- # See https://m2x.att.com/developer/documentation/datasource
6
- class Datasources
7
- # Creates a new M2X Data Sources API Wrapper
8
- def initialize(client)
9
- @client = client
10
- end
11
-
12
- # List/search all the datasources that belong to the user associated
13
- # with the M2X API key supplied when initializing M2X
14
- #
15
- # The list of data sources can be filtered by using one or more of the
16
- # following optional parameters:
17
- #
18
- # * `q` text to search, matching the name and description.
19
- # * `tags` a comma separated list of tags.
20
- # * `limit` how many results per page.
21
- # * `page` the specific results page, starting by 1.
22
- # * `latitude` and `longitude` for searching feeds geographically.
23
- # * `distance` numeric value in `distance_unit`.
24
- # * `distance_unit` either `miles`, `mi` or `km`.
25
- def list(params={})
26
- @client.get("/datasources", params)
27
- end
28
- alias_method :search, :list
29
-
30
- # Create a new data source
31
- #
32
- # Accepts the following parameters as members of a hash:
33
- #
34
- # * `name` the name of the new data source.
35
- # * `visibility` either "public" or "private".
36
- # * `description` containing a longer description (optional).
37
- # * `tags` a comma separated string of tags (optional).
38
- def create(params={})
39
- @client.post("/datasources", nil, params)
40
- end
41
-
42
- # Retrieve information about an existing data source
43
- def view(id)
44
- @client.get("/datasources/#{URI.encode(id)}")
45
- end
46
-
47
- # Update an existing data source details
48
- #
49
- # Accepts the following parameters as members of a hash:
50
- #
51
- # * `name` the name of the new data source.
52
- # * `visibility` either "public" or "private".
53
- # * `description` containing a longer description (optional).
54
- # * `tags` a comma separated string of tags (optional).
55
- def update(id, params={})
56
- @client.put("/datasources/#{URI.encode(id)}", nil, params)
57
- end
58
-
59
- # Delete an existing data source
60
- def delete(id)
61
- @client.delete("/datasources/#{URI.encode(id)}")
62
- end
63
- end
64
- end
data/lib/m2x/feeds.rb DELETED
@@ -1,194 +0,0 @@
1
- class M2X
2
-
3
- # Wrapper for AT&T M2X Feed API
4
- #
5
- # See https://m2x.att.com/developer/documentation/feed for AT&T M2X
6
- # HTTP Feed API documentation.
7
- class Feeds
8
- # Creates a new M2X Feed API Wrapper
9
- def initialize(client)
10
- @client = client
11
- end
12
-
13
- # Search the catalog of public feeds. This allows unauthenticated
14
- # users to search feeds from other users that has been marked as
15
- # public, allowing only to read their metadata, locations, list
16
- # its streams, view each stream metadata and its values.
17
- #
18
- # Refer to the feed documentation for the full list of supported parameters
19
- def catalog(params={})
20
- @client.get("/feeds/catalog", params)
21
- end
22
-
23
- # List/search all the feeds that belong to the user associated
24
- # with the M2X API key supplied when initializing M2X
25
- #
26
- # Refer to the feed documentation for the full list of supported parameters
27
- def list(params={})
28
- @client.get("/feeds", params)
29
- end
30
- alias_method :search, :list
31
-
32
- # Return the details of the supplied feed
33
- def view(id)
34
- @client.get("/feeds/#{URI.encode(id)}")
35
- end
36
-
37
- # Return a list of access log to the supplied feed
38
- def log(id)
39
- @client.get("/feeds/#{URI.encode(id)}/log")
40
- end
41
-
42
- # Return the current location of the supplied feed
43
- #
44
- # Note that this method can return an empty value (response status
45
- # of 204) if the feed has no location defined.
46
- def location(id)
47
- @client.get("/feeds/#{URI.encode(id)}/location")
48
- end
49
-
50
- # Update the current location of the feed
51
- def update_location(id, params)
52
- @client.put("/feeds/#{URI.encode(id)}/location", nil, params)
53
- end
54
-
55
- # Return a list of the associated streams for the supplied feed
56
- def streams(id)
57
- @client.get("/feeds/#{URI.encode(id)}/streams")
58
- end
59
-
60
- # Return the details of the supplied stream
61
- def stream(id, name)
62
- @client.get("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}")
63
- end
64
-
65
- # Update stream's properties
66
- #
67
- # If the stream doesn't exist it will create it. See
68
- # https://m2x.att.com/developer/documentation/feed#Create-Update-Data-Stream
69
- # for details.
70
- def update_stream(id, name, params={})
71
- @client.put("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}", {}, params)
72
- end
73
-
74
- # Delete the stream (and all its values) from the feed
75
- def delete_stream(id, name)
76
- @client.delete("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}")
77
- end
78
-
79
- # List values from an existing data stream associated with a
80
- # specific feed, sorted in reverse chronological order (most
81
- # recent values first).
82
- #
83
- # The values can be filtered by using one or more of the following
84
- # optional parameters:
85
- #
86
- # * `start` An ISO 8601 timestamp specifying the start of the date
87
- # * range to be considered.
88
- #
89
- # * `end` An ISO 8601 timestamp specifying the end of the date
90
- # * range to be considered.
91
- #
92
- # * `limit` Maximum number of values to return.
93
- def stream_values(id, name, params={})
94
- @client.get("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/values", params)
95
- end
96
-
97
- # Sample values from an existing data stream associated with a specific
98
- # feed, sorted in reverse chronological order (most recent values first).
99
- #
100
- # This method only works for numeric streams
101
- #
102
- # Refer to the sampling endpoint documentation for allowed parameters
103
- def stream_sampling(id, name, params={})
104
- @client.get("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/sampling", params)
105
- end
106
-
107
- # Return count, min, max, average and standard deviation stats for the
108
- # values on an existing data stream.
109
- #
110
- # This method only works for numeric streams
111
- #
112
- # Refer to the stats endpoint documentation for allowed parameters
113
- def stream_stats(id, name, params={})
114
- @client.get("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/stats", params)
115
- end
116
-
117
- # Update the current value of the specified stream. The timestamp
118
- # is optional. If ommited, the current server time will be used
119
- def update_stream_value(id, name, value, timestamp=nil)
120
- params = { value: value }
121
-
122
- params[:at] = timestamp if timestamp
123
-
124
- @client.put("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/value", nil, params, "Content-Type" => "application/json")
125
- end
126
-
127
- # Post multiple values to a single stream
128
- #
129
- # This method allows posting multiple values to a stream
130
- # belonging to a feed. The stream should be created before
131
- # posting values using this method. The `values` parameter is a
132
- # hash with the following format:
133
- #
134
- # {
135
- # { "at": <Time in ISO8601>, "value": x },
136
- # { "at": <Time in ISO8601>, "value": y },
137
- # [ ... ]
138
- # }
139
- def post_stream_values(id, name, values)
140
- params = { values: values }
141
- @client.post("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/values", nil, params, "Content-Type" => "application/json")
142
- end
143
-
144
- # Delete values in a stream by a date range
145
- # The `start` and `stop` parameters should be ISO8601 timestamps
146
- def delete_stream_values(id, name, start, stop)
147
- params = { from: start, end: stop }
148
- @client.delete("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/values", nil, params, "Content-Type" => "application/json")
149
- end
150
-
151
- # Post multiple values to multiple streams
152
- #
153
- # This method allows posting multiple values to multiple streams
154
- # belonging to a feed. All the streams should be created before
155
- # posting values using this method. The `values` parameters is a
156
- # hash with the following format:
157
- #
158
- # {
159
- # "stream-name-1": [
160
- # { "at": <Time in ISO8601>, "value": x },
161
- # { "at": <Time in ISO8601>, "value": y }
162
- # ],
163
- # "stream-name-2": [ ... ]
164
- # }
165
- def post_multiple(id, values)
166
- params = { values: values }
167
- @client.post("/feeds/#{URI.encode(id)}", nil, params, "Content-Type" => "application/json")
168
- end
169
-
170
- # Returns a list of API keys associated with the feed
171
- def keys(id)
172
- @client.get("/keys", feed: id)
173
- end
174
-
175
- # Creates a new API key associated to the feed
176
- #
177
- # If a parameter named `stream` is supplied with a stream name, it
178
- # will create an API key associated with that stream only.
179
- def create_key(id, params)
180
- keys_api.create(params.merge(feed: id))
181
- end
182
-
183
- # Updates an API key properties
184
- def update_key(id, key, params)
185
- keys_api.update(key, params.merge(feed: id))
186
- end
187
-
188
- private
189
-
190
- def keys_api
191
- @keys_api ||= ::M2X::Keys.new(@client)
192
- end
193
- end
194
- end