edh 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.
@@ -0,0 +1,201 @@
1
+ module EDHTest
2
+ # directly taken from Rails 3.1's OrderedHash
3
+ # see https://github.com/rails/rails/blob/master/activesupport/lib/active_support/ordered_hash.rb
4
+
5
+ # The order of iteration over hashes in Ruby 1.8 is undefined. For example, you do not know the
6
+ # order in which +keys+ will return keys, or +each+ yield pairs. <tt>ActiveSupport::OrderedHash</tt>
7
+ # implements a hash that preserves insertion order, as in Ruby 1.9:
8
+ #
9
+ # oh = ActiveSupport::OrderedHash.new
10
+ # oh[:a] = 1
11
+ # oh[:b] = 2
12
+ # oh.keys # => [:a, :b], this order is guaranteed
13
+ #
14
+ # <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts with other implementations.
15
+ class OrderedHash < ::Hash #:nodoc:
16
+ def to_yaml_type
17
+ "!tag:yaml.org,2002:omap"
18
+ end
19
+
20
+ def encode_with(coder)
21
+ coder.represent_seq '!omap', map { |k,v| { k => v } }
22
+ end
23
+
24
+ def to_yaml(opts = {})
25
+ if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
26
+ return super
27
+ end
28
+
29
+ YAML.quick_emit(self, opts) do |out|
30
+ out.seq(taguri) do |seq|
31
+ each do |k, v|
32
+ seq.add(k => v)
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def nested_under_indifferent_access
39
+ self
40
+ end
41
+
42
+ # Hash is ordered in Ruby 1.9!
43
+ if RUBY_VERSION < '1.9'
44
+
45
+ # In MRI the Hash class is core and written in C. In particular, methods are
46
+ # programmed with explicit C function calls and polymorphism is not honored.
47
+ #
48
+ # For example, []= is crucial in this implementation to maintain the @keys
49
+ # array but hash.c invokes rb_hash_aset() originally. This prevents method
50
+ # reuse through inheritance and forces us to reimplement stuff.
51
+ #
52
+ # For instance, we cannot use the inherited #merge! because albeit the algorithm
53
+ # itself would work, our []= is not being called at all by the C code.
54
+
55
+ def initialize(*args, &block)
56
+ super
57
+ @keys = []
58
+ end
59
+
60
+ def self.[](*args)
61
+ ordered_hash = new
62
+
63
+ if (args.length == 1 && args.first.is_a?(Array))
64
+ args.first.each do |key_value_pair|
65
+ next unless (key_value_pair.is_a?(Array))
66
+ ordered_hash[key_value_pair[0]] = key_value_pair[1]
67
+ end
68
+
69
+ return ordered_hash
70
+ end
71
+
72
+ unless (args.size % 2 == 0)
73
+ raise ArgumentError.new("odd number of arguments for Hash")
74
+ end
75
+
76
+ args.each_with_index do |val, ind|
77
+ next if (ind % 2 != 0)
78
+ ordered_hash[val] = args[ind + 1]
79
+ end
80
+
81
+ ordered_hash
82
+ end
83
+
84
+ def initialize_copy(other)
85
+ super
86
+ # make a deep copy of keys
87
+ @keys = other.keys
88
+ end
89
+
90
+ def []=(key, value)
91
+ @keys << key unless has_key?(key)
92
+ super
93
+ end
94
+
95
+ def delete(key)
96
+ if has_key? key
97
+ index = @keys.index(key)
98
+ @keys.delete_at index
99
+ end
100
+ super
101
+ end
102
+
103
+ def delete_if
104
+ super
105
+ sync_keys!
106
+ self
107
+ end
108
+
109
+ def reject!
110
+ super
111
+ sync_keys!
112
+ self
113
+ end
114
+
115
+ def reject(&block)
116
+ dup.reject!(&block)
117
+ end
118
+
119
+ def keys
120
+ @keys.dup
121
+ end
122
+
123
+ def values
124
+ @keys.collect { |key| self[key] }
125
+ end
126
+
127
+ def to_hash
128
+ self
129
+ end
130
+
131
+ def to_a
132
+ @keys.map { |key| [ key, self[key] ] }
133
+ end
134
+
135
+ def each_key
136
+ return to_enum(:each_key) unless block_given?
137
+ @keys.each { |key| yield key }
138
+ self
139
+ end
140
+
141
+ def each_value
142
+ return to_enum(:each_value) unless block_given?
143
+ @keys.each { |key| yield self[key]}
144
+ self
145
+ end
146
+
147
+ def each
148
+ return to_enum(:each) unless block_given?
149
+ @keys.each {|key| yield [key, self[key]]}
150
+ self
151
+ end
152
+
153
+ alias_method :each_pair, :each
154
+
155
+ alias_method :select, :find_all
156
+
157
+ def clear
158
+ super
159
+ @keys.clear
160
+ self
161
+ end
162
+
163
+ def shift
164
+ k = @keys.first
165
+ v = delete(k)
166
+ [k, v]
167
+ end
168
+
169
+ def merge!(other_hash)
170
+ if block_given?
171
+ other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
172
+ else
173
+ other_hash.each { |k, v| self[k] = v }
174
+ end
175
+ self
176
+ end
177
+
178
+ alias_method :update, :merge!
179
+
180
+ def merge(other_hash, &block)
181
+ dup.merge!(other_hash, &block)
182
+ end
183
+
184
+ # When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
185
+ def replace(other)
186
+ super
187
+ @keys = other.keys
188
+ self
189
+ end
190
+
191
+ def invert
192
+ OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
193
+ end
194
+
195
+ private
196
+ def sync_keys!
197
+ @keys.delete_if {|k| !has_key?(k)}
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,114 @@
1
+ shared_examples_for "EDH RestAPI" do
2
+ # REST_CALL
3
+ describe "when making a rest request" do
4
+ it "uses the proper path" do
5
+ method = stub('methodName')
6
+ @api.should_receive(:api).with(
7
+ "#{method}",
8
+ anything,
9
+ anything,
10
+ anything
11
+ )
12
+
13
+ @api.rest_call(method)
14
+ end
15
+
16
+ it "uses the proper path for delete" do
17
+ @api.should_receive(:api).with(
18
+ "actions/1234",
19
+ anything,
20
+ :delete,
21
+ anything
22
+ )
23
+
24
+ @api.delete(1234)
25
+ end
26
+
27
+ it "uses the proper path for update" do
28
+ @api.should_receive(:api).with(
29
+ "actions/1234",
30
+ anything,
31
+ :put,
32
+ anything
33
+ )
34
+
35
+ @api.update(1234)
36
+ end
37
+
38
+ it "uses the proper path for create" do
39
+ @api.should_receive(:api).with(
40
+ "me/pages.fundraise",
41
+ anything,
42
+ :post,
43
+ anything
44
+ )
45
+
46
+ @api.create("pages.fundraise", {:abc => "def"})
47
+ end
48
+
49
+ it "takes an optional hash of arguments" do
50
+ args = {:arg1 => 'arg1'}
51
+
52
+ @api.should_receive(:api).with(
53
+ anything,
54
+ hash_including(args),
55
+ anything,
56
+ anything
57
+ )
58
+
59
+ @api.rest_call('anything', args)
60
+ end
61
+
62
+ it "always asks for JSON" do
63
+ @api.should_receive(:api).with(
64
+ anything,
65
+ hash_including('format' => 'json'),
66
+ anything,
67
+ anything
68
+ )
69
+
70
+ @api.rest_call('anything')
71
+ end
72
+
73
+ it "passes any options provided to the API" do
74
+ options = {:a => 2}
75
+
76
+ @api.should_receive(:api).with(
77
+ anything,
78
+ hash_including('format' => 'json'),
79
+ anything,
80
+ hash_including(options)
81
+ )
82
+
83
+ @api.rest_call('anything', {}, options)
84
+ end
85
+
86
+ it "uses get by default" do
87
+ @api.should_receive(:api).with(
88
+ anything,
89
+ anything,
90
+ "get",
91
+ anything
92
+ )
93
+
94
+ @api.rest_call('anything')
95
+ end
96
+
97
+ it "allows you to specify other http methods as the last argument" do
98
+ method = 'bar'
99
+ @api.should_receive(:api).with(
100
+ anything,
101
+ anything,
102
+ method,
103
+ anything
104
+ )
105
+
106
+ @api.rest_call('anything', {}, {}, method)
107
+ end
108
+
109
+ it "throws an APIError if the status code >= 400" do
110
+ EDH.stub(:make_request).and_return(EDH::HTTPService::Response.new(500, '{"error_code": "An error occurred!"}', {}))
111
+ lambda { @api.rest_call(EDHTest.user1, {}) }.should raise_exception(EDH::Passport::APIError)
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: edh
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Koppel
9
+ - Joel Richards
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-05-07 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: multi_json
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: faraday
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: addressable
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: rake
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: A lightweight, flexible library for EDH Passport
96
+ email: joelr@everydayhero.com.au
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - readme.md
101
+ - changelog.md
102
+ files:
103
+ - .autotest
104
+ - .gitignore
105
+ - .rspec
106
+ - .travis.yml
107
+ - .yardopts
108
+ - Gemfile
109
+ - Guardfile
110
+ - LICENSE
111
+ - Manifest
112
+ - Rakefile
113
+ - autotest/discover.rb
114
+ - changelog.md
115
+ - edh.gemspec
116
+ - lib/edh.rb
117
+ - lib/edh/api.rb
118
+ - lib/edh/api/rest_api.rb
119
+ - lib/edh/errors.rb
120
+ - lib/edh/http_service.rb
121
+ - lib/edh/http_service/multipart_request.rb
122
+ - lib/edh/http_service/response.rb
123
+ - lib/edh/test_users.rb
124
+ - lib/edh/utils.rb
125
+ - lib/edh/version.rb
126
+ - readme.md
127
+ - spec/cases/api_spec.rb
128
+ - spec/cases/edh_spec.rb
129
+ - spec/cases/edh_test_spec.rb
130
+ - spec/cases/error_spec.rb
131
+ - spec/cases/http_service_spec.rb
132
+ - spec/cases/multipart_request_spec.rb
133
+ - spec/cases/utils_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/custom_matchers.rb
136
+ - spec/support/edh_test.rb
137
+ - spec/support/mock_http_service.rb
138
+ - spec/support/ordered_hash.rb
139
+ - spec/support/rest_api_shared_examples.rb
140
+ homepage: http://github.com/everydayhero/edh
141
+ licenses: []
142
+ post_install_message:
143
+ rdoc_options:
144
+ - --line-numbers
145
+ - --inline-source
146
+ - --title
147
+ - EDH
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 1.8.23
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: A lightweight, flexible library for EDH Passport
168
+ test_files:
169
+ - spec/cases/api_spec.rb
170
+ - spec/cases/edh_spec.rb
171
+ - spec/cases/edh_test_spec.rb
172
+ - spec/cases/error_spec.rb
173
+ - spec/cases/http_service_spec.rb
174
+ - spec/cases/multipart_request_spec.rb
175
+ - spec/cases/utils_spec.rb
176
+ - spec/spec_helper.rb
177
+ - spec/support/custom_matchers.rb
178
+ - spec/support/edh_test.rb
179
+ - spec/support/mock_http_service.rb
180
+ - spec/support/ordered_hash.rb
181
+ - spec/support/rest_api_shared_examples.rb
182
+ has_rdoc: