redlink 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ Basic location & thermostat discovery functional. Ability to refresh thermostat
21
21
 
22
22
  Redlink provides a basic CLI experience as well as a growing set of APIs for actual manipulation.
23
23
 
24
- `relink locations` returns a list of locations and thermostats connected to your account.
24
+ `redlink locations` returns a list of locations and thermostats connected to your account.
25
25
 
26
26
  # TODO
27
27
 
@@ -27,7 +27,8 @@ module Redlink
27
27
  end
28
28
 
29
29
  def self.session_expired?
30
- !config_file['session_id_expires'] && config_file['session_id_expires'] < Time.now
30
+ return true unless config_file['session_id_expires']
31
+ config_file['session_id_expires'] < Time.now
31
32
  end
32
33
 
33
34
  def self.user
@@ -58,12 +59,19 @@ module Redlink
58
59
  File.open(File.expand_path(CONFIG_FILE), 'w+') do |f|
59
60
  f.write YAML.dump(@config_file)
60
61
  end
62
+
63
+ # reset to force reload next time a read happens
64
+ @config_file = nil
61
65
  end
62
66
 
63
67
  def self.clear!
64
- [:session_id, :user, :username, :password].each do |k|
65
- self.send("#{k}=", nil)
68
+ new_config = @config_file.dup
69
+
70
+ [:session_id, :session_id_expires, :user, :username, :password].each do |k|
71
+ new_config.delete(k.to_s)
66
72
  end
73
+
74
+ @config_file = new_config
67
75
  self.save
68
76
  end
69
77
 
@@ -3,16 +3,8 @@ require 'savon'
3
3
  module Redlink
4
4
  class Endpoint
5
5
 
6
- def self.endpoint_client
7
- @endpoint_client ||= Savon.client do
8
- wsdl File.expand_path("../../../wsdl/MobileV2.xml", __FILE__)
9
- log_level :warn
10
- log false
11
- end
12
- end
13
-
14
6
  def self.login(username = Configuration.username, password = Configuration.password)
15
- raise "A username and password is required" unless username && password
7
+ return unless username && password
16
8
 
17
9
  params = {
18
10
  username: username,
@@ -22,11 +14,11 @@ module Redlink
22
14
  uiLanguage: 'Default'
23
15
  }
24
16
 
25
- body = endpoint_client.call(:authenticate_user_login, message: params).body
26
- p body[:authenticate_user_login_response]
27
- if body[:authenticate_user_login_response][:authenticate_user_login_result][:result] == 'Success'
28
- user = body[:authenticate_user_login_response][:authenticate_user_login_result][:user_info]
29
- session_id = body[:authenticate_user_login_response][:authenticate_user_login_result][:session_id]
17
+ result = call_remote_method(:authenticate_user_login, message: params)
18
+
19
+ if result[:result] == 'Success'
20
+ user = result[:user_info]
21
+ session_id = result[:session_id]
30
22
 
31
23
  Configuration.username = username
32
24
  Configuration.password = password
@@ -41,19 +33,16 @@ module Redlink
41
33
  end
42
34
 
43
35
  def self.logout
44
- return unless Configuration.session_id
45
- body = endpoint_client.call(:log_off, message: {sessionID: Configuration.session_id}).body
46
-
47
- if body[:log_off_response][:log_off_result][:result] == 'Success'
48
- Configuration.clear!
36
+ if Configuration.session_id
37
+ call_remote_method(:log_off, message: {sessionID: Configuration.session_id})
49
38
  end
39
+
40
+ Configuration.clear!
50
41
  end
51
42
 
52
43
  def self.locations
53
- verify_token
54
-
55
- body = endpoint_client.call(:get_locations, message: {sessionID: Configuration.session_id}).body
56
- [body[:get_locations_response][:get_locations_result][:locations]].flatten.map do |loc|
44
+ result = call_remote_method(:get_locations, message: {sessionID: Configuration.session_id})
45
+ [result[:locations]].flatten.map do |loc|
57
46
  loc[:location_info]
58
47
  end
59
48
  end
@@ -61,9 +50,9 @@ module Redlink
61
50
  def self.get_volatile_thermostat_data(thermostat_id)
62
51
  verify_token
63
52
 
64
- body = endpoint_client.call(:get_volatile_thermostat_data, message: {sessionID: Configuration.session_id, thermostatID: thermostat_id}).body
53
+ result = call_remote_method(:get_locations, message: {sessionID: Configuration.session_id, thermostatID: thermostat_id})
65
54
 
66
- body[:get_volatile_thermostat_data_response][:get_volatile_thermostat_data_result][:ui]
55
+ result[:ui]
67
56
  end
68
57
 
69
58
  private
@@ -74,5 +63,25 @@ module Redlink
74
63
  end
75
64
  end
76
65
 
66
+ def self.call_remote_method(method, options)
67
+ verify_token
68
+ body = endpoint_client.call(method, options).body
69
+ result = body["#{method}_response".to_sym]["#{method}_result".to_sym]
70
+ raise InvalidSessionError.new if result[:result] == 'InvalidSessionID'
71
+ result
72
+ end
73
+
74
+
75
+ def self.endpoint_client
76
+ @endpoint_client ||= Savon.client do
77
+ wsdl File.expand_path("../../../wsdl/MobileV2.xml", __FILE__)
78
+ log_level :warn
79
+ log false
80
+ end
81
+ end
82
+
83
+ class InvalidSessionError < StandardError; end
84
+ class NoCredentialsError < StandardError; end
77
85
  end
86
+
78
87
  end
@@ -1,3 +1,3 @@
1
1
  module Redlink
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redlink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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: 2013-03-23 00:00:00.000000000 Z
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: savon