fms-admin-api 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.
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p180@fms-admin-api"
8
+
9
+ #
10
+ # First we attempt to load the desired environment directly from the environment
11
+ # file. This is very fast and efficicent compared to running through the entire
12
+ # CLI and selector. If you want feedback on which environment was used then
13
+ # insert the word 'use' after --create as this triggers verbose mode.
14
+ #
15
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
17
+ then
18
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
19
+
20
+ if [[ -s ".rvm/hooks/after_use" ]]
21
+ then
22
+ . ".rvm/hooks/after_use"
23
+ fi
24
+ else
25
+ # If the environment file has not yet been created, use the RVM CLI to select.
26
+ if ! rvm --create "$environment_id"
27
+ then
28
+ echo "Failed to create RVM environment ''."
29
+ fi
30
+ fi
31
+
32
+ #
33
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
34
+ # it be automatically loaded. Uncomment the following and adjust the filename if
35
+ # necessary.
36
+ #
37
+ # filename=".gems"
38
+ # if [[ -s "$filename" ]] ; then
39
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
40
+ # fi
41
+
42
+ #
43
+ # If you use bundler and would like to run bundle each time you enter the
44
+ # directory, you can uncomment the following code.
45
+ #
46
+ # export PATH="./bin:$PATH"
47
+ #
48
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fms-admin-api (0.1)
5
+ activesupport
6
+ colorize
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.1.3)
12
+ multi_json (~> 1.0)
13
+ addressable (2.2.6)
14
+ colorize (0.5.8)
15
+ crack (0.3.1)
16
+ multi_json (1.0.4)
17
+ webmock (1.7.8)
18
+ addressable (~> 2.2, > 2.2.5)
19
+ crack (>= 0.1.7)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ fms-admin-api!
26
+ webmock
@@ -0,0 +1,40 @@
1
+ Flash Media Server Administration API
2
+ =====================================
3
+
4
+ Command line interface to [Flash Media Server Administration API][fmsapi]:
5
+
6
+ Usage
7
+ -----
8
+
9
+ $ fmsapi <method_name> --host=<fms host> [other params]
10
+
11
+ Just pick a method from the [documentation][fmsapi] and replace convention
12
+ from camelCase to underscore_case (same for the parameters)
13
+
14
+ Example:
15
+
16
+ $ fmsapi reload_app --host=fms.example.com --auser=fms --apswd=secret --app_inst=live
17
+
18
+
19
+ Ruby client usage
20
+ -----------------
21
+
22
+ You can use the ruby client directly on you code:
23
+
24
+ require 'fms'
25
+
26
+ client = FMS::Client.new(:host => 'fms.example.com',
27
+ :auser => 'fms',
28
+ :apswd => 'secret') # you can use :port here, 1111 is default
29
+
30
+ now just call any method defined in the [FMS Admin API][fmsapi]:
31
+
32
+ client.reload_app(:app_inst => 'live/cam1')
33
+ client.get_apps(:force => true, :verbose => true)
34
+
35
+ just note that the methods name and parameters are used with ruby_default_notation
36
+ instead of camelCase as documented.
37
+
38
+ Take a look in the tests/ folder for more detailed specification.
39
+
40
+ [fmsapi]: http://help.adobe.com/en_US/flashmediaserver/adminapi/WSa4cb07693d12388431df580a12a34991ebc-8000.html
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new do |t|
6
+ puts 'running tests'
7
+ t.libs << 'lib'
8
+ t.libs << 'tests'
9
+ t.test_files = FileList['tests/*_tests.rb', 'tests/*_test.rb']
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fms'
4
+
5
+ FMS::CmdLine.parse(ARGV)
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "fms"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "fms-admin-api"
8
+ s.version = FMS::VERSION
9
+ s.authors = ["Igor Sobreira"]
10
+ s.email = ["igor@igorsobreira.com"]
11
+ s.homepage = "http://github.com/igorsobreira/fms-admin-api"
12
+ s.summary = %q{Ruby client and command line interface to Flash Media Server Administration API}
13
+ s.description = %q{Ruby client and command line interface to Flash Media Server Administration API}
14
+
15
+ s.rubyforge_project = "fms-admin-api"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency "activesupport"
23
+ s.add_runtime_dependency "colorize"
24
+ s.add_development_dependency "webmock"
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'fms/client'
2
+ require 'fms/cmdline'
3
+ require 'fms/version'
4
+
5
+ module FMS
6
+ Docs = "http://help.adobe.com/en_US/flashmediaserver/adminapi/WSa4cb07693d12388431df580a12a34991ebc-8000.html"
7
+ end
@@ -0,0 +1,53 @@
1
+ require 'net/http'
2
+ require 'active_support'
3
+
4
+ module FMS
5
+ class Client
6
+
7
+ attr_reader :host, :port, :base_params
8
+
9
+ def initialize(options = {})
10
+ raise ArgumentError, ":host option is required" unless options.include? :host
11
+
12
+ defaults = {:auser => 'fms', :apswd => 'fms', :port => 1111}
13
+ defaults.update(options)
14
+
15
+ @host = defaults[:host]
16
+ @port = defaults[:port]
17
+ @base_params = {:auser => defaults[:auser], :apswd => defaults[:apswd]}
18
+ end
19
+
20
+ def method_missing(meth, *args)
21
+ meth = ActiveSupport::Inflector.camelize(meth.to_s, false)
22
+ if args.length == 1
23
+ params = args[0]
24
+ else
25
+ params = {}
26
+ end
27
+ do_get(meth, camelize_params(params))
28
+ end
29
+
30
+ private
31
+
32
+ def do_get(action, params = {})
33
+ resp = Net::HTTP.get_response(build_url(action, params))
34
+ raise NoMethodError, "#{action.inspect} is not a valid API method" unless resp.code == "200"
35
+ resp.body
36
+ end
37
+
38
+ def build_url(method, extra_params = {})
39
+ url = URI("http://#{@host}:#{@port}/admin/#{method}")
40
+ url.query = URI.encode_www_form(@base_params.merge(extra_params))
41
+ url
42
+ end
43
+
44
+ def camelize_params(params)
45
+ cam_params = {}
46
+ params.each_pair do |key, value|
47
+ cam_params[ActiveSupport::Inflector.camelize(key.to_s, false)] = value
48
+ end
49
+ cam_params
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,142 @@
1
+ require 'colorize'
2
+
3
+ module FMS
4
+
5
+ module CmdLine
6
+ class << self
7
+
8
+ def parse(argv)
9
+ method = MethodParser.parse(argv)
10
+ params = ParamsParser.parse(argv)
11
+ call_if_possible method, params
12
+ Output.flush
13
+ end
14
+
15
+ def call_if_possible(method, params)
16
+ return Output.help_message if help_method? method
17
+
18
+ if method and params
19
+ call_method method, params
20
+ else
21
+ Output.invalid_command
22
+ Output.help_message
23
+ end
24
+ end
25
+
26
+ def call_method(method, params)
27
+ MethodCaller.call method, params
28
+ end
29
+
30
+ def help_method?(method)
31
+ method == "help"
32
+ end
33
+
34
+ end
35
+
36
+ module MethodParser
37
+ class << self
38
+
39
+ def parse(argv)
40
+ method = argv.shift
41
+ return nil unless method
42
+ return nil unless /^[a-z_]+$/.match(method)
43
+ method
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ module ParamsParser
50
+ class << self
51
+
52
+ def parse(argv)
53
+ params = {}
54
+ argv.each do |rawparam|
55
+ param = parse_rawparam rawparam
56
+ params.update(param) if param
57
+ end
58
+ return nil if params.length != argv.length
59
+ params
60
+ end
61
+
62
+ def parse_rawparam(raw)
63
+ m = /--(.*)=(.*)/.match(raw)
64
+ {m[1].to_sym => m[2]} if m
65
+ end
66
+
67
+ end
68
+ end
69
+
70
+ module MethodCaller
71
+ class << self
72
+
73
+ def call(method, params)
74
+ init_params = filter_init_params params
75
+ begin
76
+ c = FMS::Client.new(init_params)
77
+ Output.stdout c.send(method.to_sym, params)
78
+ rescue ArgumentError => error
79
+ Output.stderr error.message.gsub(':','--')
80
+ rescue NoMethodError => error
81
+ Output.stderr error.message
82
+ end
83
+ end
84
+
85
+ def filter_init_params(params)
86
+ init_params = {}
87
+ params.each_pair do |key,value|
88
+ if init_param? key
89
+ init_params[key] = value
90
+ params.delete key
91
+ end
92
+ end
93
+ init_params
94
+ end
95
+
96
+ def init_param?(param)
97
+ [:host, :port, :auser, :apswd].include? param
98
+ end
99
+
100
+ end
101
+ end
102
+
103
+ module Output
104
+ class << self
105
+
106
+ @@buffer_stdout = []
107
+ @@buffer_stderr = []
108
+
109
+ def invalid_command
110
+ stderr "Invalid command format."
111
+ end
112
+
113
+ def help_message
114
+ stdout "\nCommand line interface to Flash Media Server Administration API"
115
+ stdout "\nUsage:"
116
+ stdout "\n $ fmsapi <method_name> --host=<fms host> [other params]"
117
+ stdout "\nJust pick a method from the documentation and replace convention "
118
+ stdout "from camelCase to underscore_case (same for the parameters)"
119
+ stdout "\nExample:"
120
+ stdout "\n $ fmsapi reload_app --host=fms.example.com --auser=fms --apswd=secret --app_inst=live"
121
+ stdout "\nFMS Admin API documentation: #{FMS::Docs}"
122
+ stdout "This project documentation: http://github.com/igorsobreira/fms-admin-api"
123
+ end
124
+
125
+ def stdout(out)
126
+ @@buffer_stdout << out
127
+ end
128
+
129
+ def stderr(err)
130
+ @@buffer_stderr << err
131
+ end
132
+
133
+ def flush
134
+ puts @@buffer_stderr.join("\n").red
135
+ puts @@buffer_stdout.join("\n")
136
+ end
137
+
138
+ end
139
+ end
140
+
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ module FMS
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'tests_helper'
2
+
3
+ class ClientTests < BaseTestCase
4
+
5
+ def test_should_require_host_parameter_and_provide_defaults_to_others
6
+ c = FMS::Client.new(:host => "fms.example.com")
7
+ assert_equal("fms.example.com", c.host)
8
+ assert_equal(1111, c.port)
9
+ assert_equal({:auser => "fms", :apswd => "fms"}, c.base_params)
10
+ end
11
+
12
+ def test_should_raise_error_if_host_not_provided
13
+ assert_raises(ArgumentError) { FMS::Client.new }
14
+ end
15
+
16
+ def test_should_build_url_based_on_method_name
17
+ c = FMS::Client.new(:host => "fms.example.com",
18
+ :apswd => "secret")
19
+ c.get_apps
20
+ assert_requested(:get, "http://fms.example.com:1111/admin/getApps?apswd=secret&auser=fms")
21
+ end
22
+
23
+ def test_should_use_params_to_build_url
24
+ c = FMS::Client.new(:host => 'fms.example.com')
25
+ c.get_apps(:force => true, :verbose => false)
26
+ assert_requested(:get, "http://fms.example.com:1111/admin/getApps?apswd=fms&auser=fms&force=true&verbose=false")
27
+ end
28
+
29
+ def test_should_camelize_parameters_to_build_url
30
+ c = FMS::Client.new(:host => 'fms.example.com')
31
+ c.reload_app(:app_inst => 'live/cam1')
32
+ assert_requested(:get, "http://fms.example.com:1111/admin/reloadApp?appInst=live/cam1&apswd=fms&auser=fms")
33
+ end
34
+
35
+ end
@@ -0,0 +1,97 @@
1
+ require 'tests_helper'
2
+
3
+ module FMS::CmdLine::Output
4
+ def self.flush
5
+ [@@buffer_stdout, @@buffer_stderr]
6
+ end
7
+
8
+ def self.clear
9
+ @@buffer_stderr = []
10
+ @@buffer_stdout = []
11
+ end
12
+ end
13
+
14
+ class CommandLineTests < BaseTestCase
15
+
16
+ # TODO: test mocked outputs
17
+
18
+ def teardown
19
+ super
20
+ FMS::CmdLine::Output.clear
21
+ end
22
+
23
+ def test_should_call_method_based_on_command
24
+ run_command ['get_apps', '--host=fms.example.com']
25
+ assert_requested(:get, "http://fms.example.com:1111/admin/getApps?apswd=fms&auser=fms")
26
+ end
27
+
28
+ def test_should_call_method_passing_command_parameters
29
+ run_command ['get_apps', '--host=fms.example.com', '--force=true']
30
+ assert_requested(:get, "http://fms.example.com:1111/admin/getApps?apswd=fms&auser=fms&force=true")
31
+ end
32
+
33
+ def test_should_require_host_parameter
34
+ run_command ['get_apps']
35
+ assert_not_requested(:get, /.*/)
36
+ assert_command_stderr_contains("--host option is required")
37
+ end
38
+
39
+ def test_should_show_help_if_invalid_command
40
+ stub_request_to_404 "http://fms.nonexistent.com:1111/admin/invalidCommand?apswd=fms&auser=fms"
41
+ run_command ["invalid_command", "--host=fms.nonexistent.com"]
42
+ assert_requested(:get, "http://fms.nonexistent.com:1111/admin/invalidCommand?apswd=fms&auser=fms")
43
+ assert_command_stderr_contains('"invalidCommand" is not a valid API method')
44
+ end
45
+
46
+ def test_show_error_msg_if_no_command_nor_options_provided
47
+ run_command []
48
+ assert_invalid_command
49
+ end
50
+
51
+ def test_show_error_msg_if_options_informed_before_command
52
+ run_command ["--options-first=foo", "get_apps"]
53
+ assert_invalid_command
54
+ end
55
+
56
+ def test_options_should_use_equal_sign_to_define_its_value
57
+ run_command ["get_apps", "--force", "true"]
58
+ assert_invalid_command
59
+ end
60
+
61
+ def test_help_command_should_show_usage_format
62
+ run_command ["help"]
63
+ assert_help_message
64
+ end
65
+
66
+ def run_command(argv)
67
+ FMS::CmdLine.parse(argv)
68
+ end
69
+
70
+ def stub_request_to_404(url)
71
+ WebMock.reset!
72
+ stub_request(:get, url).to_return(:status => 404)
73
+ end
74
+
75
+ def assert_command_stderr_contains(msg)
76
+ cmd_stdout, cmd_stderr = FMS::CmdLine::Output.flush
77
+ assert_includes(cmd_stderr, msg)
78
+ end
79
+
80
+ def assert_command_stdout_contains(msg)
81
+ cmd_stdout, cmd_stderr = FMS::CmdLine::Output.flush
82
+ assert_includes(cmd_stdout, msg)
83
+ end
84
+
85
+ def assert_invalid_command
86
+ assert_not_requested(:get, /.*/)
87
+ assert_command_stderr_contains("Invalid command format.")
88
+ assert_help_message
89
+ end
90
+
91
+ def assert_help_message
92
+ assert_not_requested(:get, /.*/)
93
+ assert_command_stdout_contains("\nUsage:")
94
+ assert_command_stdout_contains("\nExample:")
95
+ end
96
+
97
+ end
@@ -0,0 +1,17 @@
1
+ require 'minitest/autorun'
2
+ require 'webmock'
3
+
4
+ require 'fms'
5
+
6
+ class BaseTestCase < MiniTest::Unit::TestCase
7
+ include WebMock::API
8
+
9
+ def setup
10
+ stub_request(:get, /.*/)
11
+ end
12
+
13
+ def teardown
14
+ WebMock.reset!
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fms-admin-api
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Igor Sobreira
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2011-12-11 00:00:00 -02:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: activesupport
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: colorize
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ version_requirements: *id002
45
+ - !ruby/object:Gem::Dependency
46
+ name: webmock
47
+ prerelease: false
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :development
57
+ version_requirements: *id003
58
+ description: Ruby client and command line interface to Flash Media Server Administration API
59
+ email:
60
+ - igor@igorsobreira.com
61
+ executables:
62
+ - fmsapi
63
+ extensions: []
64
+
65
+ extra_rdoc_files: []
66
+
67
+ files:
68
+ - .rvmrc
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - README.md
72
+ - Rakefile
73
+ - bin/fmsapi
74
+ - fms-admin-api.gemspec
75
+ - lib/fms.rb
76
+ - lib/fms/client.rb
77
+ - lib/fms/cmdline.rb
78
+ - lib/fms/version.rb
79
+ - tests/client_tests.rb
80
+ - tests/cmdline_tests.rb
81
+ - tests/tests_helper.rb
82
+ has_rdoc: true
83
+ homepage: http://github.com/igorsobreira/fms-admin-api
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project: fms-admin-api
110
+ rubygems_version: 1.3.7
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Ruby client and command line interface to Flash Media Server Administration API
114
+ test_files: []
115
+