carnivore 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'celluloid'
3
3
  module Carnivore
4
4
  module Utils
5
5
 
6
+ # Logging utilities
6
7
  module Logging
7
8
 
8
9
  # Define base logging types
@@ -13,6 +14,9 @@ module Carnivore
13
14
  end
14
15
 
15
16
  # Log message
17
+ #
18
+ # @param args [Object] argument list
19
+ # @return [NilClass]
16
20
  def log(*args)
17
21
  if(args.empty?)
18
22
  Celluloid::Logger
@@ -1,14 +1,19 @@
1
1
  module Carnivore
2
2
  module Utils
3
- # Registry used for preventing duplicate message processing
3
+
4
+ # Registry used for preventing duplicate message processing
4
5
  class MessageRegistry
6
+
7
+ # Create new instance
5
8
  def initialize
6
9
  @store = []
7
10
  @size = 100
8
11
  end
9
12
 
10
- # message:: Carnivore::Message
11
- # Returns true if message has not been processed
13
+ # Validity of message (not found within registry)
14
+ #
15
+ # @param message [Carnivore::Message]
16
+ # @return [TrueClass, FalseClass]
12
17
  def valid?(message)
13
18
  checksum = sha(message)
14
19
  found = @store.include?(checksum)
@@ -18,8 +23,10 @@ module Carnivore
18
23
  !found
19
24
  end
20
25
 
21
- # item:: checksum
22
- # Pushes checksum into store
26
+ # Register checksum into registry
27
+ #
28
+ # @param item [String] checksum
29
+ # @return [self]
23
30
  def push(item)
24
31
  @store.push(item)
25
32
  if(@store.size > @size)
@@ -28,8 +35,10 @@ module Carnivore
28
35
  self
29
36
  end
30
37
 
31
- # thing:: Instance
32
- # Return checksum for give instance
38
+ # Generate checksum for given item
39
+ #
40
+ # @param thing [Object]
41
+ # @return [String] checksum
33
42
  def sha(thing)
34
43
  unless(thing.is_a?(String))
35
44
  thing = MultiJson.dump(thing)
@@ -1,10 +1,13 @@
1
1
  module Carnivore
2
2
  module Utils
3
3
 
4
+ # Parameter helper methods generally aimed at Hash instances
4
5
  module Params
5
6
 
6
- # hash:: Hash
7
7
  # Symbolize keys in hash
8
+ #
9
+ # @param hash [Hash]
10
+ # @return [Hash] new hash instance with symbolized keys
8
11
  def symbolize_hash(hash)
9
12
  Hash[*(
10
13
  hash.map do |k,v|
@@ -21,10 +24,11 @@ module Carnivore
21
24
  )]
22
25
  end
23
26
 
24
- # hash:: Hash
25
- # args:: Symbols or strings
26
- # Follow path in hash provided by args and return value or nil
27
- # if path is not valid
27
+ # Retrieve value in hash at given path
28
+ #
29
+ # @param hash [Hash] hash to walk into
30
+ # @param args [String, Symbol] argument list to walk in hash
31
+ # @return [Object, NilClass]
28
32
  def retrieve(hash, *args)
29
33
  valids = [::Hash, hash.is_a?(Class) ? hash : hash.class]
30
34
  args.flatten.inject(hash) do |memo, key|
@@ -0,0 +1,90 @@
1
+ require 'hashie'
2
+
3
+ module Carnivore
4
+ module Utils
5
+
6
+ # Customized Hash
7
+ class Smash < Hash
8
+ include Hashie::Extensions::IndifferentAccess
9
+ include Hashie::Extensions::DeepMerge
10
+ include Hashie::Extensions::Coercion
11
+
12
+ coerce_value Hash, Smash
13
+
14
+ # Create new instance
15
+ #
16
+ # @param args [Object] argument list
17
+ def initialize(*args)
18
+ base = nil
19
+ if(args.first.is_a?(::Hash))
20
+ base = args.shift
21
+ end
22
+ super *args
23
+ if(base)
24
+ self.replace(base)
25
+ end
26
+ end
27
+
28
+ # Get value at given path
29
+ #
30
+ # @param args [String, Symbol] key path to walk
31
+ # @return [Object, NilClass]
32
+ def retrieve(*args)
33
+ args.inject(self) do |memo, key|
34
+ if(memo.is_a?(Hash))
35
+ memo.to_smash[key]
36
+ else
37
+ nil
38
+ end
39
+ end
40
+ end
41
+ alias_method :get, :retrieve
42
+
43
+ # Fetch value at given path or return a default value
44
+ #
45
+ # @param args [String, Symbol, Object] key path to walk. last value default to return
46
+ # @return [Object] value at key or default value
47
+ def fetch(*args)
48
+ default_value = args.pop
49
+ retrieve(*args) || default_value
50
+ end
51
+
52
+ # Set value at given path
53
+ #
54
+ # @param args [String, Symbol, Object] key path to walk. set last value to given path
55
+ # @return [Object] value set
56
+ def set(*args)
57
+ unless(args.size > 1)
58
+ raise ArgumentError.new 'Set requires at least one key and a value'
59
+ end
60
+ value = args.pop
61
+ set_key = args.pop
62
+ leaf = args.inject(self) do |memo, key|
63
+ unless(memo[key].is_a?(Hash))
64
+ memo[key] = Smash.new
65
+ end
66
+ memo[key]
67
+ end
68
+ leaf[set_key] = value
69
+ value
70
+ end
71
+
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ # Hook helper into toplevel `Hash`
78
+ class Hash
79
+
80
+ # Convert to Smash
81
+ #
82
+ # @return [Smash]
83
+ def to_smash
84
+ ::Smash.new.replace(self)
85
+ end
86
+ alias_method :hulk_smash, :to_smash
87
+
88
+ end
89
+
90
+ Smash = Carnivore::Utils::Smash
@@ -2,10 +2,12 @@ require 'carnivore'
2
2
 
