m2x 0.0.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/LICENSE +19 -0
- data/README.md +60 -0
- data/lib/m2x.rb +31 -0
- data/lib/m2x/client.rb +97 -0
- data/lib/m2x/feeds.rb +90 -0
- data/lib/m2x/keys.rb +59 -0
- data/m2x.gemspec +20 -0
- metadata +58 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2013 Leandro López and Matías Flores
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# AT&T's M2X Ruby Client
|
2
|
+
|
3
|
+
[AT&T’s M2X](https://m2x.att.com/) 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.
|
4
|
+
|
5
|
+
This gem aims to provide a simple wrapper to interact with [AT&T M2X API](https://m2x.att.com/developer/documentation/overview). Refer to the [Glossary of Terms](https://m2x.att.com/developer/documentation/glossary) to understand the nomenclature used through this documentation.
|
6
|
+
|
7
|
+
## Example Usage
|
8
|
+
|
9
|
+
In order to be able to use this gem you will need an [AT&T M2X](https://m2x.att.com/) API key and a Data Source ID. If you don't have an API key, create an account and, once registered and with your account activated, create a new [Data Source Blueprint](https://m2x.att.com/blueprints), and copy the `Feed ID` and `API Key` values. The following script will send your CPU load average to three different streams named `load_1m`, `load_5m` and `load_15`. Check that there's no need to create a stream in order to write values into it:
|
10
|
+
|
11
|
+
#! /usr/bin/env ruby
|
12
|
+
|
13
|
+
#
|
14
|
+
# See https://github.com/attm2x/m2x-ruby/blob/master/README.md#example-usage
|
15
|
+
# for instructions
|
16
|
+
#
|
17
|
+
|
18
|
+
require "m2x"
|
19
|
+
|
20
|
+
API_KEY = "<YOUR-FEED-API-KEY>"
|
21
|
+
FEED = "<YOUR-FEED-ID>"
|
22
|
+
|
23
|
+
m2x = M2X.new(API_KEY)
|
24
|
+
|
25
|
+
@run = true
|
26
|
+
trap(:INT) { @run = false }
|
27
|
+
|
28
|
+
# Match `uptime` load averages output for both Linux and OSX
|
29
|
+
UPTIME_RE = /(\d+\.\d+),? (\d+\.\d+),? (\d+\.\d+)$/
|
30
|
+
|
31
|
+
while @run
|
32
|
+
load_1m, load_5m, load_15m = `uptime`.match(UPTIME_RE).captures
|
33
|
+
|
34
|
+
# Write the different values into AT&T M2X
|
35
|
+
m2x.feeds.update_stream(FEED, "load_1m", value: load_1m)
|
36
|
+
m2x.feeds.update_stream(FEED, "load_5m", value: load_5m)
|
37
|
+
m2x.feeds.update_stream(FEED, "load_15m", value: load_15m)
|
38
|
+
|
39
|
+
sleep 1
|
40
|
+
end
|
41
|
+
|
42
|
+
puts
|
43
|
+
|
44
|
+
You can find the script in [`examples/m2x-uptime.rb`](examples/m2x-uptime.rb).
|
45
|
+
|
46
|
+
## Versioning
|
47
|
+
|
48
|
+
This gem aims to adhere to [Semantic Versioning 2.0.0](http://semver.org/). As a summary, given a version number `MAJOR.MINOR.PATCH`:
|
49
|
+
|
50
|
+
1. `MAJOR` will increment when backwards-incompatible changes are introduced to the client.
|
51
|
+
2. `MINOR` will increment when backwards-compatible functionality is added.
|
52
|
+
3. `PATCH` will increment with backwards-compatible bug fixes.
|
53
|
+
|
54
|
+
Additional labels for pre-release and build metadata are available as extensions to the `MAJOR.MINOR.PATCH` format.
|
55
|
+
|
56
|
+
**Note**: the client version does not necessarily reflect the version used in the AT&T M2X API.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
This gem is delivered under the MIT license. See [LICENSE](LICENSE) for the terms.
|
data/lib/m2x.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative "m2x/client"
|
2
|
+
require_relative "m2x/keys"
|
3
|
+
require_relative "m2x/feeds"
|
4
|
+
|
5
|
+
class M2X
|
6
|
+
VERSION = "0.0.1"
|
7
|
+
|
8
|
+
attr_reader :api_base
|
9
|
+
attr_reader :api_key
|
10
|
+
|
11
|
+
def initialize(api_key=nil, api_base=nil)
|
12
|
+
@api_base = api_base
|
13
|
+
@api_key = api_key
|
14
|
+
end
|
15
|
+
|
16
|
+
def client
|
17
|
+
@client ||= ::M2X::Client.new(@api_key, @api_base)
|
18
|
+
end
|
19
|
+
|
20
|
+
def status
|
21
|
+
client.get("/status")
|
22
|
+
end
|
23
|
+
|
24
|
+
def keys
|
25
|
+
@keys ||= ::M2X::Keys.new(client)
|
26
|
+
end
|
27
|
+
|
28
|
+
def feeds
|
29
|
+
@feeds ||= ::M2X::Feeds.new(client)
|
30
|
+
end
|
31
|
+
end
|
data/lib/m2x/client.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "json"
|
3
|
+
require "openssl"
|
4
|
+
|
5
|
+
class M2X
|
6
|
+
class Client
|
7
|
+
API_BASE = "http://api-m2x.att.com/v1".freeze
|
8
|
+
|
9
|
+
VERSION = "0.0.1".freeze
|
10
|
+
|
11
|
+
CA_FILE = File.expand_path("../cacert.pem", __FILE__)
|
12
|
+
|
13
|
+
USER_AGENT = "M2X/#{VERSION} (Ruby Net::HTTP)".freeze
|
14
|
+
|
15
|
+
def initialize(api_key=nil, api_base=nil)
|
16
|
+
@api_base = api_base
|
17
|
+
@api_key = api_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def request(verb, path, qs=nil, params=nil, headers=nil)
|
21
|
+
url = URI.parse(File.join(api_base, path))
|
22
|
+
|
23
|
+
url.query = URI.encode_www_form(qs) unless qs.nil? || qs.empty?
|
24
|
+
|
25
|
+
headers = default_headers.merge(headers || {})
|
26
|
+
|
27
|
+
body = if params
|
28
|
+
headers["Content-Type"] ||= "application/x-www-form-urlencoded"
|
29
|
+
|
30
|
+
case headers["Content-Type"]
|
31
|
+
when "application/json" then JSON.dump(params)
|
32
|
+
when "application/x-www-form-urlencoded" then URI.encode_www_form(params)
|
33
|
+
else
|
34
|
+
raise ArgumentError, "Unrecognized Content-Type `#{headers["Content-Type"]}`"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
options = {}
|
39
|
+
options.merge!(use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_PEER, ca_file: CA_FILE) if url.scheme == "https"
|
40
|
+
|
41
|
+
path = url.path
|
42
|
+
path << "?#{url.query}" if url.query
|
43
|
+
|
44
|
+
res = Net::HTTP.start(url.host, url.port, options) do |http|
|
45
|
+
http.send_request(verb.to_s.upcase, path, body, headers)
|
46
|
+
end
|
47
|
+
|
48
|
+
Response.new(res)
|
49
|
+
end
|
50
|
+
|
51
|
+
[:get, :post, :put, :delete, :head, :options, :patch].each do |verb|
|
52
|
+
define_method verb do |path, qs=nil, params=nil, headers=nil|
|
53
|
+
request(verb, path, qs, params, headers)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Response
|
58
|
+
attr_reader :response
|
59
|
+
|
60
|
+
def initialize(response)
|
61
|
+
@response = response
|
62
|
+
end
|
63
|
+
|
64
|
+
def raw
|
65
|
+
@response.body
|
66
|
+
end
|
67
|
+
|
68
|
+
def json
|
69
|
+
raise "#{@response.content_type} is not application/json" unless @response.content_type == "application/json"
|
70
|
+
|
71
|
+
@json ||= ::JSON.parse(raw)
|
72
|
+
end
|
73
|
+
|
74
|
+
def code
|
75
|
+
@code ||= @response.code.to_i
|
76
|
+
end
|
77
|
+
|
78
|
+
def headers
|
79
|
+
@headers ||= @response.to_hash
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def api_base
|
86
|
+
@api_base ||= API_BASE
|
87
|
+
end
|
88
|
+
|
89
|
+
def default_headers
|
90
|
+
@headers ||= begin
|
91
|
+
headers = { "User-Agent" => USER_AGENT }
|
92
|
+
headers = { "X-M2X-KEY" => @api_key } if @api_key
|
93
|
+
headers
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/lib/m2x/feeds.rb
ADDED
@@ -0,0 +1,90 @@
|
|
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
|
+
# List all the feeds that belong to the user associated with the
|
14
|
+
# M2X API key supplied when initializing M2X
|
15
|
+
def list
|
16
|
+
@client.get("/feeds")
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return the details of the supplied feed
|
20
|
+
def view(id)
|
21
|
+
@client.get("/feeds/#{URI.encode(id)}")
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return a list of access log to the supplied feed
|
25
|
+
def log(id)
|
26
|
+
@client.get("/feeds/#{URI.encode(id)}/log")
|
27
|
+
end
|
28
|
+
|
29
|
+
# Return the current location of the supplied feed
|
30
|
+
#
|
31
|
+
# Note that this method can return an empty value (response status
|
32
|
+
# of 204) if the feed has no location defined.
|
33
|
+
def location(id)
|
34
|
+
@client.get("/feeds/#{URI.encode(id)}/location")
|
35
|
+
end
|
36
|
+
|
37
|
+
# Update the current location of the feed
|
38
|
+
def update_location(id, params)
|
39
|
+
@client.put("/feeds/#{URI.encode(id)}/location", nil, params)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Return a list of the associated streams for the supplied feed
|
43
|
+
def streams(id)
|
44
|
+
@client.get("/feeds/#{URI.encode(id)}/streams")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return a list with the latest values from a stream
|
48
|
+
def stream_values(id, name)
|
49
|
+
@client.get("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}/values")
|
50
|
+
end
|
51
|
+
|
52
|
+
# Update stream's properties
|
53
|
+
#
|
54
|
+
# If the stream doesn't exist it will create it. See
|
55
|
+
# https://m2x.att.com/developer/documentation/feed#Create-Update-Data-Stream
|
56
|
+
# for details.
|
57
|
+
def update_stream(id, name, params)
|
58
|
+
@client.put("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}", {}, params)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Delete the stream (and all its values) from the feed
|
62
|
+
def delete_stream(id, name)
|
63
|
+
@client.delete("/feeds/#{URI.encode(id)}/streams/#{URI.encode(name)}")
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns a list of API keys associated with the feed
|
67
|
+
def keys(id)
|
68
|
+
@client.get("/keys", feed: id)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Creates a new API key associated to the feed
|
72
|
+
#
|
73
|
+
# If a parameter named `stream` is supplied with a stream name, it
|
74
|
+
# will create an API key associated with that stream only.
|
75
|
+
def create_key(id, params)
|
76
|
+
keys_api.create(params.merge(feed: id))
|
77
|
+
end
|
78
|
+
|
79
|
+
# Updates an API key properties
|
80
|
+
def update_key(id, key, params)
|
81
|
+
keys_api.update(key, params.merge(feed: id))
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def keys_api
|
87
|
+
@keys_api ||= ::M2X::Keys.new(@client)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/m2x/keys.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
class M2X
|
2
|
+
|
3
|
+
# Wrapper for AT&T M2X Keys API
|
4
|
+
#
|
5
|
+
# See https://m2x.att.com/developer/documentation/keys for AT&T M2X
|
6
|
+
# HTTP Keys API documentation.
|
7
|
+
class Keys
|
8
|
+
# Creates a new M2X Keys API wrapper
|
9
|
+
#
|
10
|
+
# See M2X::Client for a description of the client API.
|
11
|
+
def initialize(client)
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# List all the Master API Keys that belongs to the user associated
|
16
|
+
# with the AT&T M2X API key supplied when initializing M2X
|
17
|
+
def list
|
18
|
+
@client.get("/keys")
|
19
|
+
end
|
20
|
+
|
21
|
+
# Return the details of the API Key supplied
|
22
|
+
def view(key)
|
23
|
+
@client.get("/keys/#{URI.encode(key.to_s)}")
|
24
|
+
end
|
25
|
+
|
26
|
+
# Delete the supplied API Key
|
27
|
+
def delete(key)
|
28
|
+
@client.delete("/keys/#{URI.encode(key.to_s)}")
|
29
|
+
end
|
30
|
+
|
31
|
+
# Create a new API Key
|
32
|
+
#
|
33
|
+
# Note that, according to the parameters sent, you can create a
|
34
|
+
# Master API Key or a Feed/Stream API Key. See
|
35
|
+
# https://m2x.att.com/developer/documentation/keys#Create-Key for
|
36
|
+
# details on the parameters accepted by this method.
|
37
|
+
def create(params)
|
38
|
+
@client.post("/keys", nil, params, "Content-Type" => "application/json")
|
39
|
+
end
|
40
|
+
|
41
|
+
# Update API Key properties
|
42
|
+
#
|
43
|
+
# This method accepts the same parameters as create API Key and
|
44
|
+
# has the same validations. Note that the Key token cannot be
|
45
|
+
# updated through this method.
|
46
|
+
def update(key, params)
|
47
|
+
@client.put("/keys/#{URI.encode(key.to_s)}", nil, params, "Content-Type" => "application/json")
|
48
|
+
end
|
49
|
+
|
50
|
+
# Regenerate an API Key token
|
51
|
+
#
|
52
|
+
# Note that if you regenerate the key that you're using for
|
53
|
+
# authentication then you would need to change your scripts to
|
54
|
+
# start using the new key token for all subsequent requests.
|
55
|
+
def regenerate(key)
|
56
|
+
@client.post("/keys/#{URI.encode(key.to_s)}/regenerate", nil, {})
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/m2x.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "m2x"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.summary = "Ruby client for AT&T M2X"
|
7
|
+
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."
|
8
|
+
s.authors = ["Leandro López", "Matías Flores"]
|
9
|
+
s.email = ["inkel.ar@gmail.com", "flores.matias@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/attm2x/m2x-ruby"
|
11
|
+
s.licenses = ["MIT"]
|
12
|
+
|
13
|
+
s.files = Dir[
|
14
|
+
"LICENSE",
|
15
|
+
"README.md",
|
16
|
+
"lib/**/*.rb",
|
17
|
+
"*.gemspec",
|
18
|
+
"test/*.*"
|
19
|
+
]
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: m2x
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leandro López
|
9
|
+
- Matías Flores
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: AT&T’s M2X is a cloud-based fully managed data storage service for network
|
16
|
+
connected machine-to-machine (M2M) devices. From trucks and turbines to vending
|
17
|
+
machines and freight containers, M2X enables the devices that power your business
|
18
|
+
to connect and share valuable data.
|
19
|
+
email:
|
20
|
+
- inkel.ar@gmail.com
|
21
|
+
- flores.matias@gmail.com
|
22
|
+
executables: []
|
23
|
+
extensions: []
|
24
|
+
extra_rdoc_files: []
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- lib/m2x/client.rb
|
29
|
+
- lib/m2x/feeds.rb
|
30
|
+
- lib/m2x/keys.rb
|
31
|
+
- lib/m2x.rb
|
32
|
+
- m2x.gemspec
|
33
|
+
homepage: http://github.com/attm2x/m2x-ruby
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.23
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Ruby client for AT&T M2X
|
58
|
+
test_files: []
|