contracto 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 539e08faf6ec2176f82085c557bf154af64ef392
4
- data.tar.gz: e229d835ff7227b6a154261f33bd9f8a5e108dea
3
+ metadata.gz: 878cd05857b9fea82b17d7a5954073fe913f0dc5
4
+ data.tar.gz: 9dff5a72b030781058855be8144a5ae4fde47f97
5
5
  SHA512:
6
- metadata.gz: 42861847062ba686ec7bf2f1119d53bbb054b8292cc5fd79c51e203c922c4762a04c8354cebf7ba275c7dcad174a67cfd2c81870c8cd2acf7d405d1b4ac7aff8
7
- data.tar.gz: 73a5d1e7bc398c71e55862cb67222872a9a158c9a43a379c2d934990abf48fda3c81be107d5a2874b836570bf66bb3057bbd17c589c190acfd083025d4136f8f
6
+ metadata.gz: d7a9e463e67cdc5e56c0e194f075bf0d685e3e29066d3faf5414c17ec15f97ac5da739611edaac3d09caed076428823019e4e00c7a360157a885b445a453286a
7
+ data.tar.gz: 33451ca2f440f3ef74e8c016e5ff3496f66a1212c1972d2811f978470e8beea887d9f20256bcf370069cb7541fe36fe37d255fcea129c96b9b61c7bd36e1c1d5
data/contracto.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ['lib']
22
22
 
23
23
  spec.add_runtime_dependency 'sinatra', '~> 1.4'
24
+ spec.add_runtime_dependency 'daemons', '~> 1.2'
24
25
 
25
26
  spec.add_development_dependency 'rspec', '~> 3.2'
26
27
  spec.add_development_dependency 'bundler', '~> 1.7'
@@ -1,11 +1,18 @@
1
1
  class Contracto::Config
2
+ extend Contracto::Constants
3
+
2
4
  class << self
3
- def repo_url=(repo_url)
4
- @repo_url = repo_url
5
+
6
+ attr_accessor :repo_url
7
+ attr_accessor :root_dir
8
+
9
+ def configure
10
+ yield self if block_given?
5
11
  end
6
12
 
7
- def repo_url
8
- @repo_url
13
+ def root_dir
14
+ @root_dir || default_root_dir
9
15
  end
16
+
10
17
  end
11
18
  end
@@ -2,48 +2,28 @@ module Contracto::Constants
2
2
  require 'fileutils'
3
3
 
4
4
  GEM_DIR = Gem::Specification.find_by_name('contracto').gem_dir
5
- ROOT_DIR = FileUtils.pwd
5
+ DEFAULT_ROOT_DIR = FileUtils.pwd
6
6
  CONTRACTO_DIR = '.contracto'
7
7
  CONTRACTO_TMP_DIR = '.tmp.contracto'
8
8
  RUBY_SERVER_DIR = "#{GEM_DIR}/lib/contracto/server/ruby"
9
- CONTRACT_FILENAME = 'contract.con.json'
9
+ CONTRACT_FILENAME = 'contract.con.json' # TODO: remove
10
10
  SAMPLE_CONTRACT_DIR = "#{GEM_DIR}/spec/fixtures"
11
- CONTRACT_PID_FILEPATH = "#{ROOT_DIR}/server.pid"
11
+ SERVER_PIDFILE_NAME = 'server'
12
12
  PORT = 54321
13
13
 
14
- def gem_dir
15
- GEM_DIR
14
+ %w(
15
+ gem_dir
16
+ default_root_dir
17
+ contracto_dir
18
+ contracto_tmp_dir
19
+ sample_contract_dir
20
+ contract_filename
21
+ server_pidfile_name
22
+ port
23
+ ).each do |method_name|
24
+ define_method method_name do
25
+ Contracto::Constants.const_get method_name.upcase
26
+ end
16
27
  end
17
28
 
18
- def root_dir
19
- ROOT_DIR
20
- end
21
-
22
- def contracto_dir
23
- CONTRACTO_DIR
24
- end
25
-
26
- def contracto_tmp_dir
27
- CONTRACTO_TMP_DIR
28
- end
29
-
30
- def ruby_server_dir
31
- RUBY_SERVER_DIR
32
- end
33
-
34
- def sample_contract_dir
35
- SAMPLE_CONTRACT_DIR
36
- end
37
-
38
- def contract_filename
39
- CONTRACT_FILENAME
40
- end
41
-
42
- def contract_pid_filepath
43
- CONTRACT_PID_FILEPATH
44
- end
45
-
46
- def port
47
- PORT
48
- end
49
29
  end
@@ -32,7 +32,7 @@ class Contracto::Contract::Response
32
32
  private
33
33
 
34
34
  def set_body
35
- @body = File.read(root_dir + body_path)
35
+ @body = File.read(Contracto::Config.root_dir + body_path)
36
36
  end
37
37
 
38
38
  def replace_params_placeholders_with_params_value
@@ -1,18 +1,22 @@
1
- require 'sinatra'
2
- require 'contracto'
1
+ require 'sinatra/base'
3
2
 
4
- get '/contracto' do
5
- "*** Contracto server is working! [#{Gem::Specification.find_by_name('contracto').version}] ***"
6
- end
3
+ class Contracto::Server < Sinatra::Base
7
4
 
8
- jsons_with_contracts = Dir["#{Contracto::Constants::ROOT_DIR}/**/*.con.json"].map do |file_with_contract|
9
- File.read file_with_contract
10
- end
5
+ set :port, Contracto::Constants::PORT
11
6
 
12
- Contracto::Parser.new(jsons_with_contracts).contracts.each do |contract|
13
- send(contract.http_method, contract.url_pattern) do
14
- contract.response_body(params)
7
+ get '/contracto' do
8
+ "*** Contracto server is working! [#{Gem::Specification.find_by_name('contracto').version}] ***"
9
+ end
10
+
11
+ jsons_with_contracts = Dir["#{Contracto::Config.root_dir}/**/*.con.json"].map do |file_with_contract|
12
+ File.read file_with_contract
13
+ end
14
+
15
+ Contracto::Parser.new(jsons_with_contracts).contracts.each do |contract|
16
+ send(contract.http_method, contract.url_pattern) do
17
+ contract.response_body(params)
18
+ end
15
19
  end
16
- end
17
20
 
21
+ end
18
22
 
@@ -21,15 +21,29 @@ class Contracto::SystemAction
21
21
  def start_server
22
22
  raise Contracto::ServerAlreadyRunningError if server_already_running?
23
23
 
24
- system "rackup #{ruby_server_dir}/config.ru -p #{port} -D -P #{contract_pid_filepath}"
25
- # TODO: loop below should terminate after n tries
26
- system "while ! echo exit | nc localhost #{port} > /dev/null && echo \"waiting for contracto server...\"; do sleep 1; done"
27
- test_request
24
+ require_relative 'server/ruby/server'
25
+ require 'daemons'
26
+
27
+ options = {
28
+ app_name: server_pidfile_name,
29
+ dir: Contracto::Config.root_dir,
30
+ dir_mode: :normal
31
+ }
32
+
33
+ Daemons.call(options) do
34
+ Contracto::Server.run!
35
+ end
36
+
37
+ 5.downto(0).each do |n|
38
+ sleep 1
39
+ puts "waiting for contracto server, #{n} tries left..."
40
+ break if test_request(silent: true)
41
+ end
28
42
  end
29
43
 
30
44
  def stop_server
31
45
  puts 'killing server...'
32
- Process.kill(15, File.read(contract_pid_filepath).to_i)
46
+ Process.kill(15, File.read("#{Contracto::Config.root_dir}/#{server_pidfile_name}.pid").to_i)
33
47
  puts '...server killed'
34
48
  rescue Errno::ENOENT
35
49
  puts 'could not kill server (pidfile not found)'
@@ -60,7 +74,7 @@ class Contracto::SystemAction
60
74
  end
61
75
 
62
76
  def contract_already_exists?
63
- File.exist?("#{root_dir}/#{contract_filename}")
77
+ File.exist?("#{Contracto::Config.root_dir}/#{contract_filename}")
64
78
  end
65
79
 
66
80
  def server_already_running?
@@ -1,3 +1,3 @@
1
1
  module Contracto
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
data/lib/contracto.rb CHANGED
@@ -8,4 +8,8 @@ module Contracto
8
8
  require_relative 'contracto/parser'
9
9
  require_relative 'contracto/system_action_chain'
10
10
  require_relative 'contracto/command'
11
+
12
+ def self.configure(&block)
13
+ Contracto::Config.configure(&block)
14
+ end
11
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contracto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kacper Walanus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-11 00:00:00.000000000 Z
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: daemons
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement