higo 0.0.2 → 0.0.8

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: 97469ec0b1755c63561a9d6ce00050a887074baf
4
- data.tar.gz: 4621abc05548852b449bbd54ca1a2522e6368e0f
3
+ metadata.gz: 95b8dff1c0539a5ed0d976717e5577f571739b41
4
+ data.tar.gz: 8b02d3084d8d4523007eb5c8c167047ffa86f5ac
5
5
  SHA512:
6
- metadata.gz: 1f52d1347c109186ca7a1bea428662ae88272e4e5afbc573eecb5e7f37804743bdebf6633e54b441ed1fde09d10c43b0dfb99a921d85922a2c26b229c0bfc8c1
7
- data.tar.gz: dbd6dcb639cd8cc2661f7780f661274e8bace6c1c8f27d96674f9a0d33a25f2a7f2fba50e26d8d9229483ea145c2409e2cdc7ee1c4f27ba2a172b530dd350e95
6
+ metadata.gz: 4b4d82731b6d25251f5f34b9b8a569dbaa8283bc98277c5b7f51f9e1a5d8fc39472badd103e9a97285c3cf3cd68fb2ce5c68beef18cb745b4e90e73a94002faa
7
+ data.tar.gz: abeb604d37fbe08b6fa7bd1fbf17afd07805f1d72bf00ae596e5805874f71f205ceda53801d47de89151f7701d653249fefa8891fb37d537555dc7adea21dbb6
data/README.md CHANGED
@@ -49,7 +49,7 @@ want. Higo might not be right for you at this stage.
49
49
 
50
50
  * ~~Generate arbitrary configuration values~~
51
51
  * Improve support for subclasses that define `method_missing`
52
- * Add support for JSON files
52
+ * ~~Add support for JSON files~~
53
53
  * ~~Read from an external source~~
54
54
  * Improve documentation
55
55
 
data/lib/higo.rb CHANGED
@@ -23,15 +23,18 @@
23
23
  # See https://github.com/rhodee/higo for updates.
24
24
  #
25
25
  #
26
- # stdlib
27
- require 'pathname'
28
- require 'fiber'
29
- require 'uri'
30
- require 'net/https'
31
- require 'tempfile'
32
-
33
26
  module Higo
27
+ # stdlib
28
+ require 'pathname'
29
+ require 'fiber'
30
+ require 'uri'
31
+ require 'net/https'
32
+ require 'tempfile'
33
+ require 'json'
34
+
35
+ # modules
34
36
  autoload :Version, 'higo/version'
35
37
  autoload :Configurable, 'higo/configurable'
36
38
  autoload :Transport, 'higo/transport'
39
+ autoload :Error, 'higo/error'
37
40
  end
@@ -36,9 +36,11 @@ module Higo
36
36
  values = if file =~ /http/
37
37
  Transport::Web.transport(path: file).finish
38
38
  elsif file =~ /.?(txt|json)/
39
- Transport::File.transport(path: file).finish
39
+ resource = Transport::Generic.new(path: file).file
40
+ resource.call
41
+ resource.finish
40
42
  else
41
- raise 'Unable to read this type of file'
43
+ raise Error::UnreadableFile
42
44
  end
43
45
  values.each do |key,value|
44
46
  self.class.__send__(:attr_accessor, key)
data/lib/higo/error.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Error
2
+ AbstractError = Class.new(StandardError) do
3
+ # define initialize
4
+ # helpful message
5
+ end
6
+
7
+ UnreachableResource = Class.new(AbstractError)
8
+ UnreadableFile = Class.new(AbstractError)
9
+ UndefinedTransportMethod = Class.new(AbstractError)
10
+ NoFileFound = Class.new(AbstractError)
11
+ end
@@ -1,6 +1,10 @@
1
1
  module Higo
2
2
  module Transport
3
- autoload :File, 'higo/transport/file'
4
- autoload :Web, 'higo/transport/web'
3
+ autoload :Web, 'higo/transport/web'
4
+ autoload :Processing, 'higo/transport/processing'
5
+ autoload :Abstract, 'higo/transport/abstract'
6
+ autoload :Generic, 'higo/transport/generic'
7
+ autoload :JSON, 'higo/transport/json'
8
+ autoload :Text, 'higo/transport/text'
5
9
  end
