reco4life 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10f31ff989799d3444b18b212898126fdfe0778b
4
+ data.tar.gz: 57a6394d4a65c88376b515c960f5226fe1dc91c9
5
+ SHA512:
6
+ metadata.gz: fbedb7e4f13fbbabb359a54b15c3f3d2f0e3bdf9ca0063c24f67142a5482779ffc790192ea37fa59f719293c24158721bc1ae060b3ad5c9a377ad0ae65d8d405
7
+ data.tar.gz: d2c06489c03fc857d1e7e217cdcbf8a841b05bc4c5efb116a6cae2bf250728ada5f3194d0fb5fcdaac171db52a8b1b4b3d11415ee3d9b4d6892fbad6bac03c56
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .idea/
12
+ test/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in reco4life.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 aaron67
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Reco4life
2
+
3
+ 封装[Reco4life开发者平台](http://www.reco4life.com/wiki/%E9%A6%96%E9%A1%B5)提供的API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'reco4life'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install reco4life
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'reco4life'
25
+
26
+ begin
27
+ Reco4life.user_name = 'your_user_name'
28
+ Reco4life.api_key ='your_api_key'
29
+
30
+ Reco4life.devices.each do |sn|
31
+ next unless Reco4life.online? sn
32
+ Reco4life.turn_on sn
33
+ puts Reco4life.powered_on? sn
34
+ end
35
+ rescue => e
36
+ puts e
37
+ end
38
+ ```
39
+
40
+ ## License
41
+
42
+ [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "reco4life"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Reco4life
2
+ VERSION = '0.1.0'
3
+ end
data/lib/reco4life.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'reco4life/version'
2
+ require 'http'
3
+ require 'json'
4
+
5
+ module Reco4life
6
+ API_URL = 'http://api.reco4life.com/v/api.1.1'
7
+ GET_TOKEN = "#{API_URL}/get_token?user_name=%s&api_key=%s"
8
+ ITEM_LIST = "#{API_URL}/item_list?user_name=%s"
9
+ ITEM_SWITCH = "#{API_URL}/item_switch?user_name=%s&sn=%s&status=%d"
10
+ ERROR_CODE_MAP = {
11
+ -1 => '操作失败',
12
+ 2 => 'TOKEN错误',
13
+ 3 => 'TOKEN失效',
14
+ 4 => 'TOKEN超出每小时使用次数',
15
+ 5 => 'TOKEN超出每天使用次数',
16
+ 6 => '用户名与API_KEY校验失败',
17
+ 7 => '用户没有权限操作对应的硬件',
18
+ 8 => '用户控制的硬件已断开连接',
19
+ 999 => '服务异常',
20
+ }
21
+
22
+ class << self
23
+ attr_accessor :user_name, :api_key
24
+
25
+ def devices
26
+ fetch_devices.map { |h| h['sn'] }
27
+ end
28
+
29
+ def turn_on(sn)
30
+ return if powered_on?(sn)
31
+ switch(sn, 1)
32
+ end
33
+
34
+ def turn_off(sn)
35
+ return unless powered_on?(sn)
36
+ switch(sn, 0)
37
+ end
38
+
39
+ def online?(sn)
40
+ check(sn, 'is_online')
41
+ end
42
+
43
+ def powered_on?(sn)
44
+ check(sn, 'is_poweron')
45
+ end
46
+
47
+ private
48
+
49
+ def refresh_token
50
+ hash = JSON.parse HTTP.get(GET_TOKEN % [user_name, api_key]).to_s
51
+ @token = hash['token']
52
+ raise ERROR_CODE_MAP[hash['result'].to_i] if @token.nil?
53
+ @token_expire_time = Time.parse(hash['expire_date'])
54
+ end
55
+
56
+ def token_valid?
57
+ return true if !@token.nil? && !@token_expire_time.nil? && Time.now < @token_expire_time
58
+ false
59
+ end
60
+
61
+ def fetch_devices
62
+ refresh_token unless token_valid?
63
+ hash = JSON.parse HTTP[token: @token].get(ITEM_LIST % user_name).to_s
64
+ raise ERROR_CODE_MAP[hash['result'].to_i] if hash['result'] != '1'
65
+ hash['list']
66
+ end
67
+
68
+ def check(sn, field_name)
69
+ fetch_devices.each { |h| return h[field_name] == 1 if h['sn'] == sn }
70
+ raise ERROR_CODE_MAP[7]
71
+ end
72
+
73
+ def switch(sn, status)
74
+ hash = JSON.parse HTTP[token: @token].get(ITEM_SWITCH % [Reco4life.user_name, sn, status]).to_s
75
+ raise ERROR_CODE_MAP[hash['result'].to_i] if hash['result'] != '1'
76
+ end
77
+ end
78
+ end
data/reco4life.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'reco4life/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'reco4life'
8
+ spec.version = Reco4life::VERSION
9
+ spec.authors = ['aaron67']
10
+ spec.email = ['aaron67@aaron67.cc']
11
+
12
+ spec.summary = %q{封装Reco4life提供的HTTP API}
13
+ spec.description = %q{Reco4life提供HTTP API来管理智能插座,封装这些接口}
14
+ spec.homepage = 'https://github.com/gitzhou/reco4life'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.13'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'http', '2.1.0'
33
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reco4life
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aaron67
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.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: 2.1.0
55
+ description: Reco4life提供HTTP API来管理智能插座,封装这些接口
56
+ email:
57
+ - aaron67@aaron67.cc
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/reco4life.rb
70
+ - lib/reco4life/version.rb
71
+ - reco4life.gemspec
72
+ homepage: https://github.com/gitzhou/reco4life
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ allowed_push_host: https://rubygems.org
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: 封装Reco4life提供的HTTP API
97
+ test_files: []