grendel-ruby 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/grendel-ruby.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{grendel-ruby}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brad Greenlee"]
12
- s.date = %q{2010-04-27}
12
+ s.date = %q{2010-04-29}
13
13
  s.description = %q{Grendel is a RESTful web service which allows for the secure storage of users'
14
14
  documents. Grendel-Ruby provides a Ruby API for Grendel.}
15
15
  s.email = %q{brad@wesabe.com}
@@ -28,7 +28,6 @@ Gem::Specification.new do |s|
28
28
  "TODO.md",
29
29
  "VERSION",
30
30
  "grendel-ruby.gemspec",
31
- "lib/core_ext/hash.rb",
32
31
  "lib/grendel.rb",
33
32
  "lib/grendel/client.rb",
34
33
  "lib/grendel/document.rb",
@@ -37,6 +36,7 @@ Gem::Specification.new do |s|
37
36
  "lib/grendel/link_manager.rb",
38
37
  "lib/grendel/linked_document.rb",
39
38
  "lib/grendel/linked_document_manager.rb",
39
+ "lib/grendel/mash.rb",
40
40
  "lib/grendel/user.rb",
41
41
  "lib/grendel/user_manager.rb",
42
42
  "spec/grendel/client_spec.rb",
data/lib/grendel.rb CHANGED
@@ -3,8 +3,8 @@ $:.unshift File.dirname(__FILE__)
3
3
  require 'rubygems'
4
4
  require 'httparty'
5
5
  require 'json'
6
- require 'core_ext/hash'
7
6
  require 'mime/types'
7
+ require 'grendel/mash'
8
8
  require 'grendel/client'
9
9
  require 'grendel/user_manager'
10
10
  require 'grendel/user'
@@ -12,53 +12,50 @@ module Grendel
12
12
  end
13
13
 
14
14
  def get(uri, options = {})
15
- options.merge!(:debug_output => @debug_output) if @debug
16
- response = HTTParty.get(@base_uri + uri, options)
17
- raise HTTPException.new(response) if response.code >= 400
18
- return response
15
+ process_response HTTParty.get(@base_uri + uri, process_options(options))
19
16
  end
20
17
 
21
18
  def head(uri, options = {})
22
- options.merge!(:debug_output => @debug_output) if @debug
23
- response = HTTParty.head(@base_uri + uri, options)
24
- raise HTTPException.new(response) if response.code >= 400
25
- return response
19
+ process_response HTTParty.head(@base_uri + uri, process_options(options))
26
20
  end
27
21
 
28
22
  def post(uri, data = {}, options = {})
29
- data = data.to_json unless options.delete(:raw_data)
30
- options.merge!(
31
- :body => data,
32
- :headers => {'Content-Type' => 'application/json'}
33
- )
34
- options.merge!(:debug_output => @debug_output) if @debug
35
- response = HTTParty.post(@base_uri + uri, options)
36
- raise HTTPException.new(response) if response.code >= 400
37
- return response
23
+ process_response HTTParty.post(@base_uri + uri, process_options(options, data))
38
24
  end
39
25
 
40
26
  def put(uri, data = {}, options = {})
41
- data = data.to_json unless options.delete(:raw_data)
42
- options = {
43
- :body => data,
44
- :headers => {'Content-Type' => 'application/json'}
45
- }.update(options)
46
- options.merge!(:debug_output => @debug_output) if @debug
47
- response = HTTParty.put(@base_uri + uri, options)
48
- raise HTTPException.new(response) if response.code >= 400
49
- return response
27
+ process_response HTTParty.put(@base_uri + uri, process_options(options, data))
50
28
  end
51
29
 
52
30
  def delete(uri, options = {})
53
- options.merge!(:debug_output => @debug_output) if @debug
54
- response = HTTParty.delete(@base_uri + uri, options)
55
- raise HTTPException.new(response) if response.code >= 400
31
+ process_response HTTParty.delete(@base_uri + uri, process_options(options))
56
32
  end
57
33
 
58
34
  def users
59
35
  UserManager.new(self)
60
36
  end
61
37
 
38
+ private
39
+ def process_response(response)
40
+ raise HTTPException.new(response) if response.code >= 400
41
+ return response
42
+ end
43
+
44
+ def process_options(options, data=nil)
45
+ options = options.dup
46
+
47
+ if data
48
+ data = data.to_json unless options.delete(:raw_data)
49
+ options[:body] ||= data
50
+ options[:headers] = (options[:headers] || {}).dup
51
+ options[:headers]['Content-Type'] ||= 'application/json'
52
+ end
53
+
54
+ options[:debug_output] ||= @debug_output if @debug
55
+
56
+ return options
57
+ end
58
+
62
59
  class HTTPException < Exception
63
60
  def initialize(response)
64
61
  msg = "#{response.code} #{response.message}"
@@ -3,7 +3,7 @@ module Grendel
3
3
  attr_accessor :user, :name, :uri, :data, :content_type
4
4
 
5
5
  def initialize(user, params)
6
- params.symbolize_keys!
6
+ params = Mash.new(params)
7
7
  @user = user
8
8
  @client = user.client
9
9
  @name = params[:name]
data/lib/grendel/link.rb CHANGED
@@ -3,7 +3,7 @@ module Grendel
3
3
  attr_accessor :document, :user, :uri
4
4
 
5
5
  def initialize(document, user, params = {})
6
- params.symbolize_keys!
6
+ params = Mash.new(params)
7
7
  @document = document
8
8
  @user = user
9
9
  @uri = params[:uri] ?
@@ -12,7 +12,7 @@ module Grendel
12
12
  # :uri => owner uri
13
13
  # }
14
14
  def initialize(linked_user, params)
15
- params.symbolize_keys!
15
+ params = Mash.new(params)
16
16
  @owner = User.new(linked_user.client, params[:owner])
17
17
  super(@owner, params)
18
18
  @linked_user = linked_user
@@ -0,0 +1,150 @@
1
+ # borrowed from http://github.com/wycats/merb-extlib/raw/master/lib/merb-extlib/mash.rb
2
+ # MIT-Licensed
3
+
4
+ # This class has dubious semantics and we only have it so that people can write
5
+ # params[:key] instead of params['key'].
6
+ module Grendel
7
+ class Mash < Hash
8
+
9
+ # @param constructor<Object>
10
+ # The default value for the mash. Defaults to an empty hash.
11
+ #
12
+ # @details [Alternatives]
13
+ # If constructor is a Hash, a new mash will be created based on the keys of
14
+ # the hash and no default value will be set.
15
+ def initialize(constructor = {})
16
+ if constructor.is_a?(Hash)
17
+ super()
18
+ update(constructor)
19
+ else
20
+ super(constructor)
21
+ end
22
+ end
23
+
24
+ # @param key<Object> The default value for the mash. Defaults to nil.
25
+ #
26
+ # @details [Alternatives]
27
+ # If key is a Symbol and it is a key in the mash, then the default value will
28
+ # be set to the value matching the key.
29
+ def default(key = nil)
30
+ if key.is_a?(Symbol) && include?(key = key.to_s)
31
+ self[key]
32
+ else
33
+ super
34
+ end
35
+ end
36
+
37
+ alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
38
+ alias_method :regular_update, :update unless method_defined?(:regular_update)
39
+
40
+ # @param key<Object> The key to set.
41
+ # @param value<Object>
42
+ # The value to set the key to.
43
+ #
44
+ # @see Mash#convert_key
45
+ # @see Mash#convert_value
46
+ def []=(key, value)
47
+ regular_writer(convert_key(key), convert_value(value))
48
+ end
49
+
50
+ # @param other_hash<Hash>
51
+ # A hash to update values in the mash with. The keys and the values will be
52
+ # converted to Mash format.
53
+ #
54
+ # @return <Mash> The updated mash.
55
+ def update(other_hash)
56
+ other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
57
+ self
58
+ end
59
+
60
+ alias_method :merge!, :update
61
+
62
+ # @param key<Object> The key to check for. This will be run through convert_key.
63
+ #
64
+ # @return <TrueClass, FalseClass> True if the key exists in the mash.
65
+ def key?(key)
66
+ super(convert_key(key))
67
+ end
68
+
69
+ # def include? def has_key? def member?
70
+ alias_method :include?, :key?
71
+ alias_method :has_key?, :key?
72
+ alias_method :member?, :key?
73
+
74
+ # @param key<Object> The key to fetch. This will be run through convert_key.
75
+ # @param *extras<Array> Default value.
76
+ #
77
+ # @return <Object> The value at key or the default value.
78
+ def fetch(key, *extras)
79
+ super(convert_key(key), *extras)
80
+ end
81
+
82
+ # @param *indices<Array>
83
+ # The keys to retrieve values for. These will be run through +convert_key+.
84
+ #
85
+ # @return <Array> The values at each of the provided keys
86
+ def values_at(*indices)
87
+ indices.collect {|key| self[convert_key(key)]}
88
+ end
89
+
90
+ # @return <Mash> A duplicate of this mash.
91
+ def dup
92
+ Mash.new(self)
93
+ end
94
+
95
+ # @param hash<Hash> The hash to merge with the mash.
96
+ #
97
+ # @return <Mash> A new mash with the hash values merged in.
98
+ def merge(hash)
99
+ self.dup.update(hash)
100
+ end
101
+
102
+ # @param key<Object>
103
+ # The key to delete from the mash.\
104
+ def delete(key)
105
+ super(convert_key(key))
106
+ end
107
+
108
+ # Used to provide the same interface as Hash.
109
+ #
110
+ # @return <Mash> This mash unchanged.
111
+ def stringify_keys!; self end
112
+
113
+ # @return <Hash> The mash as a Hash with string keys.
114
+ def to_hash
115
+ Hash.new(default).merge(self)
116
+ end
117
+
118
+ protected
119
+ # @param key<Object> The key to convert.
120
+ #
121
+ # @param <Object>
122
+ # The converted key. If the key was a symbol, it will be converted to a
123
+ # string.
124
+ #
125
+ # @api private
126
+ def convert_key(key)
127
+ key.kind_of?(Symbol) ? key.to_s : key
128
+ end
129
+
130
+ # @param value<Object> The value to convert.
131
+ #
132
+ # @return <Object>
133
+ # The converted value. A Hash or an Array of hashes, will be converted to
134
+ # their Mash equivalents.
135
+ #
136
+ # @api private
137
+ def convert_value(value)
138
+ case value
139
+ when Hash
140
+ converted = Mash.new(value)
141
+ converted.default = value.default
142
+ converted
143
+ when Array
144
+ value.collect { |e| convert_value(e) }
145
+ else
146
+ value
147
+ end
148
+ end
149
+ end
150
+ end
data/lib/grendel/user.rb CHANGED
@@ -9,7 +9,7 @@ module Grendel
9
9
  # uri
