stack-agent 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: 2d55ea43acf663c161d64dc6ea0e29919c12c80a
4
+ data.tar.gz: 722d8e12f60e54b531e5fff505671127fc33195f
5
+ SHA512:
6
+ metadata.gz: 634e4968af801ac2c3097e40e4534c33cf018bba89de0a32e292cb59e628abe987899e33f9e3a0cc6ca06904ad252f7c9f0a83429a1f5b505115bcae6b39cab2
7
+ data.tar.gz: ff0ec8a99559f401e4ade8f8de2f430a89e8d2cd4869dd6188001a481463dc899f15c6908d8e9495ba3b02aabc7a90a046ecdad7774d6b5a922fb4a2938947ad
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .ruby-version
7
+ .yardoc
8
+ Gemfile.lock
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,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Gem dependencies
4
+ gemspec
5
+
6
+ # Development dependencies
7
+ gem 'rake'
8
+ gem 'yard'
9
+
10
+ # Testing dependencies
11
+ group :test do
12
+ gem 'minitest'
13
+ gem 'minitest-wscolor'
14
+ gem 'fakeweb'
15
+ gem 'vcr'
16
+ gem 'mocha', require: false
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aaron Gotwalt
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.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task default: :test
data/Readme.markdown ADDED
@@ -0,0 +1,14 @@
1
+ # StackAgent
2
+
3
+ A Rails agent for Stack. It'll automatically register your development stack when running locally.
4
+
5
+ # Configuration
6
+
7
+ Create a new rails initializer called `stack.rb`.
8
+ ```ruby
9
+ StackAgent.configure do |c|
10
+ c.app_token = 'YOUR_APP_TOKEN_HERE'
11
+ end
12
+
13
+ StackAgent.connect!
14
+ ```
data/bin/stack-agent ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'stack-agent/cli'
6
+
7
+ StackAgent::Cli.start
@@ -0,0 +1,41 @@
1
+ require 'stack-agent/configuration'
2
+ require 'stack-agent/instance'
3
+
4
+ module StackAgent
5
+ VERSION = '0.1.0'
6
+
7
+ class << self
8
+ attr_writer :configuration
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= StackAgent::Configuration.new
13
+ end
14
+
15
+ def self.reset
16
+ @configuration = StackAgent::Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield(configuration)
21
+ end
22
+
23
+ def self.register
24
+ @instance = StackAgent::Instance.new
25
+ @instance.register
26
+ end
27
+
28
+ def self.unregister
29
+ raise 'Not registered' unless @instance && @instance.registered?
30
+ @instance.unregister
31
+ end
32
+
33
+ def self.connect!
34
+ register
35
+
36
+ at_exit do
37
+ unregister
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,52 @@
1
+ require 'thor'
2
+ require 'stack-agent'
3
+ require 'irb'
4
+
5
+ module StackAgent
6
+ class Cli < Thor
7
+ desc 'register APP_TOKEN', 'Registers this instance against the provided token'
8
+ def register(app_token = nil, name = nil, uri = nil)
9
+
10
+ StackAgent.configure do |c|
11
+ c.app_token = app_token
12
+ c.name = name
13
+ c.uri = uri
14
+ end
15
+
16
+ if id = StackAgent::Instance.new().register
17
+ puts "Registered as stack #{id}"
18
+ end
19
+ end
20
+
21
+ desc 'unregister APP_TOKEN INSTANCE_TOKEN', 'Unregisters the provided app and instance token'
22
+ def unregister(app_token, instance_token)
23
+ StackAgent.configure do |c|
24
+ c.app_token = app_token
25
+ end
26
+
27
+ StackAgent::Instance.new(instance_token).unregister
28
+ puts "Unregistered stack"
29
+ end
30
+
31
+ desc 'stacks APP_TOKEN', 'Lists known stacks for a given app'
32
+ def stacks(app_token)
33
+ StackAgent.configure do |c|
34
+ c.app_token = app_token
35
+ end
36
+
37
+ stacks = StackAgent::Instance.stacks
38
+
39
+ puts "Stacks"
40
+ stacks.each do |stack|
41
+ puts "#{stack['id'].ljust(30)} #{stack['name'].ljust(30)} #{stack['uri']}"
42
+ end
43
+ end
44
+
45
+ desc 'cli', 'Launches IRB instance with everything required'
46
+ def cli
47
+ ARGV.clear
48
+ IRB.start
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ require 'socket'
2
+
3
+ module StackAgent
4
+ class Configuration
5
+ DEFAULT_API_HOST = 'https://stackotron-discover.herokuapp.com'
6
+
7
+ attr_accessor :api_host
8
+ attr_accessor :app_token
9
+ attr_accessor :name
10
+ attr_accessor :group
11
+ attr_accessor :uri
12
+
13
+ def initialize
14
+ @api_host = DEFAULT_API_HOST
15
+ @name = Socket.gethostname
16
+
17
+ # If we're running inside rails, attempt to fill in a bunch of the blanks
18
+ if defined?(Rails)
19
+ # only do this if we're in development environment
20
+ if Rails.env.development? && defined?(Rails::Server)
21
+ ip = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address
22
+ port = Rails::Server.new.options[:Port]
23
+ @uri = "http://#{ip}:#{port}"
24
+ @group = 'Development'
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ module StackAgent
5
+ class Instance
6
+ attr_accessor :instance_token
7
+
8
+ def initialize(instance_token = nil)
9
+ self.instance_token = instance_token
10
+ end
11
+
12
+ def register
13
+ config = StackAgent.configuration
14
+
15
+ return false unless config.app_token && config.name && config.uri
16
+
17
+ url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks"
18
+
19
+ data = {
20
+ group: config.group,
21
+ name: config.name,
22
+ uri: config.uri
23
+ }
24
+
25
+ begin
26
+ response = RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).post(data)
27
+ self.instance_token = JSON.parse(response.body)['id']
28
+ rescue RestClient::BadRequest => ex
29
+ false
30
+ end
31
+ end
32
+
33
+ def unregister
34
+ raise 'Not registered' unless registered?
35
+
36
+ config = StackAgent.configuration
37
+ url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks/#{instance_token}"
38
+
39
+ begin
40
+ RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).delete
41
+ true
42
+ rescue RestClient::BadRequest => ex
43
+ false
44
+ end
45
+ end
46
+
47
+ def registered?
48
+ instance_token != nil
49
+ end
50
+
51
+ def self.stacks
52
+ config = StackAgent.configuration
53
+ url = "#{config.api_host}/v1/apps/#{config.app_token}/stacks"
54
+ response = RestClient::Resource.new(url, verify_ssl: OpenSSL::SSL::VERIFY_NONE).get
55
+ JSON.parse(response.body)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stack-agent'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'stack-agent'
8
+ gem.version = StackAgent::VERSION
9
+ gem.authors = ['Aaron Gotwalt']
10
+ gem.email = ['gotwalt@gmail.com']
11
+ gem.description = 'Control Redlink home thermostats'
12
+ gem.summary = gem.description
13
+ gem.homepage = 'https://github.com/gotwalt/stack-agent'
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.add_dependency 'thor'
22
+ gem.add_dependency 'rest-client'
23
+ end
@@ -0,0 +1,4 @@
1
+ VCR.configure do |c|
2
+ c.cassette_library_dir = 'test/cassettes'
3
+ c.hook_into :fakeweb
4
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require :test
4
+ require 'minitest/autorun'
5
+ require 'mocha/setup'
6
+ require 'redlink'
7
+
8
+ # Support files
9
+ Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
10
+ require file
11
+ end
12
+
13
+ class StackAgentTest < MiniTest::Unit::TestCase
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stack-agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Gotwalt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Control Redlink home thermostats
42
+ email:
43
+ - gotwalt@gmail.com
44
+ executables:
45
+ - stack-agent
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE
52
+ - Rakefile
53
+ - Readme.markdown
54
+ - bin/stack-agent
55
+ - lib/stack-agent.rb
56
+ - lib/stack-agent/cli.rb
57
+ - lib/stack-agent/configuration.rb
58
+ - lib/stack-agent/instance.rb
59
+ - stack-agent.gemspec
60
+ - test/support/vcr.rb
61
+ - test/test_helper.rb
62
+ homepage: https://github.com/gotwalt/stack-agent
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Control Redlink home thermostats
86
+ test_files:
87
+ - test/support/vcr.rb
88
+ - test/test_helper.rb
89
+ has_rdoc: