irkit 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e995782033d14e6589ddf02df38cf7eccb8fa9b
4
+ data.tar.gz: 43f37c11ae7a5c379c3b77be2203291196105de7
5
+ SHA512:
6
+ metadata.gz: 60b51d5dbf004ab1ee1a7b9069ec6e1129c7d01863cf42d89c00084e90239416b86156222090322d6042cfafae6ab70d5fb3a7d059c6b28cba3fbce9ffa7ad96
7
+ data.tar.gz: c2cdb0d42918061cda7821c3dd858b726ac28dc5ff6a51b46980292352a42072e5df3654ffcfbded4f1bc184611f41ad4ac9bca43d1ec2ae52086ade715f54c3
@@ -0,0 +1,19 @@
1
+ *~
2
+ *#*
3
+ .DS_Store
4
+ *.gem
5
+ *.rbc
6
+ .bundle
7
+ .config
8
+ .yardoc
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in irkit.gemspec
4
+ gemspec
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2014-03-16
2
+
3
+ * first release
4
+ * Implemented IRKit::Device and IRKit::InternetAPI
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sho Hashimoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,114 @@
1
+ # IRKit
2
+
3
+ [IRKit](http://getirkit.com) Client for Ruby
4
+
5
+
6
+ ## Requirements
7
+
8
+ - Ruby2.0+
9
+ - Mac OSX or Linux
10
+
11
+
12
+ ## Installation
13
+
14
+ for Mac
15
+
16
+ % gem install irkit
17
+
18
+ for Debian/Ubuntu Linux
19
+
20
+ % sudo apt-get install libavahi-compat-libdnssd-dev
21
+ % gem install irkit
22
+
23
+
24
+ ## Usage
25
+
26
+ see [samples](https://github.com/shokai/ruby-irkit/tree/master/samples)
27
+
28
+ ### Read/Write IR-Data
29
+
30
+ IRKit has a HTTP-API that can be used from within the same LAN.
31
+
32
+ ```ruby
33
+ require 'irkit'
34
+
35
+ # find IRKit with Bonjour
36
+ irkit = IRKit::Device.find.first
37
+
38
+ # or, specify with IP-Address
39
+ # irkit = IRKit::Device.new(address: '192.168.0.123')
40
+
41
+ unless irkit
42
+ STDERR.puts 'device not found'
43
+ exit 1
44
+ end
45
+
46
+ ir_data = irkit.get_messages
47
+ unless ir_data
48
+ STDERR.puts 'IR data not found'
49
+ exit 1
50
+ end
51
+
52
+ p ir_data
53
+
54
+ puts 'rewrite IR data'
55
+ irkit.post_messages ir_data
56
+ ```
57
+
58
+
59
+ ### Internet-API
60
+
61
+ To access IRKit from outside of the LAN, use Internet-API.
62
+ It uses api.getirkit.com as a proxy.
63
+
64
+
65
+ Get `clientkey` and `deviceid`
66
+
67
+ ```ruby
68
+ irkit = IRKit::Device.find.first
69
+
70
+ unless irkit
71
+ STDERR.puts 'device not found'
72
+ exit 1
73
+ end
74
+
75
+ token = irkit.get_token
76
+ puts "token: #{token}"
77
+ res = irkit.get_key_and_deviceid(token)
78
+
79
+ puts "clientkey:#{res.clientkey}"
80
+ puts "deviceid:#{res.deviceid}"
81
+ ```
82
+
83
+
84
+ Read/Write with Internet-API
85
+
86
+ ```ruby
87
+ irkit = IRKit::InternetAPI.new(clientkey: "your-clientkey", deviceid: "your-deviceid")
88
+
89
+ unless irkit
90
+ STDERR.puts 'device not found'
91
+ exit 1
92
+ end
93
+
94
+ p irkit
95
+
96
+ unless ir_data = irkit.get_messages
97
+ STDERR.puts 'IR data not found'
98
+ exit 1
99
+ end
100
+
101
+ p ir_data.message
102
+
103
+ puts 'rewrite IR data'
104
+ irkit.post_messages ir_data.message
105
+ ```
106
+
107
+
108
+ ## Contributing
109
+
110
+ 1. Fork it
111
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
112
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
113
+ 4. Push to the branch (`git push origin my-new-feature`)
114
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/test_*.rb"
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'irkit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "irkit"
8
+ spec.version = IRKit::VERSION
9
+ spec.authors = ["Sho Hashimoto"]
10
+ spec.email = ["hashimoto@shokai.org"]
11
+ spec.description = %q{IRKit Client for Ruby}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/shokai/ruby-irkit"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/).reject{|i| i=="Gemfile.lock" }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+
25
+ spec.add_dependency "dnssd"
26
+ spec.add_dependency "httparty"
27
+ spec.add_dependency "json"
28
+ spec.add_dependency "hashie"
29
+ end
@@ -0,0 +1,16 @@
1
+ require "socket"
2
+
3
+ require "dnssd"
4
+ require "httparty"
5
+ require "json"
6
+ require "hashie"
7
+
8
+ require "irkit/version"
9
+ require "irkit/error"
10
+ require "irkit/response"
11
+ require "irkit/device"
12
+ require "irkit/find"
13
+ require "irkit/internet_api"
14
+
15
+ module IRKit
16
+ end
@@ -0,0 +1,59 @@
1
+ module IRKit
2
+ class Device
3
+
4
+ attr_accessor :address, :bonjour_name
5
+
6
+ def initialize(address: nil)
7
+ @address = address
8
+ end
9
+
10
+ def url
11
+ "http://#{@address}"
12
+ end
13
+
14
+ def get_messages
15
+ res = HTTParty.get("#{url}/messages")
16
+ case res.code
17
+ when 200
18
+ return nil if res.body.length < 1
19
+ return IRKit::Response.new JSON.parse res.body
20
+ else
21
+ raise IRKit::Error, res.body
22
+ end
23
+ end
24
+
25
+ def post_messages(data)
26
+ opts = {
27
+ :body => data.to_json
28
+ }
29
+ HTTParty.post "#{url}/messages", opts
30
+ end
31
+
32
+ def get_token
33
+ res = HTTParty.post "#{url}/keys", {}
34
+ case res.code
35
+ when 200
36
+ return JSON.parse(res.body)["clienttoken"]
37
+ else
38
+ raise IRKit::Error, res.body
39
+ end
40
+ end
41
+
42
+ def get_key_and_deviceid(clienttoken)
43
+ raise ArgumentError, "token must be String" unless clienttoken.kind_of? String
44
+ opts = {
45
+ :body => {
46
+ :clienttoken => clienttoken
47
+ }
48
+ }
49
+ res = HTTParty.post "http://api.getirkit.com/1/keys", opts
50
+ case res.code
51
+ when 200
52
+ return IRKit::Response.new JSON.parse(res.body)
53
+ else
54
+ raise IRKit::Error, res.body
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,4 @@
1
+ module IRKit
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,27 @@
1
+ module IRKit
2
+
3
+ class Device
4
+
5
+ def self.find(&block)
6
+ hosts = []
7
+ DNSSD.browse '_irkit._tcp' do |reply|
8
+ next unless reply.name =~ /irkit/
9
+ addrs = Socket.getaddrinfo("#{reply.name}.local.", nil, Socket::AF_INET)
10
+ begin
11
+ device = IRKit::Device.new(address: addrs[0][2])
12
+ device.bonjour_name = reply.name
13
+ hosts.push device
14
+ rescue
15
+ next
16
+ end
17
+ end
18
+ 5.times do
19
+ sleep 1
20
+ break unless hosts.empty?
21
+ end
22
+ return hosts
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,50 @@
1
+ module IRKit
2
+
3
+ class InternetAPI
4
+
5
+ attr_reader :clientkey, :deviceid
6
+
7
+ def initialize(clientkey: nil, deviceid: nil)
8
+ raise ArgumentError, "clientkey must be String" unless clientkey.kind_of? String
9
+ raise ArgumentError, "deviceid must be String" unless deviceid.kind_of? String
10
+ @clientkey = clientkey
11
+ @deviceid = deviceid
12
+ end
13
+
14
+ def url
15
+ "https://api.getirkit.com/1"
16
+ end
17
+
18
+ def get_messages(query={})
19
+ opts = {
20
+ :query => {:clientkey => @clientkey}
21
+ }
22
+ query.each do |k,v|
23
+ opts[:query][k] = v
24
+ end
25
+ res = HTTParty.get("#{url}/messages", opts)
26
+ case res.code
27
+ when 200
28
+ return nil if res.body.length < 1
29
+ return IRKit::Response.new JSON.parse res.body
30
+ when 401
31
+ raise IRKit::Error, "Unauthorized"
32
+ else
33
+ raise IRKit::Error, res.body
34
+ end
35
+ end
36
+
37
+ def post_messages(data)
38
+ opts = {
39
+ :body => {
40
+ :deviceid => @deviceid,
41
+ :clientkey => @clientkey,
42
+ :message => data.to_json
43
+ }
44
+ }
45
+ HTTParty.post "#{url}/messages", opts
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,6 @@
1
+ module IRKit
2
+
3
+ class Response < ::Hashie::Mash
4
+ end
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module IRKit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
2
+
3
+ require 'irkit'
4
+
5
+ unless irkit = IRKit::Device.find.first
6
+ STDERR.puts 'device not found'
7
+ exit 1
8
+ end
9
+
10
+ token = irkit.get_token
11
+ puts "token:\t#{token}"
12
+ res = irkit.get_key_and_deviceid(token)
13
+
14
+ puts "clientkey:\t#{res.clientkey}"
15
+ puts "deviceid:\t#{res.deviceid}"
@@ -0,0 +1,22 @@
1
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
2
+
3
+ require 'irkit'
4
+
5
+ irkit = IRKit::Device.find.first
6
+ # irkit = IRKit::Device.new(address: '192.168.1.112')
7
+ unless irkit
8
+ STDERR.puts 'device not found'
9
+ exit 1
10
+ end
11
+
12
+ p irkit
13
+
14
+ unless ir_data = irkit.get_messages
15
+ STDERR.puts 'IR data not found'
16
+ exit 1
17
+ end
18
+
19
+ p ir_data
20
+
21
+ puts 'rewrite IR data'
22
+ p irkit.post_messages ir_data
@@ -0,0 +1,24 @@
1
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
2
+
3
+ require 'irkit'
4
+
5
+ CLIENT_KEY = ENV["CLIENT_KEY"] || "your_client_key"
6
+ DEVICE_ID = ENV["DEVICE_ID"] || "your_device_id"
7
+
8
+ irkit = IRKit::InternetAPI.new(clientkey: CLIENT_KEY, deviceid: DEVICE_ID)
9
+ unless irkit
10
+ STDERR.puts 'device not found'
11
+ exit 1
12
+ end
13
+
14
+ p irkit
15
+
16
+ unless ir_data = irkit.get_messages
17
+ STDERR.puts 'IR data not found'
18
+ exit 1
19
+ end
20
+
21
+ p ir_data.message
22
+
23
+ puts 'rewrite IR data'
24
+ p irkit.post_messages ir_data.message
@@ -0,0 +1,17 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestDevice < MiniTest::Test
4
+
5
+ def setup
6
+ @device = IRKit::Device.find.first
7
+ end
8
+
9
+ def test_get_messages
10
+ res = @device.get_messages
11
+ assert_equal res.class, IRKit::Response
12
+ assert_equal res.format, "raw"
13
+ assert_equal res.freq, 38
14
+ assert_equal res.data.class, Array
15
+ end
16
+
17
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestFind < MiniTest::Test
4
+
5
+ def test_find
6
+ devices = IRKit::Device.find
7
+ assert_equal devices.class, Array
8
+ devices.each do |device|
9
+ assert_equal device.class, IRKit::Device
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'minitest/autorun'
4
+
5
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
6
+ require 'irkit'
@@ -0,0 +1,19 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestTokenKeyDeviceID < MiniTest::Test
4
+
5
+ def test_token_key_deviceid
6
+ device = IRKit::Device.find.first
7
+ token = device.get_token
8
+ assert_equal token.class, String
9
+ assert token =~ /^[a-zA-Z\d]+$/, "token must be Hex String"
10
+
11
+ info = device.get_key_and_deviceid(token)
12
+ assert_equal info.class, IRKit::Response
13
+ assert_equal info.clientkey.class, String
14
+ assert info.clientkey =~ /^[a-zA-Z\d]+$/, "clientkey must be Hex String"
15
+ assert_equal info.deviceid.class, String
16
+ assert info.deviceid =~ /^[a-zA-Z\d]+$/, "deviceid must be Hex String"
17
+ end
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: irkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sho Hashimoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-16 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dnssd
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: httparty
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: hashie
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: IRKit Client for Ruby
112
+ email:
113
+ - hashimoto@shokai.org
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - History.txt
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - irkit.gemspec
125
+ - lib/irkit.rb
126
+ - lib/irkit/device.rb
127
+ - lib/irkit/error.rb
128
+ - lib/irkit/find.rb
129
+ - lib/irkit/internet_api.rb
130
+ - lib/irkit/response.rb
131
+ - lib/irkit/version.rb
132
+ - samples/get_clientkey_and_deviceid.rb
133
+ - samples/read_and_write.rb
134
+ - samples/read_and_write_internet_api.rb
135
+ - test/test_device.rb
136
+ - test/test_find.rb
137
+ - test/test_helper.rb
138
+ - test/test_token_key_deviceid.rb
139
+ homepage: https://github.com/shokai/ruby-irkit
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.0.14
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: IRKit Client for Ruby
163
+ test_files:
164
+ - test/test_device.rb
165
+ - test/test_find.rb
166
+ - test/test_helper.rb
167
+ - test/test_token_key_deviceid.rb