pickynode 0.1.2 → 0.1.3

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: d49df9c54a1dd3799f0b66b74f75f217560f283d
4
- data.tar.gz: 5cba4b0edea26ba858cb83dd2128d4b4cc45b71d
3
+ metadata.gz: f730e5368a59d75e087733cfb5cac8f39ee2205c
4
+ data.tar.gz: a2f0fa712034104a4b8142a46f350212964ee888
5
5
  SHA512:
6
- metadata.gz: 01e9aa203431912c58a97b78920c685e146f5150d0d1ee18d70999c702b17ec092bdfe480905345a1dace8318ee6fecb3c990ef3ca17c5041b5388ddad1ba52d
7
- data.tar.gz: d09e13a8f1cb105907833dc8fa69fdff3063a6e7791d70200b72d6ad8319e5639d01da3ee1775d1ff41d81a1f1dfa036bf3108f0b8b2349f546b428ba9755df8
6
+ metadata.gz: e10e3558312a30d47fb11421d3567ccf0c443fd9be43512450cef620f559d4262e1047dec7c084390b4a6ee4f30265983aa053d4a13e81fa7a1023f68f54b88f
7
+ data.tar.gz: 7bee810ab2b249a92e6cc159b44300cff1f88f94179f8a67a2538ce435ac74dc1b2f8ef9be5912ea2350b1f9825ae8d2a1dec56fe86adb21408457c440a444e9
data/.rubocop.yml CHANGED
@@ -1,4 +1,8 @@
1
+ ClassLength:
2
+ Enabled: false
1
3
  Metrics/BlockLength:
2
4
  ExcludedMethods: ['describe', 'context']
3
5
  Style/IndentHeredoc:
4
- Enabled: false
6
+ Enabled: false
7
+ Style/NumericPredicate:
8
+ Enabled: false
data/README.md CHANGED
@@ -45,7 +45,7 @@ pickynode --disconnect=USER_AGENT_FILTER
45
45
  ### Help:
46
46
 
47
47
  ```
48
- pickynode v0.1.2
48
+ pickynode v0.1.3
49
49
  Options:
50
50
  -a, --add=<s> Add node type
51
51
  -c, --connect=<s> Connect to node type
@@ -54,6 +54,7 @@ Options:
54
54
  -i, --info Local node info
55
55
  -o, --output Output commands
56
56
  -s, --disconnect=<s> Disconnect from node type
57
+ -l, --limit=<i> Limit number of nodes to add/connect
57
58
  -v, --version Print version and exit
58
59
  -h, --help Show this message
59
60
  ```
data/bin/pickynode CHANGED
@@ -12,6 +12,8 @@ opts = Trollop.options do
12
12
  opt :info, 'Local node info'
13
13
  opt :output, 'Output commands'
14
14
  opt :disconnect, 'Disconnect from node type', type: :string
15
+ opt :limit, 'Limit number of nodes to add/connect', type: :integer
15
16
  end
16
17
 
18
+ Trollop.die :limit, 'must be positive' if opts[:limit] && opts[:limit] <= 0
17
19
  Pickynode.new(opts).run
data/lib/pickynode.rb CHANGED
@@ -9,16 +9,23 @@ require 'uri'
9
9
  # Allows you to easily add/ban/connect/disconnect nodes
10
10
  # based on User Agent.
11
11
  class Pickynode
12
- VERSION = '0.1.2'
12
+ VERSION = '0.1.3'
13
13
 
14
14
  def initialize(opts = {})
15
15
  @opts = opts
16
16
  end
17
17
 
18
- def add(filter)
18
+ def add(filter, limit = nil)
19
19
  return unless filter
20
+
21
+ raise 'Limit must be greater than 0' unless valid_limit?(limit)
22
+
23
+ count = 0
20
24
  bitnode_addr_types.each do |k, v|
21
- run_cmd(%(bitcoin-cli addnode "#{k}" "add")) if v.include?(filter)
25
+ next unless v.include?(filter)
26
+ run_cmd(%(bitcoin-cli addnode "#{k}" "add"))
27
+ count += 1
28
+ break if limit == count
22
29
  end
23
30
  end
24
31
 
@@ -32,10 +39,17 @@ class Pickynode
32
39
  end
33
40
  end
34
41
 
35
- def connect(filter)
42
+ def connect(filter, limit = nil)
36
43
  return unless filter
44
+
45
+ raise 'Limit must be greater than 0' unless valid_limit?(limit)
46
+
47
+ count = 0
37
48
  bitnode_addr_types.each do |k, v|
38
- run_cmd(%(bitcoin-cli addnode "#{k}" "onetry")) if v.include?(filter)
49
+ next unless v.include?(filter)
50
+ run_cmd(%(bitcoin-cli addnode "#{k}" "onetry"))
51
+ count += 1
52
+ break if limit == count
39
53
  end
40
54
  end
41
55
 
@@ -55,14 +69,13 @@ class Pickynode
55
69
  end
56
70
 
57
71
  def run
58
- add(@opts[:add])
59
- connect(@opts[:connect])
72
+ add(@opts[:add], @opts[:limit])
73
+ connect(@opts[:connect], @opts[:limit])
60
74
 