3
3
  module Carnivore
4
4
 
5
+ # Helper utilities
5
6
  module Utils
6
7
  autoload :Params, 'carnivore/utils/params'
7
8
  autoload :Logging, 'carnivore/utils/logging'
8
9
  autoload :MessageRegistry, 'carnivore/utils/message_registry'
10
+ autoload :Smash, 'carnivore/utils/smash'
9
11
 
10
12
  extend Params
11
13
  extend Logging
@@ -1,7 +1,7 @@
1
1
  module Carnivore
2
+ # Versioning class for library
2
3
  class Version < Gem::Version
3
4
  end
4
- VERSION = Version.new('0.2.0')
5
+ # Current version of library
6
+ VERSION = Version.new('0.2.2')
5
7
  end
6
-
7
- require 'carnivore'
data/lib/carnivore.rb CHANGED
@@ -1,5 +1,19 @@
1
1
  require 'carnivore/runner'
2
2
  require 'carnivore/version'
3
-
4
3
  # Load in celluloid as required
5
- autoload :Celluloid, 'celluloid'
4
+ require 'celluloid/autostart'
5
+
6
+ # Message consumer and processor
7
+ module Carnivore
8
+ autoload :Config, 'carnivore/config'
9
+ autoload :Callback, 'carnivore/callback'
10
+ autoload :Container, 'carnivore/container'
11
+ autoload :Error, 'carnivore/errors'
12
+ autoload :Message, 'carnivore/message'
13
+ autoload :Source, 'carnivore/source'
14
+ autoload :Supervisor, 'carnivore/supervisor'
15
+ autoload :Utils, 'carnivore/utils'
16
+ autoload :Version, 'carnivore/version'
17
+ end
18
+
19
+ autoload :Smash, 'carnivore/utils/smash'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carnivore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-10 00:00:00.000000000 Z
12
+ date: 2014-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: celluloid
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: hashie
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description: Message processing helper
63
79
  email: chrisroberts.code@gmail.com
64
80
  executables: []
@@ -74,9 +90,9 @@ files:
74
90
  - lib/carnivore/runner.rb
75
91
  - lib/carnivore/spec_helper.rb
76
92
  - lib/carnivore/utils/message_registry.rb
93
+ - lib/carnivore/utils/smash.rb
77
94
  - lib/carnivore/utils/logging.rb
78
95
  - lib/carnivore/utils/params.rb
79
- - lib/carnivore/autoloader.rb
80
96
  - lib/carnivore/config.rb
81
97
  - lib/carnivore/message.rb
82
98
  - lib/carnivore/source_container.rb
@@ -87,7 +103,8 @@ files:
87
103
  - README.md
88
104
  - CHANGELOG.md
89
105
  homepage: https://github.com/carnivore-rb/carnivore
90
- licenses: []
106
+ licenses:
107
+ - Apache 2.0
91
108
  post_install_message:
92
109
  rdoc_options: []
93
110
  require_paths:
@@ -1,13 +0,0 @@
1
- # Register all base autoloading requirements
2
-
3
- module Carnivore
4
- autoload :Config, 'carnivore/config'
5
- autoload :Callback, 'carnivore/callback'
6
- autoload :Container, 'carnivore/container'
7
- autoload :Error, 'carnivore/errors'
8
- autoload :Message, 'carnivore/message'
9
- autoload :Source, 'carnivore/source'
10
- autoload :Supervisor, 'carnivore/supervisor'
11
- autoload :Utils, 'carnivore/utils'
12
- autoload :Version, 'carnivore/version'
13
- end