wiot-sdk 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2eb71783d82d2813cf8909bf9f6ce4ac8f9591e
4
- data.tar.gz: 6c9f22bcd220078cedd04bee1fa0ec2fef3a594f
3
+ metadata.gz: 64f41d47a05c9fac883320334b45cb4a006c5195
4
+ data.tar.gz: 02dec3deb466eb2e3fbfc4b0e9f43eb1ffedfdae
5
5
  SHA512:
6
- metadata.gz: e76ba9cd40b6272489993d3d4243fbf2003d08cd3a6b3c79c01cbe550355ccae900ef224bf2010c265cd7c9011482d1826ec08d367f965adffc7053263eb2464
7
- data.tar.gz: 5ccb98d28134b8e1acd29fcbd2a12c7e232e262f97121d5accb9960053baa0f00ecdf23a3918559fba4b66f690749ecd8deb491505fd2b6d7f42bedb9c15370f
6
+ metadata.gz: 78bc57cbb137ecaced9a56466c14db04fd4b58dbc9e6f26fc99757fb5be8bd33c00be0bea5040e6ef059372d3efb70f09956acd7674cf8aa7372ecd5c5f7b6b9
7
+ data.tar.gz: bb3fa4ed3086ab801085371de0645a45d6ccfdaaccdf221b53aa32e191ed76cc63b0526b6c1f23586055fd6c28ca494e7c001185b229ceb41f953c0f497a658c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wiot-sdk (0.1.1)
4
+ wiot-sdk (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -8,20 +8,20 @@ module WiotSdk
8
8
 
9
9
  class Client
10
10
 
11
- def self.init_yaml(file_path)
12
- api_key, space, project = WiotSdk::Config::ParserYml.parser file_path
11
+ def self.init_yaml(file_path, base_url)
12
+ username, api_key, space, project = WiotSdk::Config::ParserYml.parser file_path
13
13
 
14
- Request.new api_key, space, project
14
+ Request.new username, api_key, space, project, base_url
15
15
  end
16
16
 
17
- def self.init_json(file_path)
18
- api_key, space, project = WiotSdk::Config::ParserJson.parser file_path
17
+ def self.init_json(file_path, base_url)
18
+ username, api_key, space, project = WiotSdk::Config::ParserJson.parser file_path
19
19
 
20
- Request.new api_key, space, project
20
+ Request.new username, api_key, space, project, base_url
21
21
  end
22
22
 
23
- def self.init(api_key, space, project)
24
- Request.new api_key, space, project
23
+ def self.init(username, api_key, space, project, base_url)
24
+ Request.new username, api_key, space, project, base_url
25
25
  end
26
26
  end
27
27
 
@@ -0,0 +1,20 @@
1
+ require 'json'
2
+
3
+ module WiotSdk
4
+
5
+ class Payload
6
+ def initialize
7
+ @metric = {}
8
+ end
9
+
10
+ def addMetric(key, value)
11
+ @metric[key] = value
12
+ end
13
+
14
+ def to_s
15
+ payload = { metrics: @metric }
16
+ payload.to_json
17
+ end
18
+ end
19
+
20
+ end
@@ -1,34 +1,36 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
3
 
4
- require_relative 'metric'
4
+ require_relative 'payload'
5
5
  require_relative 'response'
6
6
 
7
7
  module WiotSdk
8
8
 
9
9
  class Request
10
10
 
11
- BASE_URL = 'http://api.watchiot.org/'
12
-
13
- def initialize(api_key, space, project)
11
+ def initialize(username, api_key, space, project, base_url)
12
+ @username = username
14
13
  @api_key = api_key
15
14
  @space = space
16
15
  @project = project
16
+ @base_url = base_url
17
17
  end
18
18
 
19
- def send(metric)
20
- uri = BASE_URL + @space + '/' + @project
21
- payload = '{ api: ' + @api_key + ', data: { ' + JSON.generate(metric.values).to_s + '}'
19
+ def send(payload)
20
+ uri = @base_url + '/' + @space + '/' + @project
22
21
 
23
- req uri, payload
22
+ req uri, payload.to_s
24
23
  rescue => ex
25
- Response.new nil, 500, ex.message
24
+ Response.new ex.response
26
25
  end
27
26
 
28
27
  private
29
28
 
30
29
  def req(url, payload)
31
- response = RestClient.post url, payload, content_type: 'json', accept: 'json'
30
+ response = RestClient.post url, payload,
31
+ Authorization: "#{@username} #{@api_key}",
32
+ content_type: 'json',
33
+ accept: 'json'
32
34
 
33
35
  Response.new response
34
36
  end
@@ -1,41 +1,43 @@
1
- require 'wiot-sdk/data/errors'
2
- require 'wiot-sdk/data/rate'
1
+ require 'json'
3
2
 
4
3
  module WiotSdk
5
4
 
6
5
  class Response
7
- def initialize(response, code = nil, message = nil)
8
- @code = code || response.code
9
- @rate = Rate.new response.headers
6
+ def initialize(response)
7
+ @rate_limit = parse_rate response.headers
10
8
 
11
- @message = message || parser_message(response.body)
12
- @errors = Errors.new response.body
9
+ response = JSON.parse(JSON.parse(response, :quirks_mode => true))
10
+
11
+ @code = response['code']
12
+ @msg = response['msg']
13
+ @error = response['error'] || nil
13
14
  end
14
15
 
15
16
  def code
16
17
  @code
17
18
  end
18
19
 
19
- def rate
20
- @rate
21
- end
22
-
23
- def message
24
- @message
20
+ def rate_limit
21
+ @rate_limit
25
22
  end
26
23
 
27
- def errors
28
- @errors
24
+ def msg
25
+ @msg
29
26
  end
30
27
 
31
- def has_errors?
32
- !@errors.nil? && @errors.has_errors?
28
+ def error
29
+ @error
33
30
  end
34
31
 
35
32
  private
36
33
 
37
- def parser_message(body)
38
- body
34
+ def parse_rate(headers)
35
+ {
36
+ limit: headers[:x_ratelimit_limit],
37
+ remaining: headers[:x_ratelimit_remaining],
38
+ reset: headers[:x_ratelimit_reset],
39
+ retry_after: headers[:x_retry_after] || nil
40
+ }
39
41
  end
40
42
  end
41
43
 
@@ -1,3 +1,3 @@
1
1
  module WiotSdk
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/wiot-sdk.rb CHANGED
@@ -1,18 +1,21 @@
1
1
  require 'wiot-sdk/client'
2
- require 'wiot-sdk/metric'
2
+ require 'wiot-sdk/payload'
3
+ require 'wiot-sdk/version'
3
4
 
4
5
  module WiotSdk
5
6
 
6
- def self.init(api_key, space, project)
7
- Client.init api_key, space, project
7
+ DEFAULT_BASE_URL = 'http://api.watchiot.com'
8
+
9
+ def self.init(username:, api_key:, space:, project:, base_url: DEFAULT_BASE_URL)
10
+ Client.init username, api_key, space, project, base_url
8
11
  end
9
12
 
10
- def self.init_yaml(file_path)
11
- Client.init_yaml file_path
13
+ def self.init_yaml(file_path:, base_url: DEFAULT_BASE_URL)
14
+ Client.init_yaml file_path, base_url
12
15
  end
13
16
 
14
- def self.init_json(file_path)
15
- Client.init_json file_path
17
+ def self.init_json(file_path:, base_url: DEFAULT_BASE_URL)
18
+ Client.init_json file_path, base_url
16
19
  end
17
20
 
18
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wiot-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - watchiot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-12 00:00:00.000000000 Z
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,10 +72,7 @@ files:
72
72
  - lib/wiot-sdk/client.rb
73
73
  - lib/wiot-sdk/config/parser_json.rb
74
74
  - lib/wiot-sdk/config/parser_yml.rb
75
- - lib/wiot-sdk/data/error.rb
76
- - lib/wiot-sdk/data/errors.rb
77
- - lib/wiot-sdk/data/rate.rb
78
- - lib/wiot-sdk/metric.rb
75
+ - lib/wiot-sdk/payload.rb
79
76
  - lib/wiot-sdk/request.rb
80
77
  - lib/wiot-sdk/response.rb
81
78
  - lib/wiot-sdk/version.rb
@@ -1,18 +0,0 @@
1
- module WiotSdk
2
- module Data
3
- class Error
4
- def initialize(field, msg)
5
- @field = field
6
- @msg = msg
7
- end
8
-
9
- def field
10
- @field
11
- end
12
-
13
- def msg
14
- @msg
15
- end
16
- end
17
- end
18
- end
@@ -1,23 +0,0 @@
1
- module WiotSdk
2
- module Data
3
- class Errors
4
- def initialize(body)
5
- @errors = parser_errors body
6
- end
7
-
8
- def errors
9
- @errors
10
- end
11
-
12
- def has_errors?
13
- !@errors.nil? && @errors.size > 0
14
- end
15
-
16
- private
17
-
18
- def parser_errors(body)
19
- []
20
- end
21
- end
22
- end
23
- end
@@ -1,29 +0,0 @@
1
- module WiotSdk
2
- module Data
3
- class Rate
4
- def initialize(header)
5
- parser_rate header
6
- end
7
-
8
- def rate_limit
9
- @rate_limit
10
- end
11
-
12
- def rate_remaining
13
- @rate_remaining
14
- end
15
-
16
- def rate_reset
17
- @rate_reset
18
- end
19
-
20
- private
21
-
22
- def parser_rate(header)
23
- @rate_limit = ''
24
- @rate_remaining = ''
25
- @rate_reset = ''
26
- end
27
- end
28
- end
29
- end
@@ -1,17 +0,0 @@
1
- module WiotSdk
2
-
3
- class Metric
4
- def initialize
5
- @metric = {}
6
- end
7
-
8
- def add(key, value)
9
- @metric[key] = value
10
- end
11
-
12
- def values
13
- @metric
14
- end
15
- end
16
-
17
- end