61
75
  ban(@opts[:ban])
62
76
  disconnect(@opts[:disconnect])
63
77
 
64
- info if @opts[:info]
65
- display if @opts.values.select { |v| v }.empty?
78
+ display_info
66
79
  end
67
80
 
68
81
  def clear_cache
@@ -72,6 +85,11 @@ class Pickynode
72
85
 
73
86
  private
74
87
 
88
+ def display_info
89
+ info if @opts[:info]
90
+ display if @opts.values.select { |v| v }.empty?
91
+ end
92
+
75
93
  def run_cmd(cmd)
76
94
  puts "Running #{cmd}" if @opts[:output] || @opts[:debug]
77
95
  `#{cmd}` unless @opts[:debug]
@@ -111,4 +129,9 @@ class Pickynode
111
129
  def getpeerinfo
112
130
  `bitcoin-cli getpeerinfo`
113
131
  end
132
+
133
+ def valid_limit?(limit)
134
+ return true unless limit
135
+ limit > 0
136
+ end
114
137
  end
@@ -52,6 +52,33 @@ describe Pickynode do
52
52
  expect(subject).to_not receive(:run_cmd)
53
53
  subject.add('Anything')
54
54
  end
55
+
56
+ it 'should raise an error if the limit is <= 0' do
57
+ expect { subject.add('Anything', 0) }
58
+ .to raise_error('Limit must be greater than 0')
59
+ expect { subject.add('Anything', -5) }
60
+ .to raise_error('Limit must be greater than 0')
61
+ end
62
+
63
+ context 'with a limit' do
64
+ it 'should respect a limit parameter of 1' do
65
+ expect(subject).to receive(:bitnodes_snapshot).once
66
+ .and_return(BITNODES_SNAPSHOT)
67
+ expect(subject).to receive(:run_cmd)
68
+ .with('bitcoin-cli addnode "88.99.199.87:8333" "add"')
69
+ subject.add('i', 1)
70
+ end
71
+
72
+ it 'should respect a limit parameter greater than 1' do
73
+ expect(subject).to receive(:bitnodes_snapshot).once
74
+ .and_return(BITNODES_SNAPSHOT)
75
+ expect(subject).to receive(:run_cmd)
76
+ .with('bitcoin-cli addnode "88.99.199.87:8333" "add"')
77
+ expect(subject).to receive(:run_cmd)
78
+ .with(%(bitcoin-cli addnode "#{ipv6_ip}" "add"))
79
+ subject.add('i', 2)
80
+ end
81
+ end
55
82
  end
56
83
 
57
84
  describe '.ban' do
@@ -111,6 +138,33 @@ describe Pickynode do
111
138
  expect(subject).to_not receive(:run_cmd)
112
139
  subject.connect('Anything')
113
140
  end
141
+
142
+ it 'should raise an error if the limit is <= 0' do
143
+ expect { subject.connect('Anything', 0) }
144
+ .to raise_error('Limit must be greater than 0')
145
+ expect { subject.connect('Anything', -5) }
146
+ .to raise_error('Limit must be greater than 0')
147
+ end
148
+
149
+ context 'with a limit' do
150
+ it 'should respect a limit parameter of 1' do
151
+ expect(subject).to receive(:bitnodes_snapshot).once
152
+ .and_return(BITNODES_SNAPSHOT)
153
+ expect(subject).to receive(:run_cmd)
154
+ .with('bitcoin-cli addnode "88.99.199.87:8333" "onetry"')
155
+ subject.connect('i', 1)
156
+ end
157
+
158
+ it 'should respect a limit parameter greater than 1' do
159
+ expect(subject).to receive(:bitnodes_snapshot).once
160
+ .and_return(BITNODES_SNAPSHOT)
161
+ expect(subject).to receive(:run_cmd)
162
+ .with('bitcoin-cli addnode "88.99.199.87:8333" "onetry"')
163
+ expect(subject).to receive(:run_cmd)
164
+ .with(%(bitcoin-cli addnode "#{ipv6_ip}" "onetry"))
165
+ subject.connect('i', 2)
166
+ end
167
+ end
114
168
  end
115
169
 
116
170
  describe '.disconnect' do
@@ -182,7 +236,8 @@ describe Pickynode do
182
236
  add: 'Wanted',
183
237
  connect: 'Now',
184
238
  ban: 'Nefarious',
185
- disconnect: 'Fools'
239
+ disconnect: 'Fools',
240
+ limit: 1
186
241
  }
187
242
  end
188
243
 
@@ -191,9 +246,9 @@ describe Pickynode do
191
246
  .and_return(BITNODES_SNAPSHOT)
192
247
  expect(subject).to receive(:getpeerinfo).once
193
248
  .and_return(PEER_INFO)
194
- expect(subject).to receive(:add).with(opts[:add])
249
+ expect(subject).to receive(:add).with(opts[:add], opts[:limit])
195
250
  .and_call_original
196
- expect(subject).to receive(:connect).with(opts[:connect])
251
+ expect(subject).to receive(:connect).with(opts[:connect], opts[:limit])
197
252
  .and_call_original
198
253
  expect(subject).to receive(:ban).with(opts[:ban])
199
254
  .and_call_original
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pickynode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Ellithorpe