simple_upnp 0.0.2 → 0.0.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.
@@ -3,11 +3,21 @@ PATH
3
3
  specs:
4
4
  simple_upnp (0.0.2)
5
5
  nori (~> 2.0.0)
6
+ rspec (~> 2.12.0)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
- nori (2.0.0)
11
+ diff-lcs (1.1.3)
12
+ nori (2.0.3)
13
+ rspec (2.12.0)
14
+ rspec-core (~> 2.12.0)
15
+ rspec-expectations (~> 2.12.0)
16
+ rspec-mocks (~> 2.12.0)
17
+ rspec-core (2.12.2)
18
+ rspec-expectations (2.12.1)
19
+ diff-lcs (~> 1.1.3)
20
+ rspec-mocks (2.12.1)
11
21
 
12
22
  PLATFORMS
13
23
  ruby
data/README.md CHANGED
@@ -25,7 +25,11 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- The below code (which can be triggered with the included rake task: "rake simple_upnp:discover"):
28
+ You may either:
29
+ * search for a configurable number of seconds and return all unique devices that have responded.
30
+ * attempt to find a specific device and exit early once found
31
+
32
+ The following example shows search (which can be triggered with the included rake task: "rake simple_upnp:search"):
29
33
  ```
30
34
  include_location_details = true
31
35
  devices = SimpleUpnp::Discovery.search()
@@ -36,7 +40,32 @@ devices.each do |device|
36
40
  end
37
41
  ```
38
42
 
