icwot 0.0.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73f448dc7e413e657d113e1fcd9cabfc6a41be7e
4
- data.tar.gz: 0ebeeb7bf3a5facabc9485810643cccd0d283c21
3
+ metadata.gz: 44ef186ed365a911a195d9f8ab987530fad72827
4
+ data.tar.gz: e04f2e3b74c1f06825c296674a90269f4c9c2ef0
5
5
  SHA512:
6
- metadata.gz: 90594c346e7a20dd281332f7e195372efb9fcd684048d3e031401b904f45dbb37c21fd5951cf68f7a443826b664a0590cd8adf1186ddb2a0cb6cda5a5181dfa0
7
- data.tar.gz: 15b78714b94cca8b4135f2234a0d53475c0d11722247b20f592b53ce4e9eda8176ca9daf98d2d3ed53ac602bca4527d0e72ab47b7b9fdd55d062a1edf5c96821
6
+ metadata.gz: 181b4a37d46af2eaddfa2a3c01c1587cbec499da05fd7ce4891b386fb950993546206f35c6cf366201b006cb44633fa16d652b854ec59790b8bdacd8923cf79b
7
+ data.tar.gz: 5c333aac859fe46fa61478411f5673abec035828e3a6565b6f09f61a786e2510aa73cf6dd1154e2d5d776734d3983109e31d0652e581534c32b2df94c739a716
data/.gitignore CHANGED
@@ -15,4 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- ./idea
18
+ .idea/
data/Gemfile CHANGED
@@ -5,3 +5,5 @@ gemspec
5
5
 
6
6
  gem 'sinatra', :github => 'sinatra/sinatra'
7
7
  gem 'thin'
8
+ gem 'rest-client', '~> 1.6.7'
9
+ gem 'json', '~> 1.8.1'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Icwot
2
2
 
