Skip to content

Commit 667d1a6

Browse files
committed
Initial Commit
0 parents  commit 667d1a6

17 files changed

+372
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in roku-packager.gemspec
4+
gemspec
5+
6+
gem 'rake'

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2012 brookemckim
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.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Roku::Packager
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'roku-packager'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install roku-packager
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'bundler/gem_tasks'
2+
require 'rake/testtask'
3+
4+
Rake::TestTask.new do |t|
5+
t.libs.push 'lib'
6+
t.libs.push 'spec'
7+
t.test_files = FileList['spec/**/*_spec.rb']
8+
t.verbose = true
9+
end
10+
11+
task :default => [:test]
12+
task :spec => [:test]

lib/roku-packager.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'roku-packager/version'
2+
require 'roku-packager/key_gen'
3+
require 'roku-packager/uploader'
4+
require 'roku-packager/packager'
5+
6+
module Roku
7+
module Packager
8+
9+
end
10+
end

lib/roku-packager/genkey_output.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module RokuPackager
2+
module GenkeyOutput
3+
def parse(output)
4+
dev_id, password = nil, nil
5+
6+
if (match = /Password: (\S*)/.match(output))
7+
password = match[1]
8+
end
9+
10+
if (match = /DevID: (\S*)/.match(output))
11+
dev_id = match[1]
12+
end
13+
14+
[dev_id, password]
15+
end
16+
end
17+
end

lib/roku-packager/key_gen.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'net/telnet'
2+
require 'roku-packager/genkey_output'
3+
4+
module RokuPackager
5+
class KeyGen
6+
class ConnectionError < StandardError; end
7+
class GenerationError < StandardError; end
8+
9+
def initialize(roku_hostname)
10+
@hostname = roku_hostname
11+
end
12+
13+
def create
14+
dev_id, password = parse(generate)
15+
16+
if dev_id && password
17+
[dev_id, password]
18+
else
19+
raise GenerationError, 'Error generating credentials'
20+
end
21+
end
22+
23+
def generate
24+
output_from_connection = ''
25+
26+
connection.cmd('genkey') do |output|
27+
output_from_connection << output
28+
end
29+
rescue Timeout::Error
30+
output_from_connection
31+
end
32+
33+
def connection
34+
@connection ||= Net::Telnet::new("Host" => @hostname, "Port" => '8080')
35+
rescue Errno::ECONNREFUSED
36+
raise ConnectionError, 'Connection to device was refused. Possibly wrong host.'
37+
rescue Timeout::Error
38+
raise ConnectionError, 'Connection to device failed. Host is possibly down.'
39+
end
40+
41+
private
42+
43+
include GenkeyOutput
44+
end
45+
end

lib/roku-packager/packager.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'rest-client'
2+
require 'time'
3+
4+
module RokuPackager
5+
class Packager
6+
def initialize(host, name_and_version, password)
7+
@host = host
8+
@name_and_version = name_and_version
9+
@password = password
10+
end
11+
12+
def submit
13+
relative_path = pull_out_relative_package_path(submission_response)
14+
15+
URI::HTTP.build(host: @host, path: '/' + relative_path)
16+
end
17+
18+
def submission_response
19+
parameters = { passwd: @password,
20+
pkg_time: Time.now.to_i,
21+
app_name: @name_and_version,
22+
mysubmit: 'Package',
23+
multipart: true }
24+
25+
RestClient.post url.to_s, parameters
26+
end
27+
28+
def url
29+
@url ||= URI::HTTP.build(host: @host, path: '/plugin_package')
30+
end
31+
32+
private
33+
34+
def pull_out_relative_package_path(html_body)
35+
/a href=\\"(pkgs\S*)\\">/.match(html_body)[1]
36+
end
37+
end
38+
end

lib/roku-packager/uploader.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'uri'
2+
require 'rest_client'
3+
4+
module RokuPackager
5+
class Uploader
6+
def initialize(host)
7+
@host = host
8+
end
9+
10+
def run(file)
11+
delete
12+
upload(file)
13+
end
14+
15+
def upload(file)
16+
parameters = { archive: File.new(file), mysubmit: 'Replace' }
17+
RestClient.post url.to_s, parameters
18+
end
19+
20+
def delete
21+
parameters = { mysubmit: 'Delete', archive: nil, multipart: true }
22+
RestClient.post url.to_s, parameters
23+
end
24+
25+
def url
26+
@url ||= URI::HTTP.build(host: @host, path: '/plugin_install')
27+
end
28+
end
29+
end