6
10
  end
@@ -0,0 +1,42 @@
1
+ module Higo
2
+ module Transport
3
+ class Abstract
4
+ include Transport::Processing
5
+
6
+ attr_reader :path
7
+ attr_accessor :responses, :_raw_file
8
+
9
+ def initialize(path: nil)
10
+ @path = find_file(path)
11
+ @_raw_file = {}
12
+ @responses = {}
13
+ end
14
+
15
+ # Convenience method for creating Higo::Transport objects and returning a Fiber
16
+ # containing the process for reading JSON or text files from a specified path
17
+ #
18
+ # @param [String]
19
+ # @return [Fiber]
20
+ def self.transport(path)
21
+ file = new(path)
22
+ file._raw_file = file.call
23
+ file
24
+ end
25
+
26
+ # Placeholder method. Must be implemented in subclasses of Higo::Transport::Abstract
27
+ #
28
+ # @return [Fiber]
29
+ def call
30
+ raise Error::UndefinedTransportMethod
31
+ end
32
+
33
+ # Placeholder method. Must be implemented in subclasses of Higo::Transport::Abstract
34
+ #
35
+ # @return [Hash]
36
+ def finish
37
+ raise Error::UndefinedTransportMethod
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,25 @@
1
+ # Provide an interface to read file JSON or text files from a specified path
2
+ #
3
+ # @param [String]
4
+ # @return [Higo::Transport::JSON, Higo::Transport::Text]
5
+ module Higo
6
+ module Transport
7
+ class Generic
8
+ include Processing
9
+
10
+ attr_reader :file
11
+
12
+ def initialize(path: nil)
13
+ proxy = find_file(path)
14
+ case proxy.extname
15
+ when '.js'
16
+ @file = JSON.new(path: proxy.to_s)
17
+ when '.json'
18
+ @file = JSON.new(path: proxy.to_s)
19
+ when '.txt'
20
+ @file = Text.new(path: proxy.to_s)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ # Provide an interface to read JSON files from a specified path and return a Ruby hashes of each key value pair
2
+ module Higo
3
+ module Transport
4
+ class JSON < Abstract
5
+
6
+ def initialize(*)
7
+ super
8
+ @_raw_file = call
9
+ end
10
+
11
+ # Reads file from path provided by Higo::Transport::Abstract
12
+ #
13
+ # @return [Fiber]
14
+ def call
15
+ Fiber.new do
16
+ ::File.open(path, 'r') do |f|
17
+ f.read
18
+ end
19
+ end
20
+ end
21
+
22
+ # Provide an interface to read JSON files from a specified path and thurn them into Ruby hashes
23
+ #
24
+ # @return [Hash]
25
+ def finish
26
+ data = _raw_file.resume
27
+ responses.merge!(process_value(data))
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,39 @@
1
+ # Provide helper methods to return String or Hash objects used in process Higo::Transport objects
2
+ module Higo
3
+ module Transport
4
+ module Processing
5
+
6
+ private
7
+ # Provides a string if text or Hash if JSON format detected
8
+ #
9
+ # @private
10
+ # @param [String]
11
+ # @return [String, Hash]
12
+ def process_value(str, delimeter='=')
13
+
14
+ file_type = path.extname.tr('.','')
15
+ if file_type == 'txt'
16
+ str.tr('\'','').chomp.split(delimeter)
17
+ elsif file_type == 'json' || file_type == 'js'
18
+ ::JSON.parse(str)
19
+ end
20
+ end
21
+
22
+ # Makes attempt to find file in relative and absolute paths
23
+ #
24
+ # @private
25
+ # @return [Hash]
26
+ def find_file(path)
27
+ path_to_file = Pathname.new(path)
28
+
29
+ if path_to_file.file?
30
+ return path_to_file
31
+ elsif path_to_file.relative? && path_to_file.expand_path.file?
32
+ return path_to_file
33
+ else
34
+ raise Error::NoFileFound
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,34 @@
1
+ # Provide an interface to read text files from a specified path and return a Ruby hashes of each key value pair
2
+ module Higo
3
+ module Transport
4
+ class Text < Abstract
5
+
6
+ def initialize(*)
7
+ super
8
+ @_raw_file = call
9
+ end
10
+
11
+ # Reads file from path provided by Higo::Transport::Abstract
12
+ #
13
+ # @return [Fiber]
14
+ def call
15
+ Fiber.new do
16
+ ::File.open(path, 'r') do |f|
17
+ f.readlines.map
18
+ end
19
+ end
20
+ end
21
+
22
+ # Returns a Hash object based on the key value provided per line
23
+ #
24
+ # @return [Hash]
25
+ def finish
26
+ _raw_file.resume.map do |val|
27
+ responses.merge!(Hash[*process_value(val)])
28
+ end
29
+ responses
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -27,20 +27,20 @@ module Higo
27
27
 
28
28
  def finish(data=nil)
29
29
  result = data.respond_to?(:resume) ? data.resume : self._raw_data.resume
30
- raise 'Provide an object that responds to resume' if data.nil?
30
+ raise Error::UnreachableResource if bad_request?(result.code) or server_unavailable?(result.code)
31
31
  temp_file = Tempfile.new([self.path.host.tr('.',''), ::Pathname.new(self.path.to_s).extname])
32
32
 
33
33
  if success?(result.code) && result.body.bytesize > 0
34
34
  ::File.open(temp_file.path, 'w') { |f| f.write(result.body) }
35
- response = Transport::File.transport(path: temp_file.path).finish
35
+ result = Transport::Generic.new(path: temp_file.path).file.finish
36
36
  temp_file.unlink
37
37
  else
38
- raise 'No Data at endpoint'
38
+ raise Error::UnreachableResource
39
39
  end
40
40
 
41
- response
41
+ result
42
42
  rescue => e
43
- raise e if e.class == SocketError
43
+ raise e if e.class == Error::UnreachableResource
44
44
  end
45
45
 
46
46
  private
data/lib/higo/version.rb CHANGED
@@ -2,7 +2,7 @@ module Higo
2
2
  module Version
3
3
  Major = 0
4
4
  Minor = 0
5
- Tiny = 2
5
+ Tiny = 8
6
6
  Pre = nil
7
7
 
8
8
  String = [Major, Minor, Tiny, Pre].compact.join(".")
@@ -0,0 +1,20 @@
1
+ require_relative './test_helper'
2
+ require 'higo/transport/generic'
3
+
4
+ class TestGenericTransport < Higo::TestCase
5
+ def test_text_fetch
6
+ resource = Higo::Transport::Generic.new(path: 'test/support/config_values.txt')
7
+ assert_instance_of Higo::Transport::Text, resource.file
8
+ end
9
+
10
+ def test_json_fetch
11
+ resource = Higo::Transport::Generic.new(path: 'test/support/config_values.json')
12
+ assert_instance_of Higo::Transport::JSON, resource.file
13
+ end
14
+
15
+ def test_js_fetch
16
+ resource = Higo::Transport::Generic.new(path: 'test/support/config_values.js')
17
+ assert_instance_of Higo::Transport::JSON, resource.file
18
+ end
19
+ end
20
+
data/test/test_helper.rb CHANGED
@@ -8,6 +8,8 @@ require 'minitest/pride'
8
8
 
9
9
  module Higo
10
10
  class TestCase < Minitest::Test
11
+ require 'higo/error'
12
+
11
13
  StubResponse = Struct.new(:code, :body)
12
14
  end
13
15
  end
@@ -0,0 +1,18 @@
1
+ require_relative './test_helper'
2
+ require 'higo/transport/json'
3
+
4
+ class TestJSONTransport < Higo::TestCase
5
+ def test_file_path
6
+ resource = Higo::Transport::JSON.new(path: 'test/support/config_values.json')
7
+ assert_instance_of Pathname, resource.path
8
+ end
9
+
10
+ def test_file_path_error
11
+ assert_raises(Error::NoFileFound) { Higo::Transport::JSON.new(path: 'test/support/no_file_here.jsun') }
12
+ end
13
+
14
+ def test_txt_file_read
15
+ results = Higo::Transport::JSON.transport(path: 'test/support/config_values.json').finish
16
+ refute_equal true, results.empty?
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require_relative './test_helper'
2
+ require 'higo/transport/text'
3
+
4
+ class TestFileTransport < Higo::TestCase
5
+ def test_file_path
6
+ resource = Higo::Transport::Text.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(Error::NoFileFound) { Higo::Transport::Text.new(path: 'test/support/no_file_here.txt') }
12
+ end
13
+
14
+ def test_txt_file_read
15
+ results = Higo::Transport::Text.transport(path: 'test/support/config_values.txt').finish
16
+ refute_equal true, results.empty?
17
+ end
18
+ end
@@ -7,13 +7,13 @@ class TestWebTransport < Higo::TestCase
7
7
  end
8
8
 
9
9
  def test_resource_path_error
10
- assert_raises(SocketError) { Higo::Transport::Web.transport(path: 'http://www.notaresource.comz').finish }
10
+ assert_raises(Error::UnreachableResource) { Higo::Transport::Web.transport(path: 'http://www.notaresource.comz').finish }
11
11
  end
12
12
 
13
13
  def test_file_read
14
- url = 'https://secret.configuration.com/plain_config.txt'
15
- request = Higo::Transport::Web.new(path: url)
16
- rsp = StubResponse.new(200, "user=foo\njoy='code'")
14
+ url = 'https://secret.configuration.com/plain_config.txt'
15
+ request = Higo::Transport::Web.transport(path: url)
16
+ rsp = StubResponse.new(200, "user=foo\njoy='code'")
17
17
  payload = Fiber.new { rsp }
18
18
 
19
19
  refute_equal true, request.finish(payload).empty?
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.2
4
+ version: 0.0.8
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-25 00:00:00.000000000 Z
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -134,15 +134,22 @@ files:
134
134
  - higo.gemspec
135
135
  - lib/higo.rb
136
136
  - lib/higo/configurable.rb
137
+ - lib/higo/error.rb
137
138
  - lib/higo/transport.rb
138
- - lib/higo/transport/file.rb
139
+ - lib/higo/transport/abstract.rb
140
+ - lib/higo/transport/generic.rb
141
+ - lib/higo/transport/json.rb
142
+ - lib/higo/transport/processing.rb
143
+ - lib/higo/transport/text.rb
139
144
  - lib/higo/transport/web.rb
140
145
  - lib/higo/version.rb
141
146
  - test/test_configurable.rb
142
- - test/test_file_transport.rb
147
+ - test/test_generic_transport.rb
143
148
  - test/test_helper.rb
144
149
  - test/test_higo_included.rb
145
150
  - test/test_higo_subclass.rb
151
+ - test/test_json_transport.rb
152
+ - test/test_text_transport.rb
146
153
  - test/test_web_transport.rb
147
154
  homepage: https://github.com/rhodee/higo
148
155
  licenses:
@@ -164,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
171
  version: '0'
165
172
  requirements: []
166
173
  rubyforge_project:
167
- rubygems_version: 2.2.0
174
+ rubygems_version: 2.2.2
168
175
  signing_key:
169
176
  specification_version: 4
170
177
  summary: A dyanmic configuration object creator. Reads from local files, remote resources
@@ -1,55 +0,0 @@
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 = file.call
17
- file
18
- end
19
-
20
- def call
21
- Fiber.new do
22
- ::File.open(path, 'r') do |f|
23
- f.readlines.map
24
- end
25
- end
26
- end
27
-
28
- def finish
29
- _raw_file.resume.map do |val|
30
- responses.merge!(Hash[*process_value(val)])
31
- end
32
- responses
33
- end
34
-
35
- private
36
- def process_value(str, delimeter='=')
37
- if path.extname.tr('.','') == 'txt'
38
- str.tr('\'','').chomp.split(delimeter)
39
- end
40
- end
41
-
42
- def find_file(path)
43
- path_to_file = Pathname.new(path)
44
-
45
- if path_to_file.file?
46
- return path_to_file
47
- elsif path_to_file.relative? && path_to_file.expand_path.file?
48
- return path_to_file
49
- else
50
- raise 'Unable to find file at location'
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,18 +0,0 @@
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