3
- TODO: Write a gem description
3
+ RESTfull [sinatra](http://www.sinatrarb.com) server providing inversion of control for logging messages from another RESTfull application web-server
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,7 +10,7 @@ Add this line to your application's Gemfile:
10
10
 
11
11
  And then execute:
12
12
 
13
- $ bundle
13
+ $ bundle install
14
14
 
15
15
  Or install it yourself as:
16
16
 
@@ -18,7 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ In a terminal run:
22
+
23
+ $ icwot
24
+
25
+ It will start a [sinatra](http://www.sinatrarb.com) server on port 4567. Logging messages will be saved to : your-home-directory/log/icwot-msg.log
22
26
 
23
27
  ## Contributing
24
28
 
data/bin/icwot CHANGED
@@ -1,4 +1,128 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  # A simple tool to run server on the client side
4
- `bundle exec ruby client_server.rb -e production`
4
+
5
+ require 'socket'
6
+ require 'timeout'
7
+ require 'rest-client'
8
+ require 'json'
9
+
10
+ # determine if a port is used or not
11
+ # from http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open
12
+ def self.is_port_open?(ip, port)
13
+ begin
14
+ Timeout::timeout(1) do
15
+ begin
16
+ s = TCPSocket.new(ip, port)
17
+ s.close
18
+ return true
19
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
20
+ return false
21
+ end
22
+ end
23
+ rescue Timeout::Error
24
+ end
25
+ return false
26
+ end
27
+
28
+ class Client
29
+ attr_accessor :uri, :content_type
30
+
31
+ def initialize
32
+ @content_type = 'application/json'
33
+ end
34
+
35
+ def to_xml
36
+ "<client xmlns=\"http://jaxb.xwot.first.ch.unifr.diuf\"><uri>#{uri}</uri></client>"
37
+ end
38
+
39
+ def to_hash
40
+ {uri: uri}
41
+ end
42
+
43
+ def get_ip
44
+ ip=Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
45
+ ip.ip_address if ip
46
+ end
47
+
48
+ def to_s
49
+ case content_type
50
+ when 'application/json'
51
+ to_hash.to_json
52
+ when 'application/xml'
53
+ to_xml
54
+ end
55
+ end
56
+ end
57
+
58
+ if ARGV.empty?
59
+ puts 'you need to provide at least one argument! See icwot -h'
60
+ exit 0
61
+ end
62
+ produces = 'application/json'
63
+ accept = 'application/json'
64
+ port = 4567
65
+ host = ''
66
+ protocol = 'http://'
67
+ log_path = ''
68
+ ARGV.each_with_index do |a, index|
69
+ case a
70
+ when '-h'
71
+ puts 'Usage : icwot <host>
72
+ -h print help
73
+ -l the host is localhost
74
+ -c the content-type value for the header application/json by default
75
+ -a the accept value for the header text/plain by default
76
+ -p the port where to run the server
77
+ -t the protocol to use http:// by default
78
+ -l where to save the log by default your-home-directory/log/icwot-{port}-msg.log
79
+ host is where to register for the service.
80
+ '
81
+ exit 0
82
+ when '-l'
83
+ host='localhost'
84
+ when '-c'
85
+ produces = ARGV.delete_at index + 1
86
+ when '-a'
87
+ accept = ARGV.delete_at index + 1
88
+ when '-p'
89
+ port = ARGV.delete_at(index + 1).to_i
90
+ when '-t'
91
+ unless (temp = ARGV.delete_at(index + 1)).nil?
92
+ protocol = temp
93
+ end
94
+ when '-l'
95
+ log_path = ARGV.delete_at index + 1
96
+ else
97
+ host += a
98
+ end
99
+ end
100
+
101
+ if host == ''
102
+ puts 'Host must be set !'
103
+ exit 0
104
+ end
105
+
106
+ if port == 0
107
+ puts 'The port must be bigger than 0'
108
+ exit 0
109
+ end
110
+
111
+ if is_port_open?('localhost', port)
112
+ puts 'Port already in use ! Try another one.'
113
+ exit 0
114
+ end
115
+
116
+ client = Client.new
117
+ client.uri = "http://#{client.get_ip.to_s}:#{port}/"
118
+ client.content_type = produces
119
+
120
+
121
+ response = RestClient.post protocol+host, client.to_s, accept: accept, content_type: produces
122
+
123
+ unless response.code == 200
124
+ puts 'Error. Connection not establish with the server.'
125
+ exit 0
126
+ end
127
+
128
+ `sinatra_port=#{port} sinatra_log=#{log_path} bundle exec ruby lib/icwot.rb -e production -p #{port}`
data/icwot.gemspec CHANGED
@@ -18,9 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
21
+ spec.add_development_dependency 'rake'
23
22
 
23
+ spec.add_dependency 'bundler', '~> 1.3'
24
24
  spec.add_dependency 'sinatra', '~> 1.4.4'
25
25
  spec.add_dependency 'thin'
26
+ spec.add_dependency 'rest-client', '~> 1.6.7'
27
+ spec.add_dependency 'json', '~> 1.8.1'
26
28
  end
data/lib/icwot/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Icwot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1"
3
3
  end
data/lib/icwot.rb CHANGED
@@ -11,8 +11,9 @@ end
11
11
  # POST sur / avec le port 4567
12
12
  post '/' do
13
13
  content_type 'text/plain'
14
- # On enregistre dans un log spécial le body envoyé
14
+ # On enregistre dans un log spécial le body de la requête
15
15
  msg.info request.body.read
16
+ logger.info "message saved to #{self.class.logger_log_file.path}"
16
17
  # On retourne le code http 200 avec le texte 'ok'
17
18
  'ok'
18
19
  end
@@ -41,10 +42,10 @@ private
41
42
 
42
43
  # Si le fichier log/msg.log n'existe pas, on le créé, sinon on l'ouvre
43
44
  def create_or_open_log
44
- file_name = "msg.log"
45
- directory_path = "#{settings.root}/log/"
45
+ file_name = "icwot-#{ENV['sinatra_port']}-msg.log"
46
+ directory_path = "#{Dir.home}/log/"
46
47
  Dir.mkdir(directory_path) unless File.exists?(directory_path)
47
- file = File.new(directory_path+file_name, 'a+')
48
+ file = ((log_path = ENV['sinatra_log']) == '' && !File.exist?(log_path) ? File.new(directory_path+file_name, 'a+') : File.new(ENV['sinatra_log']))
48
49
  file.sync = true
49
50
  file
50
51
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: icwot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - leo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-04 00:00:00.000000000 Z
11
+ date: 2013-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
33
+ version: '1.3'
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sinatra
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.7
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.7
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 1.8.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 1.8.1
69
97
  description: 'icwot : RESTfull sinatra server providing inversion of control for logging
70
98
  messages from another web-server'
71
99
  email: