voomify_utility_drawer 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1 @@
1
+ This is a gem that defines extensions to classes and modules
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'yaml'
4
+ require 'voomify/config'
5
+ require 'extensions/hash'
6
+
7
+
8
+ class ClientBase
9
+ include HTTParty
10
+ include Voomify::Config
11
+ has_site_config "voomify.yml"
12
+
13
+ private
14
+ def self.hashes2struct(object, name="Root#{object.object_id}".gsub('-',''))
15
+ return case object
16
+ when Hash
17
+ object = object.clone
18
+ object.each do |key, value|
19
+ object[key] = self.hashes2struct(value, key.capitalize)
20
+ end
21
+ object.to_struct(name)
22
+ when Array
23
+ object = object.clone
24
+ object.map! { |i| self.hashes2struct(i, "Array#{self.object_id}".gsub('-','')) }
25
+ else
26
+ object
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,10 @@
1
+ class Hash
2
+ def to_struct(struct_name)
3
+ if Struct.const_defined?(struct_name)
4
+ struct = Struct.const_get(struct_name)
5
+ else
6
+ struct = Struct.new(struct_name,*keys)
7
+ end
8
+ struct.new(*values)
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ require 'stringio'
2
+
3
+ module Kernel
4
+
5
+ def capture_stdout
6
+ out = StringIO.new
7
+ $stdout = out
8
+ yield
9
+ return out
10
+ ensure
11
+ $stdout = STDOUT
12
+ end
13
+
14
+ end
@@ -0,0 +1,17 @@
1
+ # OverrideRakeTask
2
+ Rake::TaskManager.class_eval do
3
+ def alias_task(old_name, new_name)
4
+ @tasks[new_name] = @tasks.delete(old_name)
5
+ end
6
+ end
7
+
8
+ def alias_task(old_name, new_name)
9
+ Rake.application.alias_task(old_name, new_name)
10
+ end
11
+
12
+ def override_task(*args, &block)
13
+ name, params, deps = Rake.application.resolve_args(args.dup)
14
+ task = "#{Rake.application.current_scope.join(':')}:#{name.to_s}"
15
+ alias_task task.to_s, "#{task}:original"
16
+ Rake::Task.define_task(*args, &block)
17
+ end
@@ -0,0 +1,2 @@
1
+ require 'extensions/kernel'
2
+ require 'extensions/hash'
@@ -0,0 +1,27 @@
1
+ module Voomify
2
+ module Config
3
+ module ClassMethods
4
+ def has_site_config(config_name)
5
+ class_eval <<-STUFF
6
+ @site_config = nil
7
+
8
+ def self.site_config
9
+ load_site_config unless @site_config
10
+ @site_config
11
+ end
12
+
13
+ def self.load_site_config
14
+ raw_config = File.read("#{RAILS_ROOT}/config/#{config_name}")
15
+ erb_config = ERB.new(raw_config).result
16
+ @site_config = YAML.load(erb_config)[RAILS_ENV]
17
+ end
18
+ STUFF
19
+ end
20
+ end
21
+
22
+ def self.included(base)
23
+ base.extend ClassMethods
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ #require 'extensions/hash'
2
+ require 'utility_drawer'
3
+
4
+ describe "Hash" do
5
+ before(:all) do
6
+ end
7
+
8
+ it "to_struct should return a Struct" do
9
+ {:my_key=>"somevalue"}.to_struct("SomeStruct")
10
+ end
11
+
12
+ it "to_struct should return a without a warning" do
13
+ {:my_key=>"somevalue"}.to_struct("SomeStruct")
14
+ end
15
+
16
+ after(:each) do
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ RAILS_ROOT = File.dirname(__FILE__)
2
+
3
+ require 'utility_drawer'
4
+ require 'base/httparty'
5
+
6
+
7
+ class SomeClass < ClientBase
8
+ base_uri "http://localhost:2001"
9
+ def self.find_instance(instance)
10
+ response = self.get('/application_instances/find_instance.xml', :query => {:instance=> instance})
11
+ return nil if response.code != 200
12
+ self.hashes2struct(response["application_instance"])
13
+ end
14
+
15
+ def self.find_account_by_alias(account_alias)
16
+ response = self.get('/accounts/find_account_by_alias.xml', :query => {:alias=> account_alias})
17
+ self.hashes2struct(response["account"])
18
+ end
19
+ end
20
+
21
+ describe "base/HttParty" do
22
+ before(:all) do
23
+ end
24
+
25
+ # HAL must be running on localhost for this test. Use the live database
26
+ it "hashes2struct should work with two different XML files as input" do
27
+ instance = SomeClass.find_instance("friand-1")
28
+ instance.name.should == "website"
29
+ account = SomeClass.find_account_by_alias("orders@dancindogg.com")
30
+ account.subdomain.should == 'dancindogg'
31
+ end
32
+
33
+ after(:each) do
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: voomify_utility_drawer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 8
10
+ version: 0.1.8
11
+ platform: ruby
12
+ authors:
13
+ - Russell Edens
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-08 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This gem contains a bunch of useful utilities.
23
+ email: russell@voomify.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ files:
31
+ - lib/base/httparty.rb
32
+ - lib/extensions/hash.rb
33
+ - lib/extensions/kernel.rb
34
+ - lib/replace_rake_tasks.rb
35
+ - lib/utility_drawer.rb
36
+ - lib/voomify/config.rb
37
+ - README
38
+ - spec/httparty_base_spec.rb
39
+ - spec/hash_extension_spec.rb
40
+ has_rdoc: true
41
+ homepage: http://www.github.com/voomify/utility_drawer
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Voomify useful classes that most of our projects use
74
+ test_files:
75
+ - spec/httparty_base_spec.rb
76
+ - spec/hash_extension_spec.rb