10
10
  # password
11
11
  def initialize(client, params)
12
- params.symbolize_keys!
12
+ params = Mash.new(params)
13
13
  @client = client
14
14
  @id = params[:id]
15
15
  @uri = params[:uri] ?
@@ -4,16 +4,16 @@ describe "Grendel::Client" do
4
4
  before do
5
5
  @client = Grendel::Client.new("http://example.com")
6
6
  end
7
-
7
+
8
8
  describe "new method" do
9
9
  it "should set the base_uri" do
10
10
  @client.base_uri.should == "http://example.com"
11
11
  end
12
12
  end
13
-
13
+
14
14
  describe "users method" do
15
15
  it "should return a Grendel::UserManager" do
16
- @client.users.class.should be(Grendel::UserManager)
16
+ @client.users.should be_a(Grendel::UserManager)
17
17
  end
18
18
  end
19
19
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brad Greenlee
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-27 00:00:00 -07:00
17
+ date: 2010-04-29 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -75,7 +75,6 @@ files:
75
75
  - TODO.md
76
76
  - VERSION
77
77
  - grendel-ruby.gemspec
78
- - lib/core_ext/hash.rb
79
78
  - lib/grendel.rb
80
79
  - lib/grendel/client.rb
81
80
  - lib/grendel/document.rb
@@ -84,6 +83,7 @@ files:
84
83
  - lib/grendel/link_manager.rb
85
84
  - lib/grendel/linked_document.rb
86
85
  - lib/grendel/linked_document_manager.rb
86
+ - lib/grendel/mash.rb
87
87
  - lib/grendel/user.rb
88
88
  - lib/grendel/user_manager.rb
89
89
  - spec/grendel/client_spec.rb
data/lib/core_ext/hash.rb DELETED
@@ -1,17 +0,0 @@
1
- # stolen^H^H^H^H^H^Hborrowed from Rails' ActiveSupport
2
- class Hash
3
- # Return a new hash with all keys converted to symbols, as long as
4
- # they respond to +to_sym+.
5
- def symbolize_keys
6
- dup.symbolize_keys!
7
- end
8
-
9
- # Destructively convert all keys to symbols, as long as they respond
10
- # to +to_sym+.
11
- def symbolize_keys!
12
- keys.each do |key|
13
- self[(key.to_sym rescue key) || key] = delete(key)
14
- end
15
- self
16
- end
17
- end