watson-ruby 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/watson/remote.rb CHANGED
@@ -1,120 +1,120 @@
1
1
  module Watson
2
- # Remote class that handles all remote HTTP calls to Bitbucket and GitHub
3
- class Remote
2
+ # Remote class that handles all remote HTTP calls to Bitbucket and GitHub
3
+ class Remote
4
4
 
5
- # Debug printing for this class
6
- DEBUG = false
7
-
8
- class << self
9
-
10
- # Include for debug_print
11
- include Watson
5
+ # Debug printing for this class
6
+ DEBUG = false
7
+
8
+ class << self
9
+
10
+ # Include for debug_print
11
+ include Watson
12
12
 
13
- # Required libs
14
- require 'net/https'
15
- require 'uri'
16
- require 'json'
17
-
18
-
19
- # Default options hash for http_call
20
- # Will get merged with input argument hash to maintain defaults
21
- HTTP_opts = {
22
- :url => nil, #--> URL of endpoint [String]
23
- :ssl => false, #--> Use SSL in connection (HTTPS) (True/False]
24
- :method => nil, #--> GET or POST for respective HTTP method [String]
25
- :basic_auth => Array.new(0), #--> Array of username and pw to use for basic authentication
26
- # If empty, assume no user authentication [Array]
27
- :auth => nil, #--> Authentication token [String]
28
- :data => nil, #--> Hash of data to be POST'd in HTTP request [Hash]
29
- :verbose => false #--> Turn on verbose debug for this call [True/False]
30
- }
31
-
32
- ###########################################################
33
- # Generic HTTP call method
34
- # Accepts input hash of options that dictate how the HTTP call is to be made
35
- def http_call( opts )
36
- # [review] - Don't use DEBUG inside Remote class but pull from calling method's class?
37
- # [review] - Not sure if this is the best/proper way to do things but it works...
13
+ # Required libs
14
+ require 'net/https'
15
+ require 'uri'
16
+ require 'json'
17
+
18
+
19
+ # Default options hash for http_call
20
+ # Will get merged with input argument hash to maintain defaults
21
+ HTTP_opts = {
22
+ :url => nil, #--> URL of endpoint [String]
23
+ :ssl => false, #--> Use SSL in connection (HTTPS) (True/False]
24
+ :method => nil, #--> GET or POST for respective HTTP method [String]
25
+ :basic_auth => Array.new(0), #--> Array of username and pw to use for basic authentication
26
+ # If empty, assume no user authentication [Array]
27
+ :auth => nil, #--> Authentication token [String]
28
+ :data => nil, #--> Hash of data to be POST'd in HTTP request [Hash]
29
+ :verbose => false #--> Turn on verbose debug for this call [True/False]
30
+ }
31
+
32
+ ###########################################################
33
+ # Generic HTTP call method
34
+ # Accepts input hash of options that dictate how the HTTP call is to be made
35
+ def http_call( opts )
36
+ # [review] - Don't use DEBUG inside Remote class but pull from calling method's class?
37
+ # [review] - Not sure if this is the best/proper way to do things but it works...
38
38
 
39
- # Identify method entry
40
- debug_print "#{ self.class } : #{ __method__ }\n"
41
-
42
- # Merge default options with those passed in by user to form complete opt list
43
- opts = HTTP_opts.merge(opts)
39
+ # Identify method entry
40
+ debug_print "#{ self.class } : #{ __method__ }\n"
41
+
42
+ # Merge default options with those passed in by user to form complete opt list
43
+ opts = HTTP_opts.merge(opts)
44
44
 
45
45
 
46
- # Check URL in hash and get URI from it, then set up HTTP connection
47
- if opts[:url] =~ /^#{URI::regexp}$/
48
- _uri = URI(opts[:url])
49
- else
50
- debug_print "No URL specified in input opts, exiting HTTP call\n"
51
- return false
52
- end
53
-
54
- _http = Net::HTTP.new(_uri.host, _uri.port)
55
-
56
- # Print out verbose HTTP request if :verbose is set
57
- # For hardcore debugging when shit really doesn't work
58
- _http.set_debug_output $stderr if opts[:verbose] == true
59
-
60
- # If SSL is set in hash, set HTTP connection to use SSL
61
- _http.use_ssl = true if opts[:ssl] == true
46
+ # Check URL in hash and get URI from it, then set up HTTP connection
47
+ if opts[:url] =~ /^#{URI::regexp}$/
48
+ _uri = URI(opts[:url])
49
+ else
50
+ debug_print "No URL specified in input opts, exiting HTTP call\n"
51
+ return false
52
+ end
53
+
54
+ _http = Net::HTTP.new(_uri.host, _uri.port)
55
+
56
+ # Print out verbose HTTP request if :verbose is set
57
+ # For hardcore debugging when shit really doesn't work
58
+ _http.set_debug_output $stderr if opts[:verbose] == true
59
+
60
+ # If SSL is set in hash, set HTTP connection to use SSL
61
+ _http.use_ssl = true if opts[:ssl] == true
62
62
 
63
- # Create request based on HTTP method
64
- # [review] - Not sure if to fail with no method or default to GET?
65
- case opts[:method].upcase
66
- when "GET"
67
- _req = Net::HTTP::Get.new(_uri.request_uri)
68
-
69
- when "POST"
70
- _req = Net::HTTP::Post.new(_uri.request_uri)
71
-
72
- else
73
- debug_print "No method specified, cannot make HTTP request\n"
74
- return false
75
- end
63
+ # Create request based on HTTP method
64
+ # [review] - Not sure if to fail with no method or default to GET?
65
+ case opts[:method].upcase
66
+ when "GET"
67
+ _req = Net::HTTP::Get.new(_uri.request_uri)
68
+
69
+ when "POST"
70
+ _req = Net::HTTP::Post.new(_uri.request_uri)
71
+
72
+ else
73
+ debug_print "No method specified, cannot make HTTP request\n"
74
+ return false
75
+ end
76
76
 
77
- # Check for basic authentication key in hash
78
- if opts[:basic_auth].size == 2
79
- _req.basic_auth(opts[:basic_auth][0], opts[:basic_auth][1])
80
- end
77
+ # Check for basic authentication key in hash
78
+ if opts[:basic_auth].size == 2
79
+ _req.basic_auth(opts[:basic_auth][0], opts[:basic_auth][1])
80
+ end
81
81
 
82
- # Check for Authentication token key in hash to be used in header
83
- # I think this is pretty universal, but specifically works for GitHub
84
- if opts[:auth]
85
- _req["Authorization"] = "token #{ opts[:auth] }"
86
- end
82
+ # Check for Authentication token key in hash to be used in header
83
+ # I think this is pretty universal, but specifically works for GitHub
84
+ if opts[:auth]
85
+ _req["Authorization"] = "token #{ opts[:auth] }"
86
+ end
87
87
 
88
- # [review] - Add :data_format to use set_form_data vs json body?
89
- # For now, use Hash or Array, this is to differentiate between
90
- # putting post data in body vs putting it in the form
91
-
92
- # If a POST method, :data is present, and is a Hash, fill request body with data
93
- if opts[:method].upcase == "POST" && opts[:data] && opts[:data].is_a?(Hash)
94
- _req.body = opts[:data].to_json
95
- end
88
+ # [review] - Add :data_format to use set_form_data vs json body?
89
+ # For now, use Hash or Array, this is to differentiate between
90
+ # putting post data in body vs putting it in the form
91
+
92
+ # If a POST method, :data is present, and is a Hash, fill request body with data
93
+ if opts[:method].upcase == "POST" && opts[:data] && opts[:data].is_a?(Hash)
94
+ _req.body = opts[:data].to_json
95
+ end
96
96
 
97
- # If a POST method, :data is present, and is an Array, use set_form_data
98
- if opts[:method].upcase == "POST" && opts[:data] && opts[:data].is_a?(Array)
99
- _req.set_form_data(opts[:data][0])
100
- end
97
+ # If a POST method, :data is present, and is an Array, use set_form_data
98
+ if opts[:method].upcase == "POST" && opts[:data] && opts[:data].is_a?(Array)
99
+ _req.set_form_data(opts[:data][0])
100
+ end
101
101
 
102
- # Make HTTP request
103
- _resp = _http.request(_req)
102
+ # Make HTTP request
103
+ _resp = _http.request(_req)
104
104
 
105
- # Debug prints for status and message
106
- debug_print "HTTP Response Code: #{ _resp.code }\n"
107
- debug_print "HTTP Response Msg: #{ _resp.message }\n"
105
+ # Debug prints for status and message
106
+ debug_print "HTTP Response Code: #{ _resp.code }\n"
107
+ debug_print "HTTP Response Msg: #{ _resp.message }\n"
108
108
 
109
- # [fix] - Not sure if 401 is the only code that gives nonparseable body?
110
- # Figure out what other response codes are bad news for JSON.parse
111
- _json = _resp.code == "401" ? Hash.new : JSON.parse(_resp.body)
112
- debug_print "JSON: \n #{ _json }\n"
109
+ # [fix] - Not sure if 401 is the only code that gives nonparseable body?
110
+ # Figure out what other response codes are bad news for JSON.parse
111
+ _json = _resp.code == "401" ? Hash.new : JSON.parse(_resp.body)
112
+ debug_print "JSON: \n #{ _json }\n"
113
113
 
114
- # [review] - Returning hash of json + response the right thing to do?
115
- # return {:json => _json, :resp => _resp}
116
- return _json, _resp
117
- end
118
- end
119
- end
114
+ # [review] - Returning hash of json + response the right thing to do?
115
+ # return {:json => _json, :resp => _resp}
116
+ return _json, _resp
117
+ end
118
+ end
119
+ end
120
120
  end
@@ -1,3 +1,3 @@
1
1
  module Watson
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
data/spec/config_spec.rb CHANGED
@@ -2,117 +2,117 @@ module Watson
2
2
  require_relative 'helper_spec'
3
3
 
4
4
  describe Config do
5
- before(:each) do
6
- @config = Config.new
7
- @file = @config.instance_variable_get(:@rc_file)
8
- silence_output
9
- end
10
-
11
- after(:each) do
12
- File.delete(@file) if File.exists?(@file)
13
- enable_output
14
- end
15
-
16
-
17
- describe '#create_conf' do
18
- it 'create config file, return true' do
19
- @config.create_conf.should be_true
20
- File.exists?(@file).should be_true
21
- end
22
- end
23
-
24
- describe '#check_conf' do
25
- context 'config does not exist' do
26
- it 'create config file, return true' do
27
- @config.check_conf.should be_true
28
- File.exists?(@file).should be_true
29
- end
30
- end
31
-
32
- context 'config does exist' do
33
- it 'should return true' do
34
- @config.check_conf.should be_true
35
- File.exists?(@file).should be_true
36
- end
37
- end
38
- end
39
-
40
- describe '#read_conf' do
41
- context 'config does not exist' do
42
- it 'no config, return false' do
43
- @config.read_conf.should be_false
44
- end
45
- end
46
-
47
- context 'config does exist' do
48
- before { @config.create_conf }
49
-
50
- it 'return true, values match default config' do
51
- @config.create_conf.should be_true
52
- @config.read_conf.should be_true
53
- @config.dir_list.should include('.')
54
- @config.tag_list.should include('fix', 'review', 'todo')
55
- @config.ignore_list.should include('.git', '*.swp')
56
-
57
- end
58
- end
59
- end
60
-
61
- describe '#update_conf' do
62
- before do
63
- @config.check_conf
64
- @config.parse_depth = 1000
65
- @config.update_conf('parse_depth')
66
- end
67
-
68
- it 'updated config.parse_depth should be 1000' do
69
- @new_config = Config.new
70
- @new_config.check_conf.should be_true
71
- @new_config.read_conf.should be_true
72
- @new_config.parse_depth.to_i.should eql 1000
73
- end
74
- end
75
-
76
- # [review] - Should this be #initialize or #new?
77
- describe '#initialize' do
78
- it 'should initialize all member vars' do
79
- @config.cl_entry_set.should be_false
80
- @config.cl_tag_set.should be_false
81
- @config.cl_ignore_set.should be_false
82
-
83
- @config.use_less.should be_false
84
-
85
- @config.ignore_list.should == []
86
- @config.dir_list.should == []
87
- @config.file_list.should == []
88
- @config.tag_list.should == []
89
-
90
- @config.remote_valid.should be_false
91
-
92
- @config.github_valid.should be_false
93
- @config.github_api.should == ''
94
- @config.github_repo.should == ''
95
- @config.github_issues.should == {:open => Hash.new(),
96
- :closed => Hash.new() }
97
-
98
- @config.bitbucket_valid.should be_false
99
- @config.bitbucket_api.should == ''
100
- @config.bitbucket_repo.should == ''
101
- @config.bitbucket_issues.should == {:open => Hash.new(),
102
- :closed => Hash.new() }
103
-
104
- end
105
- end
106
-
107
- describe '#run' do
108
- it 'should populate all member vars' do
109
- @config.run
110
- @config.ignore_list.should include('.git', '*.swp')
111
- @config.tag_list.should include('fix', 'review', 'todo')
112
- @config.dir_list.should include('.')
113
- end
114
-
115
- end
5
+ before(:each) do
6
+ @config = Config.new
7
+ @file = @config.instance_variable_get(:@rc_file)
8
+ silence_output
9
+ end
10
+
11
+ after(:each) do
12
+ File.delete(@file) if File.exists?(@file)
13
+ enable_output
14
+ end
15
+
16
+
17
+ describe '#create_conf' do
18
+ it 'create config file, return true' do
19
+ @config.create_conf.should be_true
20
+ File.exists?(@file).should be_true
21
+ end
22
+ end
23
+
24
+ describe '#check_conf' do
25
+ context 'config does not exist' do
26
+ it 'create config file, return true' do
27
+ @config.check_conf.should be_true
28
+ File.exists?(@file).should be_true
29
+ end
30
+ end
31
+
32
+ context 'config does exist' do
33
+ it 'should return true' do
34
+ @config.check_conf.should be_true
35
+ File.exists?(@file).should be_true
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#read_conf' do
41
+ context 'config does not exist' do
42
+ it 'no config, return false' do
43
+ @config.read_conf.should be_false
44
+ end
45
+ end
46
+
47
+ context 'config does exist' do
48
+ before { @config.create_conf }
49
+
50
+ it 'return true, values match default config' do
51
+ @config.create_conf.should be_true
52
+ @config.read_conf.should be_true
53
+ @config.dir_list.should include('.')
54
+ @config.tag_list.should include('fix', 'review', 'todo')
55
+ @config.ignore_list.should include('.git', '*.swp')
56
+
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#update_conf' do
62
+ before do
63
+ @config.check_conf
64
+ @config.parse_depth = 1000
65
+ @config.update_conf('parse_depth')
66
+ end
67
+
68
+ it 'updated config.parse_depth should be 1000' do
69
+ @new_config = Config.new
70
+ @new_config.check_conf.should be_true
71
+ @new_config.read_conf.should be_true
72
+ @new_config.parse_depth.to_i.should eql 1000
73
+ end
74
+ end
75
+
76
+ # [review] - Should this be #initialize or #new?
77
+ describe '#initialize' do
78
+ it 'should initialize all member vars' do
79
+ @config.cl_entry_set.should be_false
80
+ @config.cl_tag_set.should be_false
81
+ @config.cl_ignore_set.should be_false
82
+
83
+ @config.use_less.should be_false
84
+
85
+ @config.ignore_list.should == []
86
+ @config.dir_list.should == []
87
+ @config.file_list.should == []
88
+ @config.tag_list.should == []
89
+
90
+ @config.remote_valid.should be_false
91
+
92
+ @config.github_valid.should be_false
93
+ @config.github_api.should == ''
94
+ @config.github_repo.should == ''
95
+ @config.github_issues.should == {:open => Hash.new(),
96
+ :closed => Hash.new() }
97
+
98
+ @config.bitbucket_valid.should be_false
99
+ @config.bitbucket_api.should == ''
100
+ @config.bitbucket_repo.should == ''
101
+ @config.bitbucket_issues.should == {:open => Hash.new(),
102
+ :closed => Hash.new() }
103
+
104
+ end
105
+ end
106
+
107
+ describe '#run' do
108
+ it 'should populate all member vars' do
109
+ @config.run
110
+ @config.ignore_list.should include('.git', '*.swp')
111
+ @config.tag_list.should include('fix', 'review', 'todo')
112
+ @config.dir_list.should include('.')
113
+ end
114
+
115
+ end
116
116
 
117
117
  end
118
118