insnergy-api-ruby-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f91ffd9749ab96e698db3dbf0d1d13608150644
4
+ data.tar.gz: 67a96602b572ef1555d8d1959f26910490c98c38
5
+ SHA512:
6
+ metadata.gz: a6e8b8f748c0cb6ed1e21cba4598a005e1fbe92c6040e21ea0df3072b55a84af0fc08c0ddf87fc5ae80389bdc88b140bbc445f33bf0cf0e3ee5b33265b13197c
7
+ data.tar.gz: c72ef65aadd3b1df1698cd96cc7e486a527396338feb56b21669bcf1009359c74b3ff877289168018879dd091a98c19b56229083b24b2564adb57364fb690829
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "juwelier", "~> 2.1.0"
13
+ gem "simplecov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activesupport (5.0.0.1)
5
+ concurrent-ruby (~> 1.0, >= 1.0.2)
6
+ i18n (~> 0.7)
7
+ minitest (~> 5.1)
8
+ tzinfo (~> 1.1)
9
+ addressable (2.4.0)
10
+ builder (3.2.2)
11
+ concurrent-ruby (1.0.2)
12
+ descendants_tracker (0.0.4)
13
+ thread_safe (~> 0.3, >= 0.3.1)
14
+ docile (1.1.5)
15
+ faraday (0.9.2)
16
+ multipart-post (>= 1.2, < 3)
17
+ git (1.3.0)
18
+ github_api (0.14.5)
19
+ addressable (~> 2.4.0)
20
+ descendants_tracker (~> 0.0.4)
21
+ faraday (~> 0.8, < 0.10)
22
+ hashie (>= 3.4)
23
+ oauth2 (~> 1.0)
24
+ hashie (3.4.4)
25
+ highline (1.7.8)
26
+ i18n (0.7.0)
27
+ json (1.8.3)
28
+ juwelier (2.1.2)
29
+ builder
30
+ bundler (>= 1.0)
31
+ git (>= 1.2.5)
32
+ github_api
33
+ highline (>= 1.6.15)
34
+ nokogiri (>= 1.5.10)
35
+ rake
36
+ rdoc
37
+ semver
38
+ jwt (1.5.4)
39
+ mini_portile2 (2.1.0)
40
+ minitest (5.9.0)
41
+ multi_json (1.12.1)
42
+ multi_xml (0.5.5)
43
+ multipart-post (2.0.0)
44
+ nokogiri (1.6.8)
45
+ mini_portile2 (~> 2.1.0)
46
+ pkg-config (~> 1.1.7)
47
+ oauth2 (1.2.0)
48
+ faraday (>= 0.8, < 0.10)
49
+ jwt (~> 1.0)
50
+ multi_json (~> 1.3)
51
+ multi_xml (~> 0.5)
52
+ rack (>= 1.2, < 3)
53
+ pkg-config (1.1.7)
54
+ rack (2.0.1)
55
+ rake (11.2.2)
56
+ rdoc (3.12.2)
57
+ json (~> 1.4)
58
+ semver (1.0.1)
59
+ shoulda (3.5.0)
60
+ shoulda-context (~> 1.0, >= 1.0.1)
61
+ shoulda-matchers (>= 1.4.1, < 3.0)
62
+ shoulda-context (1.2.1)
63
+ shoulda-matchers (2.8.0)
64
+ activesupport (>= 3.0.0)
65
+ simplecov (0.12.0)
66
+ docile (~> 1.1.0)
67
+ json (>= 1.8, < 3)
68
+ simplecov-html (~> 0.10.0)
69
+ simplecov-html (0.10.0)
70
+ thread_safe (0.3.5)
71
+ tzinfo (1.2.2)
72
+ thread_safe (~> 0.1)
73
+
74
+ PLATFORMS
75
+ ruby
76
+
77
+ DEPENDENCIES
78
+ bundler (~> 1.0)
79
+ juwelier (~> 2.1.0)
80
+ rdoc (~> 3.12)
81
+ shoulda
82
+ simplecov
83
+
84
+ BUNDLED WITH
85
+ 1.11.2
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 kaogaau
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = insnergy-api-ruby-client
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to insnergy-api-ruby-client
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2016 kaogaau. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'juwelier'
15
+ Juwelier::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "insnergy-api-ruby-client"
18
+ gem.homepage = "http://github.com/kaogaau/insnergy-api-ruby-client"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{The ruby gem about In-Snergy Family}
21
+ gem.description = %Q{The ruby gem about In-Snergy Family}
22
+ gem.email = "itri.n100.a20229@gmail.com"
23
+ gem.authors = ["kaogaau"]
24
+
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Juwelier::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ desc "Code coverage detail"
37
+ task :simplecov do
38
+ ENV['COVERAGE'] = "true"
39
+ Rake::Task['test'].execute
40
+ end
41
+
42
+ task :default => :test
43
+
44
+ require 'rdoc/task'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "insnergy-api-ruby-client #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,61 @@
1
+ # Generated by juwelier
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: insnergy-api-ruby-client 0.1.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "insnergy-api-ruby-client"
9
+ s.version = "0.1.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["kaogaau"]
14
+ s.date = "2016-09-14"
15
+ s.description = "The ruby gem about In-Snergy Family"
16
+ s.email = "itri.n100.a20229@gmail.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "insnergy-api-ruby-client.gemspec",
30
+ "lib/insnergy-api-ruby-client.rb"
31
+ ]
32
+ s.homepage = "http://github.com/kaogaau/insnergy-api-ruby-client"
33
+ s.licenses = ["MIT"]
34
+ s.rubygems_version = "2.5.1"
35
+ s.summary = "The ruby gem about In-Snergy Family"
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 4
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
42
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
43
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
44
+ s.add_development_dependency(%q<juwelier>, ["~> 2.1.0"])
45
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
46
+ else
47
+ s.add_dependency(%q<shoulda>, [">= 0"])
48
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
50
+ s.add_dependency(%q<juwelier>, ["~> 2.1.0"])
51
+ s.add_dependency(%q<simplecov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
57
+ s.add_dependency(%q<juwelier>, ["~> 2.1.0"])
58
+ s.add_dependency(%q<simplecov>, [">= 0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,158 @@
1
+ module Insnergy
2
+
3
+ class Client
4
+ attr_accessor :domain, :oauth_key, :oauth_secert, :refresh_token
5
+ attr_reader :access_token, :user_id
6
+
7
+ def initialize(domain: nil, oauth_key: nil, oauth_secert: nil, refresh_token: nil)
8
+ @domain = domain
9
+ @oauth_key = oauth_key
10
+ @oauth_secert = oauth_secert
11
+ @refresh_token = refresh_token
12
+ @user_id = nil
13
+ refresh!
14
+ user_id!
15
+ end
16
+
17
+ def refresh!
18
+ response = JSON.parse(RestClient.post "#{@domain}/if/oauth/token" ,{:client_id => @oauth_key, :client_secret => @oauth_secert, :absytem => 'IFA', :grant_type => 'refresh_token', :refresh_token => @refresh_token }, :accept => :json)
19
+ raise "<refresh_token is nil>\n#{response}" unless response['refresh_token'] != nil
20
+ raise "<access_token is nil>\n#{response}" unless response['access_token'] != nil
21
+ @refresh_token = response['refresh_token']
22
+ @access_token = response['access_token']
23
+ end
24
+
25
+ def user_id!
26
+ response = JSON.parse(RestClient.get "#{@domain}/if/3/user/me" ,{:Authorization => "Bearer #{@access_token}"})
27
+ raise "<user_id is nil>\n#{response}" unless response['user']['user_id'] != nil
28
+ @user_id = response['user']['user_id']
29
+ end
30
+
31
+ def sensor
32
+ Client::Sensor.new(client: self, category: 'sensor')
33
+ end
34
+
35
+ def power(device_id: nil, start_time: nil, end_time: nil)
36
+ client = Client::Power.new(client: self, device_id: device_id, start_time: start_time, end_time: end_time)
37
+ client.get_device
38
+ end
39
+ end
40
+
41
+ class Client::Power
42
+ attr_reader :access_token, :user_id, :device_type_pointer
43
+
44
+ def initialize(client: nil, device_id: nil, start_time: nil, end_time: nil)
45
+ @access_token = client.access_token
46
+ @user_id = client.user_id
47
+ @domain = client.domain
48
+ @device_id = device_id
49
+ @start_time = start_time
50
+ @end_time = end_time
51
+ end
52
+
53
+ def get_device
54
+ Power.new(get_response)
55
+ end
56
+
57
+ def get_response
58
+ parameter = {:params => {:apsystem => "IFA", :email => @user_id, :attr => "dm1mi", :start_time => @start_time, :end_time => @end_time, :dev_ids => @device_id}, :Authorization => "Bearer #{@access_token}"}
59
+ response = JSON.parse(RestClient.get "#{@domain}/if/3/device/history_ext", parameter)
60
+ response
61
+ end
62
+ end
63
+
64
+ class Power
65
+ attr_reader :device_type_pointer, :type, :month_total_power_value, :alias
66
+
67
+ def initialize(opts = {})
68
+ @device_type_pointer = opts['devices'][0].keys.include? 'sub_dev_id'
69
+ case device_type_pointer
70
+ when true
71
+ device_is_multiple_hole(opts)
72
+ when false
73
+ device_is_single_hole(opts)
74
+ end
75
+ end
76
+
77
+ def device_is_multiple_hole(opts)
78
+ @type = "multiple hole"
79
+ holes = opts['devices'][0]['data'][0]['value'].split(';')
80
+ names = opts['devices'][0]['sub_alias'].to_s.split(';')
81
+ for i in 1..holes.size
82
+ self.class.send(:attr_reader, "hole_#{i}_power_value", "hole_#{i}_alias")
83
+ instance_variable_set("@hole_#{i}_power_value", holes[i - 1].to_f)
84
+ instance_variable_set("@hole_#{i}_alias", names[i - 1].split('|')[1].split(':')[1])
85
+ end
86
+ end
87
+
88
+ def device_is_single_hole(opts)
89
+ @type = "single hole"
90
+ @power_value = opts['devices'][0]['data'][0]['value'].to_f
91
+ @alias = opts['devices'][0]['alias']
92
+ end
93
+ end
94
+
95
+ class Client::Sensor
96
+ attr_reader :access_token, :user_id, :widgets
97
+
98
+ def initialize(client: nil,category: nil)
99
+ @access_token = client.access_token
100
+ @user_id = client.user_id
101
+ @domain = client.domain
102
+ @category = category
103
+ get_device
104
+ end
105
+
106
+ def get_device
107
+ @widgets = Array.new
108
+ get_response['widgets'].each do |ele|
109
+ @widgets << Sensor.new(ele)
110
+ end
111
+ end
112
+
113
+ def each
114
+ @widgets.each do |ele|
115
+ yield ele
116
+ end
117
+ end
118
+
119
+ def get_response
120
+ parameter = {:params => {:apsystem => "IFA", :email => @user_id, :type_code => 1, :dev_category => "#{@category}"}, :Authorization => "Bearer #{@access_token}"}
121
+ response = JSON.parse(RestClient.get "#{@domain}/if/3/user/widgets", parameter)
122
+ raise "err code is not zero\n#{response}" unless response['err']['code'] == '0'
123
+ response
124
+ end
125
+ end
126
+
127
+ class Sensor
128
+ attr_reader :widgets_alias, :dev_id, :dev_type_name, :status
129
+
130
+ def initialize(opts = {})
131
+ @infos = Hash.new
132
+ @widgets_alias = opts['alias']
133
+ @dev_id = opts['dev_id']
134
+ @dev_type_name = opts['dev_type_name']
135
+ @status = opts['status']
136
+ opts['widget_infos'].each do |ele|
137
+ @infos[ele['info_name']] = ele['info_value']
138
+ end
139
+ end
140
+
141
+ def widgets_info_value
142
+ @dev_type_name
143
+ end
144
+
145
+ def co_meter
146
+ @infos['400700']
147
+ end
148
+
149
+ def co2_meter
150
+ @infos['400600']
151
+ end
152
+
153
+ def sensor_th_ty
154
+ "#{@infos['400100']}|#{@infos['400200']}"
155
+ end
156
+ end
157
+
158
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insnergy-api-ruby-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kaogaau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: juwelier
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: The ruby gem about In-Snergy Family
84
+ email: itri.n100.a20229@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - ".document"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - insnergy-api-ruby-client.gemspec
99
+ - lib/insnergy-api-ruby-client.rb
100
+ homepage: http://github.com/kaogaau/insnergy-api-ruby-client
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.5.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: The ruby gem about In-Snergy Family
124
+ test_files: []