nsicloudooo 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
-
5
+ gem 'sinatra'
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
data/Rakefile CHANGED
@@ -29,11 +29,7 @@ require 'rspec/core'
29
29
  require 'rspec/core/rake_task'
30
30
  RSpec::Core::RakeTask.new(:spec) do |spec|
31
31
  spec.pattern = FileList['spec/**/*_spec.rb']
32
- end
33
-
34
- RSpec::Core::RakeTask.new(:rcov) do |spec|
35
- spec.pattern = 'spec/**/*_spec.rb'
36
- spec.rcov = true
32
+ spec.rspec_opts = "--color --format nested"
37
33
  end
38
34
 
39
35
  task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -0,0 +1,98 @@
1
+ require "json"
2
+ require "net/http"
3
+ require File.dirname(__FILE__) + '/errors'
4
+
5
+ module NSICloudooo
6
+ class Client
7
+
8
+ def initialize(url)
9
+ user_and_pass = url.match(/(\w+):(\w+)/)
10
+ @user, @password = user_and_pass[1], user_and_pass[2]
11
+ @url = url.match(/@(.*):/)[1]
12
+ @port = url.match(/([0-9]+)(\/)?$/)[1]
13
+ end
14
+
15
+ # Send a document be granulated by a nsi.cloudooo node
16
+ #
17
+ # @param [Hash] options used to send a document to be graulated
18
+ # @option options [String] file the base64 encoded file to be granulated
19
+ # @option options [String] filename the filename of the document
20
+ # @note the filename is very importante, the cloudooo node will convert the document based on its filename, if necessary
21
+ # @option options [String] doc_link link to the document that'll be granulated
22
+ # @note if provided both doc_link and file options, file will be ignored and the client will download the document instead
23
+ # @option options [String] callback a callback url to the file granulation
24
+ # @option options [String] verb the callback request verb, when not provided, nsi.cloudooo default to POST
25
+ #
26
+ # @example A simple granulation
27
+ # doc = Base64.encode64(File.new('document.odt', 'r').read)
28
+ # nsicloudooo.granulate(:file => doc, :filename => 'document.odt')
29
+ # @example Downloading and granulating from web
30
+ # nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt')
31
+ # @example Sending a callback url
32
+ # doc = Base64.encode64(File.new('document.odt', 'r').read)
33
+ # nsicloudooo.granulate(:file => doc, :filename => 'document.odt', :callback => 'http://google.com')
34
+ # nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt', :callback => 'http://google.com')
35
+ # @example Using a custom verb to the callback
36
+ # doc = Base64.encode64(File.new('document.odt', 'r').read)
37
+ # nsicloudooo.granulate(:file => doc, :filename => 'document.odt', :callback => 'http://google.com', :verb => "PUT")
38
+ # nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt', :callback => 'http://google.com', :verb => "PUT")
39
+ #
40
+ # @return [Hash] response
41
+ # * "key" [String] the key to access the granulated document if the sam node it was stored
42
+ def granulate(options = {})
43
+ @request_data = Hash.new
44
+ if options[:doc_link]
45
+ insert_download_data options
46
+ else
47
+ file_data = {:doc => options[:file], :filename => options[:filename]}
48
+ @request_data.merge! file_data
49
+ end
50
+ insert_callback_data options
51
+ request = prepare_request :POST, @request_data.to_json
52
+ execute_request(request)
53
+ end
54
+
55
+ # Verify if a document is already granulated
56
+ #
57
+ # @raise NSICloudooo::Errors::Client:KeyNotFoundError when an invalid document key is provided
58
+ #
59
+ # @param [String] key of the desired document
60
+ # @return [Hash] response
61
+ # * "done" [String] true if the document was already granualted, otherwise, false
62
+ #
63
+ # @example
64
+ # nsicloudooo.done("some key")
65
+ def done(key)
66
+ request = prepare_request :GET, {:key => key}.to_json
67
+ execute_request(request)
68
+ end
69
+
70
+ private
71
+
72
+ def insert_download_data(options)
73
+ download_data = {doc_link: options[:doc_link]}
74
+ @request_data.merge! download_data
75
+ end
76
+
77
+ def insert_callback_data(options)
78
+ @request_data[:callback] = options[:callback] unless options[:callback].nil?
79
+ @request_data[:verb] = options[:verb] unless options[:verb].nil?
80
+ end
81
+
82
+ def prepare_request(verb, body)
83
+ verb = verb.to_s.capitalize!
84
+ request = Net::HTTP.const_get("#{verb}").new '/'
85
+ request.body = body
86
+ request.basic_auth @user, @password
87
+ request
88
+ end
89
+
90
+ def execute_request(request)
91
+ response = Net::HTTP.start @url, @port do |http|
92
+ http.request(request)
93
+ end
94
+ raise NSICloudooo::Errors::Client::KeyNotFoundError if response.code == "404"
95
+ JSON.parse(response.body)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,72 @@
1
+ require "logger"
2
+ require "sinatra"
3
+ require "json"
4
+ require "thread"
5
+
6
+ module NSICloudooo
7
+ class Server < Sinatra::Application
8
+
9
+ configure :development do
10
+ Dir.mkdir('logs') unless File.exist?('logs')
11
+ $stderr.reopen("logs/output.log", "w")
12
+ end
13
+
14
+ def self.prepare
15
+ @@done = Hash.new
16
+ end
17
+
18
+ post "/" do
19
+ content_type :json
20
+ incoming = JSON.parse(request.body.read)
21
+ filename = incoming["filename"]
22
+ filename = File.basename(incoming["doc_link"]) if incoming.has_key? "doc_link"
23
+ callback = incoming["callback"] || nil
24
+ verb = incoming["verb"] || nil
25
+ if filename.include? "secs"
26
+ seconds = filename.split(".")[0].delete("secs").to_i
27
+ sleep seconds-1
28
+ end
29
+ {
30
+ key: "key for document #{filename}",
31
+ callback: callback,
32
+ verb: verb,
33
+ }.to_json
34
+ end
35
+
36
+ get "/" do
37
+ content_type :json
38
+ incoming = JSON.parse(request.body.read)
39
+ if incoming["key"].include? "secs"
40
+ unless @@done.has_key? incoming["key"]
41
+ @@done[incoming["key"]] = true
42
+ return {done: false}.to_json
43
+ else
44
+ return {done: true}.to_json
45
+ end
46
+ else
47
+ return 404 if incoming["key"].include? "dont"
48
+ end
49
+ end
50
+ end
51
+
52
+ class FakeServerManager
53
+
54
+ # Start the nsi.cloudooo fake server
55
+ #
56
+ # @param [Fixnum] port the port where the fake server will listen
57
+ # * make sure there's not anything else listenning on this port
58
+ def start_server(port=8886)
59
+ @thread = Thread.new do
60
+ Server.run! :port => port
61
+ end
62
+ sleep(1)
63
+ self
64
+ end
65
+
66
+ # Stop the SAM fake server
67
+ def stop_server
68
+ @thread.kill
69
+ self
70
+ end
71
+ end
72
+ end
data/lib/nsicloudooo.rb CHANGED
@@ -1,7 +1,11 @@
1
- require 'client'
2
- require 'fake_server'
1
+ require File.dirname(__FILE__) + '/nsicloudooo/client'
2
+ require File.dirname(__FILE__) + '/nsicloudooo/fake_server'
3
+ require File.dirname(__FILE__) + '/nsicloudooo/errors'
4
+
5
+ module NSISam
6
+ end
7
+
3
8
 
4
9
  module NSICloudooo
5
- include Client
6
- include FakeServer
7
10
  end
11
+
data/nsicloudooo.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "nsicloudooo"
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Douglas Camata"]
@@ -20,14 +20,14 @@ Gem::Specification.new do |s|
20
20
  ".document",
21
21
  ".rspec",
22
22
  "Gemfile",
23
- "Gemfile.lock",
24
23
  "LICENSE.txt",
25
24
  "README.rdoc",
26
25
  "Rakefile",
27
26
  "VERSION",
28
- "lib/client.rb",
29
- "lib/errors.rb",
30
27
  "lib/nsicloudooo.rb",
28
+ "lib/nsicloudooo/client.rb",
29
+ "lib/nsicloudooo/errors.rb",
30
+ "lib/nsicloudooo/fake_server.rb",
31
31
  "nsicloudooo.gemspec",
32
32
  "spec/nsicloudooo_spec.rb",
33
33
  "spec/spec_helper.rb"
@@ -42,12 +42,14 @@ Gem::Specification.new do |s|
42
42
  s.specification_version = 3
43
43
 
44
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<sinatra>, [">= 0"])
45
46
  s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
46
47
  s.add_development_dependency(%q<yard>, ["~> 0.7"])
47
48
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
49
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
50
51
  else
52
+ s.add_dependency(%q<sinatra>, [">= 0"])
51
53
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
52
54
  s.add_dependency(%q<yard>, ["~> 0.7"])
53
55
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
@@ -55,6 +57,7 @@ Gem::Specification.new do |s|
55
57
  s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
56
58
  end
57
59
  else
60
+ s.add_dependency(%q<sinatra>, [">= 0"])
58
61
  s.add_dependency(%q<rspec>, ["~> 2.8.0"])
59
62
  s.add_dependency(%q<yard>, ["~> 0.7"])
60
63
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
@@ -1,7 +1,72 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'base64'
3
+ require 'nsisam'
2
4
 
3
- describe "Nsicloudooo" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
5
+ $folder = File.expand_path(File.dirname(__FILE__))
6
+
7
+ describe NSICloudooo do
8
+
9
+ before :all do
10
+ @nsicloudooo = NSICloudooo::Client.new 'http://test:test@localhost:8886'
11
+ @fake_cloudooo = NSICloudooo::FakeServerManager.new.start_server
12
+ end
13
+
14
+ after :all do
15
+ @fake_cloudooo.stop_server
16
+ end
17
+
18
+ context "simple granulation" do
19
+ it "can send a document to be granulated by a cloudooo node" do
20
+ response = @nsicloudooo.granulate(:file => 'document', :filename => 'test.odt')
21
+ response.should_not be_nil
22
+ response["key"].should == "key for document test.odt"
23
+ end
24
+ end
25
+
26
+ context "granulation with conversion" do
27
+ it "can send document in a closed format to be granulated by a cloudooo node" do
28
+ response = @nsicloudooo.granulate(:file => 'document', :filename => 'test.doc')
29
+ response.should_not be_nil
30
+ response["key"].should == "key for document test.doc"
31
+ end
32
+ end
33
+
34
+ context "granulation with download" do
35
+ it "can download documents from a link to be granulated by a cloudooo node" do
36
+ response = @nsicloudooo.granulate(:doc_link => "http://doc_link/test.doc")
37
+ response.should_not be_nil
38
+ response["key"].should == "key for document test.doc"
39
+ end
6
40
  end
41
+
42
+ context "granualtion with callback" do
43
+ it "can send a document to be granulated by a cloudooo node and specify a callback url" do
44
+ response = @nsicloudooo.granulate(:file => 'document', :filename => 'test.odt', :callback => 'http://google.com')
45
+ response.should_not be_nil
46
+ response["key"].should == "key for document test.odt"
47
+ response["callback"].should == 'http://google.com'
48
+ end
49
+
50
+ it "can send a document to be granulated by a cloudooo node and specify the verb" do
51
+ response = @nsicloudooo.granulate(:file => 'document', :filename => 'test.odt', :callback => 'http://google.com', :verb => 'PUT')
52
+ response.should_not be_nil
53
+ response["key"].should == "key for document test.odt"
54
+ response["callback"].should == 'http://google.com'
55
+ response["verb"].should == 'PUT'
56
+ end
57
+ end
58
+
59
+ context "verify granulation" do
60
+ it "can verify is a granulation is done or no" do
61
+ key = @nsicloudooo.granulate(:file => 'document', :filename => '2secs.odt')["key"]
62
+ @nsicloudooo.done(key)["done"].should be_false
63
+ @nsicloudooo.done(key)["done"].should be_true
64
+ end
65
+
66
+ it "raises an error whentrying to verify if non-existing key is done" do
67
+ expect { @nsicloudooo.done("dont")["done"].should be_false }.to raise_error(NSICloudooo::Errors::Client::KeyNotFoundError)
68
+ end
69
+ end
70
+
7
71
  end
72
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nsicloudooo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,9 +11,20 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: &15924680 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *15924680
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rspec
16
- requirement: &70157113224960 !ruby/object:Gem::Requirement
27
+ requirement: &15923600 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ~>
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: 2.8.0
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *70157113224960
35
+ version_requirements: *15923600
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: yard
27
- requirement: &70157113223880 !ruby/object:Gem::Requirement
38
+ requirement: &15752860 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0.7'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70157113223880
46
+ version_requirements: *15752860
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rdoc
38
- requirement: &70157113222920 !ruby/object:Gem::Requirement
49
+ requirement: &15751620 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '3.12'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70157113222920
57
+ version_requirements: *15751620
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: bundler
49
- requirement: &70157113221160 !ruby/object:Gem::Requirement
60
+ requirement: &15750640 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 1.0.0
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70157113221160
68
+ version_requirements: *15750640
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: jeweler
60
- requirement: &70157113218800 !ruby/object:Gem::Requirement
71
+ requirement: &15749720 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ~>
@@ -65,7 +76,7 @@ dependencies:
65
76
  version: 1.8.3
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *70157113218800
79
+ version_requirements: *15749720
69
80
  description: A simple gem to access a nsi.cloudooo node