lib/roku-packager/version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Roku
2+
module Packager
3+
VERSION = "0.0.1"
4+
end
5+
end

roku-packager.gemspec

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- encoding: utf-8 -*-
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'roku-packager/version'
5+
6+
Gem::Specification.new do |gem|
7+
gem.name = "roku-packager"
8+
gem.version = Roku::Packager::VERSION
9+
gem.authors = ["brookemckim"]
10+
gem.email = ["brooke.mckim@gmail.com"]
11+
gem.description = %q{TODO: Write a gem description}
12+
gem.summary = %q{TODO: Write a gem summary}
13+
gem.homepage = ""
14+
15+
gem.files = `git ls-files`.split($/)
16+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18+
gem.require_paths = ["lib"]
19+
20+
gem.add_dependency 'rest-client', '~> 1.6.6'
21+
22+
gem.add_development_dependency 'mocha'
23+
gem.add_development_dependency 'minitest'
24+
end
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe RokuPackager::GenkeyOutput do
4+
before do
5+
class Genkey
6+
extend RokuPackager::GenkeyOutput
7+
end
8+
9+
@output =<<EOF
10+
.
11+
.
12+
.
13+
.
14+
+
15+
+
16+
+
17+
Password: jO3nThMQZxaAMSH2a1mIiw==
18+
DevID: 65b331d470432d609fec8c5f78e09683559e56b9
19+
>
20+
EOF
21+
end
22+
23+
it 'parses out devid and password' do
24+
Genkey.parse(@output).must_equal [
25+
'65b331d470432d609fec8c5f78e09683559e56b9',
26+
'jO3nThMQZxaAMSH2a1mIiw=='
27+
]
28+
end
29+
end

spec/roku-packager/key_gen_spec.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
3+
describe RokuPackager::KeyGen do
4+
describe '#create' do
5+
before do
6+
@key_gen = RokuPackager::KeyGen.new('192.168.1.10')
7+
end
8+
9+
it 'returns parsed_output' do
10+
@key_gen.expects(:generate).returns('output_from_device')
11+
@key_gen.expects(:parse)
12+
.with('output_from_device').returns(['devid', 'password'])
13+
14+
@key_gen.create.must_equal ['devid', 'password']
15+
end
16+
17+
it 'raises exception if dev_id is nil' do
18+
@key_gen.stubs(:generate).returns(nil)
19+
@key_gen.stubs(:parse).returns([nil, 'password'])
20+
21+
lambda {
22+
@key_gen.create
23+
}.must_raise RokuPackager::KeyGen::GenerationError
24+
end
25+
26+
it 'raises exception if password is nil' do
27+
@key_gen.stubs(:generate).returns(nil)
28+
@key_gen.stubs(:parse).returns(['devid', nil])
29+
30+
lambda {
31+
@key_gen.create
32+
}.must_raise RokuPackager::KeyGen::GenerationError
33+
end
34+
end
35+
end
36+

spec/roku-packager/packager_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
describe RokuPackager::Packager do
4+
before do
5+
host = '192.168.0.10'
6+
name_and_version = 'Lol 1.0'
7+
password = 'sporks'
8+
9+
@packager = RokuPackager::Packager.new(host, name_and_version, password)
10+
end
11+
12+
describe '#url' do
13+
it 'builds a the proper url' do
14+
@packager.url.must_equal URI('http://192.168.0.10/plugin_package')
15+
end
16+
end
17+
18+
describe '#submit' do
19+
it 'builds a download url for the submitted package' do
20+
@packager.expects(:submission_response).returns(nil)
21+
@packager.expects(:pull_out_relative_package_path).returns('pkg/blah.zip')
22+
23+
@packager.submit.must_equal URI('http://192.168.0.10/pkg/blah.zip')
24+
end
25+
end
26+
end
27+

spec/roku-packager/uploader_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'spec_helper'
2+
3+
describe RokuPackager::Uploader do
4+
before do
5+
@uploader = RokuPackager::Uploader.new("192.168.0.10")
6+
end
7+
8+
describe '#run' do
9+
it 'deletes and then uploads the file' do
10+
@uploader.expects(:delete)
11+
@uploader.expects(:upload).with('afile')
12+
13+
@uploader.run('afile')
14+
end
15+
end
16+
17+
describe '#url' do
18+
it 'builds a proper url' do
19+
@uploader.url.must_equal URI("http://192.168.0.10/plugin_install")
20+
end
21+
end
22+
end

spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'minitest/autorun'
2+
require 'mocha'
3+
4+
require 'roku-packager'

0 commit comments

Comments
 (0)