higo 0.0.1.alpha → 0.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 +4 -4
- data/README.md +3 -2
- data/lib/higo/configurable.rb +43 -21
- data/lib/higo/transport/file.rb +47 -0
- data/lib/higo/transport/web.rb +61 -0
- data/lib/higo/transport.rb +6 -0
- data/lib/higo/version.rb +1 -1
- data/lib/higo.rb +11 -2
- data/test/test_configurable.rb +16 -0
- data/test/test_file_transport.rb +18 -0
- data/test/test_helper.rb +1 -1
- data/test/test_web_transport.rb +26 -0
- metadata +11 -6
- /data/test/{test_higo_inheritance.rb → test_higo_included.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e64120e8327764a40c07d0d79c7efc78a1c4922d
|
4
|
+
data.tar.gz: 45fa3ad29a672badfa2e76936d92b027dec63a04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 642bd77bd34f6661554ce6d06a9936a0d20229f2a4523d81f002666e612789c4810bf91a1d875b57ed17e250f1c0d862290421b297801426d44cd2b5733e2cee
|
7
|
+
data.tar.gz: 1db2709a88a6d3c6746386ab75ce7d0e621217dad1c155c71f81a7aecfdf8bc78133bcb6b28d149a719e4702a37651402e0ce92c778ede8f6722491dc40f3279
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
`gem install higo`
|
5
5
|
|
6
6
|
Detailed usage examples can be found in the `/examples` directory.
|
7
|
+
Documentation can be found [here](http://www.rubydoc.info/github/rhodee/higo/master).
|
7
8
|
|
8
9
|
## Gem Story - TL;DR
|
9
10
|
Higo creates 'config' objects dynamic using the well understood `configure` block:
|
@@ -32,13 +33,13 @@ were taken. Sure I could've named it `figy`, but that would be too confusing. So
|
|
32
33
|
|
33
34
|
|
34
35
|
__Note__-if you already defined `method_missing` in your class `Higo` will honor that and that is likely not what you
|
35
|
-
want. Higo might
|
36
|
+
want. Higo might not be right for you at this stage.
|
36
37
|
|
37
38
|
### To Do List
|
38
39
|
|
39
40
|
* ~~Generate arbitrary configuration values~~
|
40
41
|
* Improve support for subclasses that define `method_missing`
|
41
|
-
* Read from an external source
|
42
|
+
* ~~Read from an external source~~
|
42
43
|
* Improve documentation
|
43
44
|
|
44
45
|
## Contributing
|
data/lib/higo/configurable.rb
CHANGED
@@ -3,13 +3,14 @@
|
|
3
3
|
#
|
4
4
|
module Higo
|
5
5
|
def self.included(mod)
|
6
|
+
# If the module is included write to a variable
|
6
7
|
mod.extend(Macros)
|
7
8
|
end
|
8
9
|
|
9
10
|
module Macros
|
10
|
-
def configure(&block)
|
11
|
-
configuration ||= Higo::Configurable.
|
12
|
-
configuration.instance_eval(&block)
|
11
|
+
def configure(path:nil, &block)
|
12
|
+
configuration ||= Higo::Configurable.generate_configurable(path)
|
13
|
+
configuration.instance_eval(&block) if block_given?
|
13
14
|
return configuration
|
14
15
|
end
|
15
16
|
end
|
@@ -20,12 +21,36 @@ module Higo
|
|
20
21
|
#
|
21
22
|
# @param [Proc]
|
22
23
|
# @return [Configurable] responds to getter, setter, predicate methods defined
|
23
|
-
def self.configure(&block)
|
24
|
-
configuration = generate_configurable
|
25
|
-
configuration.instance_eval(&block)
|
24
|
+
def self.configure(path:nil, &block)
|
25
|
+
configuration = generate_configurable(path)
|
26
|
+
configuration.instance_eval(&block) if block_given?
|
26
27
|
return configuration
|
27
28
|
end
|
28
29
|
|
30
|
+
def self.inherited(mod)
|
31
|
+
# If the class is inherited write to a variable
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(file)
|
35
|
+
if file
|
36
|
+
values = if file =~ /http/
|
37
|
+
Transport::Web.transport(path: file).finish
|
38
|
+
elsif file =~ /.?(txt|json)/
|
39
|
+
Transport::File.transport(path: file).finish
|
40
|
+
else
|
41
|
+
raise 'Unable to read this type of file'
|
42
|
+
end
|
43
|
+
values.each do |key,value|
|
44
|
+
self.class.__send__(:attr_accessor, key)
|
45
|
+
self.class.__send__(:define_method, "#{key}?", proc { !!value })
|
46
|
+
instance_variable_set("@#{key}", value)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
super()
|
50
|
+
end
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
29
54
|
def respond_to_missing?(method_name, include_private = false)
|
30
55
|
super unless self.instance_variables.include?(convert_method_to_symbol(method_name))
|
31
56
|
end
|
@@ -34,32 +59,29 @@ module Higo
|
|
34
59
|
# all unknown methods are defined as getter, setters and predicate methods
|
35
60
|
def method_missing(meth, *args, &block)
|
36
61
|
|
37
|
-
|
38
|
-
|
39
|
-
|
62
|
+
# check for a variable and then delegate to that method missing implementation
|
63
|
+
# should instruct user to call super() as well
|
64
|
+
klass = self.class
|
65
|
+
super(meth, *args, &block) if respond_to?(meth)
|
40
66
|
|
41
|
-
|
42
|
-
|
67
|
+
ivar = convert_method_to_symbol(meth)
|
68
|
+
data = args.size == 1 ? args.first : args
|
43
69
|
|
44
|
-
|
45
|
-
|
70
|
+
klass.__send__(:attr_accessor, ivar)
|
71
|
+
klass.__send__(:define_method, "#{ivar}?", proc { !!data })
|
46
72
|
|
47
|
-
|
73
|
+
instance_variable_set("@#{ivar}", data)
|
48
74
|
|
49
|
-
|
50
|
-
else
|
51
|
-
super
|
52
|
-
end
|
75
|
+
self
|
53
76
|
end
|
54
77
|
|
55
|
-
|
56
78
|
private
|
57
79
|
def convert_method_to_symbol(meth)
|
58
80
|
meth.to_s.gsub('=', '').to_sym
|
59
81
|
end
|
60
82
|
|
61
|
-
def self.generate_configurable
|
62
|
-
configuration ||= Configurable.new
|
83
|
+
def self.generate_configurable(path=nil)
|
84
|
+
configuration ||= Higo::Configurable.new(path)
|
63
85
|
end
|
64
86
|
end
|
65
87
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Higo
|
2
|
+
module Transport
|
3
|
+
class File
|
4
|
+
|
5
|
+
attr_reader :path
|
6
|
+
attr_accessor :responses, :_raw_file
|
7
|
+
|
8
|
+
def initialize(path: nil)
|
9
|
+
@path = find_file(path)
|
10
|
+
@_raw_file = {}
|
11
|
+
@responses = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.transport(path)
|
15
|
+
file = new(path)
|
16
|
+
file._raw_file = Fiber.new { ::File.open(file.path, 'r') { |f| f.readlines.map } }
|
17
|
+
file
|
18
|
+
end
|
19
|
+
|
20
|
+
def finish
|
21
|
+
_raw_file.resume.map do |val|
|
22
|
+
responses.merge!(Hash[*process_value(val)])
|
23
|
+
end
|
24
|
+
responses
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def process_value(str, delimeter='=')
|
29
|
+
if path.extname.tr('.','') == 'txt'
|
30
|
+
str.tr('\'','').chomp.split(delimeter)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_file(path)
|
35
|
+
path_to_file = Pathname.new(path)
|
36
|
+
|
37
|
+
if path_to_file.file?
|
38
|
+
return path_to_file
|
39
|
+
elsif path_to_file.relative? && path_to_file.expand_path.file?
|
40
|
+
return path_to_file
|
41
|
+
else
|
42
|
+
raise 'Unable to find file at location'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Higo
|
2
|
+
module Transport
|
3
|
+
class Web
|
4
|
+
|
5
|
+
attr_reader :path
|
6
|
+
attr_accessor :response, :_raw_data
|
7
|
+
|
8
|
+
def initialize(path: nil)
|
9
|
+
@path = URI(path)
|
10
|
+
@_raw_data = nil
|
11
|
+
@response = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.transport(url)
|
15
|
+
request = new(url)
|
16
|
+
request._raw_data = Fiber.new do
|
17
|
+
http = Net::HTTP.new(request.path.host, request.path.port)
|
18
|
+
http.use_ssl = true if request.path.port == 443
|
19
|
+
http.request(Net::HTTP::Get.new(request.path.to_s))
|
20
|
+
end
|
21
|
+
request
|
22
|
+
end
|
23
|
+
|
24
|
+
def finish
|
25
|
+
data = _raw_data.resume
|
26
|
+
temp_file = Tempfile.new([self.path.host.tr('.',''), ::Pathname.new(self.path.to_s).extname])
|
27
|
+
if success?(data.code) && data.body.bytesize > 0
|
28
|
+
::File.open(temp_file.path, 'w') { |f| f.write(data.body) }
|
29
|
+
response = Transport::File.transport(path: temp_file.path).finish
|
30
|
+
temp_file.unlink
|
31
|
+
else
|
32
|
+
raise 'No Data at endpoint'
|
33
|
+
end
|
34
|
+
response
|
35
|
+
rescue => e
|
36
|
+
raise e if e.class == SocketError
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def bad_request?(code)
|
41
|
+
(400..499).member?(status_code(code))
|
42
|
+
end
|
43
|
+
|
44
|
+
def server_unavailable?(code)
|
45
|
+
(500..599).member?(status_code(code))
|
46
|
+
end
|
47
|
+
|
48
|
+
def success?(code)
|
49
|
+
(200..399).member?(status_code(code))
|
50
|
+
end
|
51
|
+
|
52
|
+
def status_code(code)
|
53
|
+
Integer(code) rescue nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def value(code, value)
|
57
|
+
{":#{code}" => value}
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/higo/version.rb
CHANGED
data/lib/higo.rb
CHANGED
@@ -22,7 +22,16 @@
|
|
22
22
|
#
|
23
23
|
# See https://github.com/rhodee/higo for updates.
|
24
24
|
#
|
25
|
+
#
|
26
|
+
# stdlib
|
27
|
+
require 'pathname'
|
28
|
+
require 'fiber'
|
29
|
+
require 'uri'
|
30
|
+
require 'net/https'
|
31
|
+
require 'tempfile'
|
32
|
+
|
25
33
|
module Higo
|
26
|
-
autoload :Version,
|
27
|
-
autoload :Configurable,
|
34
|
+
autoload :Version, 'higo/version'
|
35
|
+
autoload :Configurable, 'higo/configurable'
|
36
|
+
autoload :Transport, 'higo/transport'
|
28
37
|
end
|
data/test/test_configurable.rb
CHANGED
@@ -10,4 +10,20 @@ class TestConfigurable < Higo::TestCase
|
|
10
10
|
assert_includes @configuration.methods, :foo?
|
11
11
|
assert_includes @configuration.methods, :foo=
|
12
12
|
end
|
13
|
+
|
14
|
+
def test_file_read
|
15
|
+
@configuration = Higo::Configurable.configure(path: 'test/support/config_values.txt')
|
16
|
+
|
17
|
+
assert_includes @configuration.methods, :username?
|
18
|
+
assert_includes @configuration.methods, :username=
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_config_merge
|
22
|
+
@configuration = Higo::Configurable.configure(path: 'test/support/config_values.txt') do |conf|
|
23
|
+
conf.foo = 'whooaaaaa'
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_includes @configuration.methods, :foo
|
27
|
+
assert_includes @configuration.methods, :username?
|
28
|
+
end
|
13
29
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative './test_helper'
|
2
|
+
require 'higo/transport/file'
|
3
|
+
|
4
|
+
class TestFileTransport < Higo::TestCase
|
5
|
+
def test_file_path
|
6
|
+
resource = Higo::Transport::File.new(path: 'test/support/config_values.txt')
|
7
|
+
assert_instance_of Pathname, resource.path
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_file_path_error
|
11
|
+
assert_raises(RuntimeError) { Higo::Transport::File.new(path: 'test/support/no_file_here.txt') }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_file_read
|
15
|
+
results = Higo::Transport::File.transport(path: 'test/support/config_values.txt').finish
|
16
|
+
refute_equal true, results.empty?
|
17
|
+
end
|
18
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.expand_path("../lib/higo", File.dirname(__FILE__))
|
2
2
|
|
3
3
|
# Load support files
|
4
|
-
Dir.glob(File.expand_path('./support
|
4
|
+
Dir.glob(File.expand_path('./support/*.rb', File.dirname(__FILE__))) {|file| require file }
|
5
5
|
|
6
6
|
require 'minitest/autorun'
|
7
7
|
require 'minitest/pride'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative './test_helper'
|
2
|
+
require 'higo/transport/web'
|
3
|
+
|
4
|
+
class TestWebTransport < Higo::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@web_request = Higo::Transport::Web.new(path: 'https://gist.githubusercontent.com/rhodee/9732239/raw/356c3877c5055a19b11ed6ed083387c7cd78cdf7/test.txt')
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
@web_request = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_transport_retrieval
|
15
|
+
assert @web_request.path.to_s =~ /http/
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_resource_path_error
|
19
|
+
assert_raises(SocketError) { Higo::Transport::Web.transport(path: 'http://www.notaresource.comz').finish }
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_file_read
|
23
|
+
results = Higo::Transport::Web.transport(path: @web_request.path.to_s).finish
|
24
|
+
refute_equal true, results.empty?
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: higo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rhodee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|
@@ -134,11 +134,16 @@ files:
|
|
134
134
|
- higo.gemspec
|
135
135
|
- lib/higo.rb
|
136
136
|
- lib/higo/configurable.rb
|
137
|
+
- lib/higo/transport.rb
|
138
|
+
- lib/higo/transport/file.rb
|
139
|
+
- lib/higo/transport/web.rb
|
137
140
|
- lib/higo/version.rb
|
138
141
|
- test/test_configurable.rb
|
142
|
+
- test/test_file_transport.rb
|
139
143
|
- test/test_helper.rb
|
140
|
-
- test/
|
144
|
+
- test/test_higo_included.rb
|
141
145
|
- test/test_higo_subclass.rb
|
146
|
+
- test/test_web_transport.rb
|
142
147
|
homepage: https://github.com/rhodee/higo
|
143
148
|
licenses:
|
144
149
|
- MIT
|
@@ -154,12 +159,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
154
159
|
version: '0'
|
155
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
161
|
requirements:
|
157
|
-
- - "
|
162
|
+
- - ">="
|
158
163
|
- !ruby/object:Gem::Version
|
159
|
-
version:
|
164
|
+
version: '0'
|
160
165
|
requirements: []
|
161
166
|
rubyforge_project:
|
162
|
-
rubygems_version: 2.2.
|
167
|
+
rubygems_version: 2.2.2
|
163
168
|
signing_key:
|
164
169
|
specification_version: 4
|
165
170
|
summary: A (soon) to be highly configurable configuration manager.
|
File without changes
|