70
81
  email: d.camata@gmail.com
71
82
  executables: []
@@ -77,14 +88,14 @@ files:
77
88
  - .document
78
89
  - .rspec
79
90
  - Gemfile
80
- - Gemfile.lock
81
91
  - LICENSE.txt
82
92
  - README.rdoc
83
93
  - Rakefile
84
94
  - VERSION
85
- - lib/client.rb
86
- - lib/errors.rb
87
95
  - lib/nsicloudooo.rb
96
+ - lib/nsicloudooo/client.rb
97
+ - lib/nsicloudooo/errors.rb
98
+ - lib/nsicloudooo/fake_server.rb
88
99
  - nsicloudooo.gemspec
89
100
  - spec/nsicloudooo_spec.rb
90
101
  - spec/spec_helper.rb
@@ -103,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
114
  version: '0'
104
115
  segments:
105
116
  - 0
106
- hash: 2043752576424527838
117
+ hash: 1497111065395602476
107
118
  required_rubygems_version: !ruby/object:Gem::Requirement
108
119
  none: false
109
120
  requirements:
data/Gemfile.lock DELETED
@@ -1,33 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- diff-lcs (1.1.3)
5
- git (1.2.5)
6
- jeweler (1.8.3)
7
- bundler (~> 1.0)
8
- git (>= 1.2.5)
9
- rake
10
- rdoc
11
- json (1.6.5)
12
- rake (0.9.2.2)
13
- rdoc (3.12)
14
- json (~> 1.4)
15
- rspec (2.8.0)
16
- rspec-core (~> 2.8.0)
17
- rspec-expectations (~> 2.8.0)
18
- rspec-mocks (~> 2.8.0)
19
- rspec-core (2.8.0)
20
- rspec-expectations (2.8.0)
21
- diff-lcs (~> 1.1.2)
22
- rspec-mocks (2.8.0)
23
- yard (0.7.5)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- bundler (~> 1.0.0)
30
- jeweler (~> 1.8.3)
31
- rdoc (~> 3.12)
32
- rspec (~> 2.8.0)
33
- yard (~> 0.7)
data/lib/client.rb DELETED
@@ -1,55 +0,0 @@
1
- require "json"
2
- require "net/http"
3
- require "errors"
4
-
5
- module Client
6
-
7
- class Client
8
-
9
- def initialize(url)
10
- user_and_pass = url.match(/(\w+):(\w+)/)
11
- @user, @password = user_and_pass[1], user_and_pass[2]
12
- @url = url.match(/@(.*):/)[1]
13
- @port = url.match(/([0-9]+)(\/)?$/)[1]
14
- end
15
-
16
- def granulate_doc(file, filename, options = {})
17
- @request_data = Hash.new
18
- if options[:doc_link]
19
- insert_download_data options
20
- else
21
- @request_data.merge! {:file => file, :filename => filename}
22
- end
23
- insert_callback_data options
24
- request = prepare_request :POST, @request_data
25
- execute_request(request)
26
- end
27
-
28
- private
29
-
30
- def insert_download_data(options)
31
- @request_data.merge! {:doc_link => options[:doc_link]}
32
- end
33
-
34
- def insert_callback_data(options)
35
- @request_data[:callback] = options[:callback] unless options[:callback].nil?
36
- @request_data[:verb] = options[:verb] unless options[:verb].nil?
37
- end
38
-
39
- def prepare_request(verb, body)
40
- verb = verb.to_s.capitalize!
41
- request = Net::HTTP.const_get("#{verb}").new '/'
42
- request.body = body
43
- request.basic_auth @user, @password
44
- request
45
- end
46
-
47
- def execute_request(request)
48
- response = Net::HTTP.start @url, @port do |http|
49
- http.request(request)
50
- end
51
- raise NSICloudooo::Errors::Client::KeyNotFoundError if response.code == "404"
52
- JSON.parse(response.body)
53
- end
54
- end
55
- end
File without changes