39
- Should produce output similar to the following:
43
+ The following example shows find (which can be triggered with the included rake task: "rake simple_upnp:find_hue"):
44
+ ```
45
+ include_location_details = true
46
+ hue_device = nil
47
+ SimpleUpnp::Discovery.find do |device|
48
+ device_json = device.to_json(include_location_details)
49
+ if device_json['root']
50
+ if device_json['root']['device']
51
+ if device_json['root']['device']['friendlyName']
52
+ friendlyName = device_json['root']['device']['friendlyName']
53
+ if friendlyName =~ /Philips hue/
54
+ hue_device = device
55
+ break
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ if hue_device
62
+ puts 'Device Found: '
63
+ puts hue_device.to_json(include_location_details)
64
+ puts ''
65
+ end
66
+ ```
67
+
68
+ Search should produce output similar to the following:
40
69
  ```
41
70
  Searching for devices...
42
71
  Device Found:
data/Rakefile CHANGED
@@ -3,8 +3,15 @@ require "bundler/gem_tasks"
3
3
  $LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/lib")
4
4
  require 'simple_upnp'
5
5
 
6
+ require "bundler/gem_tasks"
7
+ require "rspec/core/rake_task"
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :default => :spec
12
+
6
13
  namespace :simple_upnp do
7
- task :discover do
14
+ task :search do
8
15
  puts "Searching for devices...\n"
9
16
  include_location_details = true
10
17
  devices = SimpleUpnp::Discovery.search()
@@ -14,4 +21,29 @@ namespace :simple_upnp do
14
21
  puts ''
15
22
  end
16
23
  end
24
+
25
+ task :find_hue do
26
+ puts "Searching for Phillips Hue...\n"
27
+ include_location_details = true
28
+ hue_device = nil
29
+ SimpleUpnp::Discovery.find do |device|
30
+ device_json = device.to_json(include_location_details)
31
+ if device_json['root']
32
+ if device_json['root']['device']
33
+ if device_json['root']['device']['friendlyName']
34
+ friendlyName = device_json['root']['device']['friendlyName']
35
+ if friendlyName =~ /Philips hue/
36
+ hue_device = device
37
+ break
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ if hue_device
44
+ puts 'Device Found: '
45
+ puts hue_device.to_json(include_location_details)
46
+ puts ''
47
+ end
48
+ end
17
49
  end
@@ -11,42 +11,54 @@ module SimpleUpnp
11
11
 
12
12
  # Signal the uPnP Multicast address and wait for responses, returning an array of SimpleUpnp::Devices
13
13
  def self.search(seconds_to_listen=5)
14
- socket = UDPSocket.new
15
- socket.bind('', SSDP_PORT)
16
- socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, [1].pack('i'))
17
- socket.send(M_SEARCH, 0, SSDP_ADDR, SSDP_PORT)
18
- messages = receive_messages(socket, seconds_to_listen)
19
- devices = process_messages(messages)
14
+ devices = []
15
+ open_socket do |socket|
16
+ process_messages(socket, seconds_to_listen) do |message|
17
+ device = SimpleUpnp::Device.new(message)
18
+ index = devices.index { |x| x.usn == device.usn }
19
+ devices << device if index.nil?
20
+ end
21
+ end
22
+ devices
23
+ end
24
+
25
+ # Signal the uPnP Multicast address and wait for responses, which can be processed by an input block accepting a SimpleUpnp::Device
26
+ # Use break to exit the block once you have found the device being looked for
27
+ def self.find(seconds_to_listen=5, &block)
28
+ open_socket do |socket|
29
+ process_messages(socket, seconds_to_listen) do |message|
30
+ device = SimpleUpnp::Device.new(message)
31
+ yield device
32
+ end
33
+ end
20
34
  end
21
35
 
22
36
  private
23
37
 
24
- # Read responses from the socket until the Timout triggers
25
- def self.receive_messages(socket, seconds_to_listen)
26
- messages = []
38
+ def self.open_socket(&block)
39
+ begin
40
+ socket = UDPSocket.new
41
+ socket.bind('', SSDP_PORT)
42
+ socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, [1].pack('i'))
43
+ socket.send(M_SEARCH, 0, SSDP_ADDR, SSDP_PORT)
44
+ yield socket
45
+ ensure
46
+ socket.close
47
+ end
48
+ end
49
+
50
+ def self.process_messages(socket, seconds_to_listen, &block)
27
51
  begin
28
52
  Timeout::timeout(seconds_to_listen) do
29
53
  while true
30
54
  message, sender = socket.recvfrom(MAX_RECEIVE_LENGTH)
31
- messages << message
55
+ yield message
32
56
  end
33
57
  end
34
58
  rescue Timeout::Error => e
35
59
  # Finished Listening
36
60
  end
37
- messages
38
- end
39
-
40
- # Convert messages to a unique array of Devices
41
- def self.process_messages(messages)
42
- devices = []
43
- messages.each do |message|
44
- device = SimpleUpnp::Device.new(message)
45
- index = devices.index { |x| x.usn == device.usn }
46
- devices << device if index.nil?
47
- end
48
- devices
49
61
  end
50
62
 
51
63
  end
52
- end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleUpnp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -16,5 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = SimpleUpnp::VERSION
17
17
 
18
18
  gem.add_dependency "nori", '~> 2.0.0'
19
+ gem.add_dependency "rspec", '~> 2.12.0'
19
20
 
20
21
  end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'simple_upnp'
5
+
6
+ # Requires supporting ruby files with custom matchers and macros, etc,
7
+ # in spec/support/ and its subdirectories.
8
+ Dir[File.dirname(__FILE__)+"/support/**/*.rb"].each do |f|
9
+ require f
10
+ end
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_upnp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-27 00:00:00.000000000 Z
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nori
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 2.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.12.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.12.0
30
46
  description: Simple library to perform basic UPnP network discovery.
31
47
  email:
32
48
  - dennis.kuczynski@gmail.com
@@ -45,6 +61,7 @@ files:
45
61
  - lib/simple_upnp/discovery.rb
46
62
  - lib/simple_upnp/version.rb
47
63
  - simple_upnp.gemspec
64
+ - spec/spec_helper.rb
48
65
  homepage: ''
49
66
  licenses: []
50
67
  post_install_message:
@@ -69,5 +86,6 @@ rubygems_version: 1.8.24
69
86
  signing_key:
70
87
  specification_version: 3
71
88
  summary: Simple library to perform basic UPnP network discovery.
72
- test_files: []
89
+ test_files:
90
+ - spec/spec_helper.rb
73
91
  has_rdoc: