ruby-spimaging 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-spimaging.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'rack'
8
+ gem 'thin'
9
+ gem 'uuid'
10
+
11
+ gem 'net-ssh'
12
+ gem 'net-scp'
13
+ gem 'highline'
14
+ # gem 'ruby-debug-base'
15
+ # gem 'ruby-debug-ide'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Anthony Marendy
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/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Ruby::Spimaging
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby-spimaging'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby-spimaging
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
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require "bundler/gem_tasks"
2
+ require 'net/ssh'
3
+ require 'net/ssh/authentication/methods/password'
4
+ require 'net/scp'
5
+ require 'highline/import'
6
+
7
+ task :push => :build do
8
+ def upload(ssh)
9
+ filename = "spimaging-#{SPImaging::VERSION}.gem"
10
+ print "Uploading #{filename}..."
11
+ u = ssh.scp.upload("pkg/#{filename}", "/var/www/rubygems/gems/#{filename}") do
12
+ print '.'
13
+ end
14
+ u.wait
15
+ puts
16
+ ssh.exec! '/usr/local/rvm/bin/gem generate_index --directory=/var/www/rubygems'
17
+ end
18
+
19
+ opts = Net::SSH.configuration_for('sp43.sp.local')
20
+ begin
21
+ Net::SSH.start('sp43.sp.local', 'rubygems', :auth_methods => %w[publickey]) {|o| upload(o)}
22
+ rescue
23
+ password = ask("rubygems@sp43.sp.local's password: ") { |q| q.echo = false }
24
+ Net::SSH.start('sp43.sp.local', 'rubygems', :password => password) {|o| upload(o)}
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ module SPImaging
2
+ class Configuration
3
+ class <<self
4
+ attr_accessor :configuration_file
5
+
6
+ def current
7
+ env = nil
8
+ env = Rails.env if defined? Rails
9
+ env ||= ENV['RAILS_ENV']
10
+ env ||= ENV['RACK_ENV']
11
+ raise 'Unable to determine environment' if env.nil?
12
+
13
+ c = read
14
+ raise "Environment not defined in SPImaging configuration: #{env}" if c[env].nil?
15
+ (c['global'] || {}).merge(c[env])
16
+ end
17
+
18
+ private
19
+
20
+ def read
21
+ if defined? Rails
22
+ self.configuration_file ||= File.join(Rails.root, 'config', 'spimaging.yml')
23
+ elsif self.configuration_file.nil?
24
+ raise 'Unable to determine a default location for SPImaging configuration'
25
+ end
26
+
27
+ @configuration ||= YAML.load_file(self.configuration_file)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+
4
+ module SPImaging
5
+ class Protocol
6
+ def self.instance
7
+ @instance ||= self.new Configuration.current
8
+ end
9
+
10
+ def initialize(config)
11
+ @base_url = config['base_url']
12
+ @render_path = config['render_path']
13
+ @username = config['username']
14
+ @password = config['password']
15
+
16
+ url = URI.parse @base_url
17
+ @http = Net::HTTP.new url.host, url.port
18
+ @http.use_ssl = (url.scheme =~ /https/i)
19
+
20
+ end
21
+
22
+ def download_images(xml)
23
+ request = Net::HTTP::Post.new URI.join(@base_url, "image/retrieve").to_s
24
+ request.body = xml
25
+ request['Accept'] = 'text/html'
26
+ request['Content-Type'] = 'application/xml'
27
+ request.basic_auth @username, @password
28
+
29
+ response = http.request request
30
+ raise "Invalid response received: #{response.to_s}" unless response.code == '200'
31
+
32
+ SPImaging::ShowImagesResponse.new response.body, response.content_type, response['content-encoding'], response['content-disposition']
33
+ end
34
+
35
+ private
36
+
37
+ def http
38
+ @http.start unless @http.started?
39
+ @http
40
+ end
41
+
42
+ def render_url
43
+ URI.join(@base_url, @render_path).to_s
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ require 'nokogiri'
2
+
3
+ module SPImaging
4
+ class ShowImagesRequest
5
+ attr_accessor :uuids, :xml
6
+
7
+ def initialize(uuids)
8
+ self.uuids = uuids
9
+ end
10
+
11
+ def download
12
+
13
+ builder = Nokogiri::XML::Builder.new :encoding => 'utf-8' do |xml|
14
+ # Clients that treat this as a real builder sometimes need to call 'tag!'
15
+ # That's ok.
16
+ def xml.tag!(*args, &bl)
17
+ # Handle reserved names correctly
18
+ name = args.shift + '_'
19
+ send name, *args, &bl
20
+ end
21
+
22
+ xml.spimaging do
23
+ xml.uuids do
24
+ uuids.each do | uuid |
25
+ xml.uuid(uuid)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ @xml = builder.to_xml
32
+ SPImaging::Protocol.instance.download_images @xml
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ module SPImaging
2
+ class ShowImagesResponse
3
+ attr_accessor :body, :content_type, :content_encoding, :content_disposition
4
+
5
+ def initialize(body, content_type, content_encoding, content_disposition)
6
+ self.body = body
7
+ self.content_type = content_type
8
+ self.content_encoding = content_encoding
9
+ self.content_disposition = content_disposition
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module SPImaging
2
+ # Please use Semantic Versioning - http://semver.org/
3
+ VERSION = "1.0.0"
4
+ end
data/lib/spimaging.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "spimaging/version"
2
+
3
+ module SPImaging
4
+ autoload :Configuration, 'spimaging/configuration'
5
+ autoload :Protocol, 'spimaging/protocol'
6
+
7
+ autoload :ShowImagesRequest, 'spimaging/show_images_request'
8
+ autoload :ShowImagesResponse, 'spimaging/show_images_response'
9
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spimaging/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-spimaging"
8
+ spec.version = SPImaging::VERSION
9
+ spec.authors = ["Anthony Marendy"]
10
+ spec.email = ["amarendy@sp.com.au"]
11
+ spec.description = %q{Interfaces to spimaging}
12
+ spec.summary = %q{Downloads a collection of files from spimaging}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_runtime_dependency 'nokogiri', "1.5.9"
24
+ end
@@ -0,0 +1,40 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="FacetManager">
4
+ <facet type="gem" name="Ruby Gem">
5
+ <configuration>
6
+ <option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
7
+ <option name="GEM_APP_TEST_PATH" value="$MODULE_DIR$/test" />
8
+ <option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
9
+ </configuration>
10
+ </facet>
11
+ </component>
12
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
13
+ <exclude-output />
14
+ <content url="file://$MODULE_DIR$">
15
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
16
+ </content>
17
+ <orderEntry type="inheritedJdk" />
18
+ <orderEntry type="sourceFolder" forTests="false" />
19
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="daemons (v1.1.9, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.5, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="eventmachine (v1.0.3, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="highline (v1.6.21, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="macaddr (v1.7.1, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="net-scp (v1.2.1, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="net-ssh (v2.9.1, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.5.9, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="rack (v1.5.2, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.1.0, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.1.0, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.1.7, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.1.2, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.1.3, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.1.2, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="systemu (v2.6.4, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="thin (v1.6.3, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="uuid (v2.3.7, RVM: ree-1.8.7-2012.02 [global]) [gem]" level="application" />
38
+ </component>
39
+ </module>
40
+
data/spec/config.yml ADDED
@@ -0,0 +1,4 @@
1
+ test:
2
+ username: test
3
+ password: test
4
+ render_path: /render
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe SPImaging::ShowImagesRequest do
4
+ subject do
5
+ SPImaging::ShowImagesRequest.new(['UUID-1', 'UUID-2'])
6
+ end
7
+
8
+ it 'downloads the file' do
9
+ server.response = [200, {'content-type' => 'text/plain'}, "test file"]
10
+
11
+ response = subject.download
12
+ response.body.should == "test file"
13
+ response.content_type.should == 'text/plain'
14
+ response.content_encoding.should be_nil
15
+ end
16
+
17
+ it 'includes the content encoding' do
18
+ server.response = [200, {'content-type' => 'text/plain', 'content-encoding' => 'gzip'}, "test file"]
19
+
20
+ response = subject.download
21
+ response.body.should == "test file"
22
+ response.content_type.should == 'text/plain'
23
+ response.content_encoding.should == 'gzip'
24
+ end
25
+
26
+ it 'includes the content disposition' do
27
+ server.response = [200, {'content-type' => 'text/plain', 'content-disposition' => 'attachment; filename=test.txt'}, 'test file']
28
+ response = subject.download
29
+ response.content_disposition.should == 'attachment; filename=test.txt'
30
+ end
31
+
32
+ # it 'downloads real content' do
33
+ # real_request = SPImaging::ShowImagesRequest.new(['c49abdea-8402-4dd0-914a-e41f6f9c776a', 'ae3227e5-9575-446b-b657-72d8793de7e7'])
34
+ # response = real_request.download
35
+ # response.content_type.should == 'application/pdf'
36
+ # response.content_encoding.should == 'gzip'
37
+ # end
38
+ end
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'spimaging'
5
+ require 'test_server'
6
+
7
+ require 'tempfile'
8
+ require 'yaml'
9
+
10
+ RSpec.configure do |config|
11
+ config.add_setting :server
12
+ config.add_setting :spimaging_configuration
13
+
14
+ config.server = SPImaging::TestServer.new
15
+ config.server.start
16
+
17
+ config.include SPImaging::TestServerHelper
18
+
19
+ config.before :suite do
20
+ ENV['RACK_ENV'] = 'test'
21
+ ENV['RAILS_ENV'] = 'test'
22
+
23
+ yml = Tempfile.new(['spimaging-config', '.yml'])
24
+ template = YAML.load_file File.join(File.dirname(__FILE__), 'config.yml')
25
+ template['test'].merge!('base_url' => config.server.base_url)
26
+ yml.write YAML.dump(template)
27
+ yml.close
28
+
29
+ # yml = Tempfile.new(['spimaging-config', '.yml'])
30
+ # template = YAML.load_file File.join(File.dirname(__FILE__), 'config.yml')
31
+ # template['test'].merge!('base_url' => "http://localhost:8081/spimaging/", 'username' => 'secret', 'password' => 'secret')
32
+ # yml.write YAML.dump(template)
33
+ # yml.close
34
+
35
+ config.spimaging_configuration = yml
36
+ SPImaging::Configuration.configuration_file = yml.path
37
+ end
38
+
39
+ config.after :suite do
40
+ config.spimaging_configuration.unlink
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ require 'net/http'
2
+ require 'rack'
3
+ require 'rack/handler/thin'
4
+
5
+ module SPImaging
6
+ class TestServer
7
+ attr_accessor :env, :response
8
+
9
+ def initialize
10
+ @host = '127.0.0.1'
11
+ @port = find_available_port
12
+ end
13
+
14
+ def base_url
15
+ "http://#{@host}:#{@port}"
16
+ end
17
+
18
+ def url(path)
19
+ URI.join(base_url, path).to_s
20
+ end
21
+
22
+ def call(env)
23
+ self.env = env
24
+ response.tap{self.response = nil}
25
+ end
26
+
27
+ def start
28
+ @thread = Thread.new do
29
+ Rack::Handler::Thin.run self, :Host => @host, :Port => @port do |server|
30
+ @server = server
31
+ end
32
+ end
33
+ sleep(0.1) until @server and @server.running?
34
+ end
35
+
36
+ private
37
+
38
+ # Stolen from Capybara
39
+ def find_available_port
40
+ server = TCPServer.new(@host, 0)
41
+ server.addr[1]
42
+ ensure
43
+ server.close if server
44
+ end
45
+ end
46
+
47
+ module TestServerHelper
48
+ def server
49
+ RSpec.configuration.server
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-spimaging
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Marendy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-11-18 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: &id001 !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: "1.3"
20
+ type: :development
21
+ name: bundler
22
+ requirement: *id001
23
+ prerelease: false
24
+ - !ruby/object:Gem::Dependency
25
+ version_requirements: &id002 !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - &id004
28
+ - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: "0"
31
+ type: :development
32
+ name: rake
33
+ requirement: *id002
34
+ prerelease: false
35
+ - !ruby/object:Gem::Dependency
36
+ version_requirements: &id003 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.9
41
+ type: :runtime
42
+ name: nokogiri
43
+ requirement: *id003
44
+ prerelease: false
45
+ description: Interfaces to spimaging
46
+ email:
47
+ - amarendy@sp.com.au
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - .gitignore
56
+ - .idea/.name
57
+ - .idea/.rakeTasks
58
+ - .idea/compiler.xml
59
+ - .idea/encodings.xml
60
+ - .idea/misc.xml
61
+ - .idea/modules.xml
62
+ - .idea/scopes/scope_settings.xml
63
+ - .idea/vcs.xml
64
+ - .idea/workspace.xml
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/spimaging.rb
70
+ - lib/spimaging/configuration.rb
71
+ - lib/spimaging/protocol.rb
72
+ - lib/spimaging/show_images_request.rb
73
+ - lib/spimaging/show_images_response.rb
74
+ - lib/spimaging/version.rb
75
+ - ruby-spimaging.gemspec
76
+ - ruby-spimaging.iml
77
+ - spec/config.yml
78
+ - spec/show_images_request_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/test_server.rb
81
+ homepage: ""
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - *id004
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - *id004
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.11
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Downloads a collection of files from spimaging
104
+ test_files:
105
+ - spec/config.yml
106
+ - spec/show_images_request_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/test_server.rb