clientele 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/clientele/request.rb +2 -2
- data/lib/clientele/request_builder.rb +15 -15
- data/lib/clientele/resource/pagination.rb +64 -0
- data/lib/clientele/resource.rb +1 -0
- data/lib/clientele/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a96006c58600f3a0e961f8c5798038eb69c8286
|
4
|
+
data.tar.gz: ee0b2685b429d42b87ee27cdaf60cc69f762c708
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff8ee67c48d38979c2e12b298adc61ed60f99652468ba93f85d5e75373b948c63f3b2c0e23d5bf4e583877d115d3be9057eee7e9bc28d0ac614c824b1e23159f
|
7
|
+
data.tar.gz: ec40b35950efe963177f21d3d33bfd539c5b2d1cafccc317ba02569c3889693c992efcf5f0877f94320f63eab4dcc29a095283c7a63f6b67bfd573066847eada
|
data/lib/clientele/request.rb
CHANGED
@@ -12,12 +12,12 @@ module Clientele
|
|
12
12
|
|
13
13
|
VERBS = %i[get post put patch delete]
|
14
14
|
VERBS.each do |verb|
|
15
|
-
define_singleton_method verb do |opts={}, &callback|
|
15
|
+
define_singleton_method verb do |opts = {}, &callback|
|
16
16
|
new opts.merge(verb: __method__, callback: callback)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def initialize(props={})
|
20
|
+
def initialize(props = {})
|
21
21
|
apply self.class.defaults
|
22
22
|
apply props
|
23
23
|
end
|
@@ -4,9 +4,12 @@ require 'clientele/utils'
|
|
4
4
|
module Clientele
|
5
5
|
class RequestBuilder
|
6
6
|
include Clientele::Utils
|
7
|
+
include Enumerable
|
8
|
+
attr_accessor :stack, :client
|
9
|
+
alias_method :to_a, :stack
|
7
10
|
|
8
|
-
def initialize(*
|
9
|
-
@stack = Array(
|
11
|
+
def initialize(*request_components, client: API.client)
|
12
|
+
@stack = Array(request_components).flatten
|
10
13
|
@client = client
|
11
14
|
end
|
12
15
|
|
@@ -18,11 +21,7 @@ module Clientele
|
|
18
21
|
protected
|
19
22
|
|
20
23
|
def build
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_a
|
25
|
-
@stack
|
24
|
+
stack.map(&:to_request).inject(:+).to_request(client.configuration.to_hash)
|
26
25
|
end
|
27
26
|
|
28
27
|
# Compare values only, class doesn't matter
|
@@ -51,24 +50,25 @@ module Clientele
|
|
51
50
|
end
|
52
51
|
|
53
52
|
def to_s
|
54
|
-
merge_paths(
|
53
|
+
merge_paths(stack.map(&:to_s))
|
55
54
|
end
|
56
55
|
|
57
56
|
private
|
58
57
|
|
59
58
|
def method_missing(method_name, *args, &block)
|
60
59
|
if API::resources.keys.include? method_name
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
60
|
+
tap { |builder| builder.stack << API::resources[method_name] }
|
61
|
+
elsif stack.last.respond_to? :each_with_builder and method_name == :each
|
62
|
+
stack.last.each_with_builder(self, &block)
|
63
|
+
elsif stack.last.respond_to? method_name, false
|
64
|
+
tap { |builder| builder.stack = builder.stack[0..-2] << builder.stack.last.send(method_name, *args, &block) }
|
66
65
|
else; super; end
|
67
66
|
end
|
67
|
+
|
68
68
|
def respond_to_missing?(method_name, include_private=false)
|
69
69
|
API::resources.keys.include?(method_name) \
|
70
|
-
|
71
|
-
|
70
|
+
or stack.last.respond_to?(method_name, include_private) \
|
71
|
+
or super
|
72
72
|
end
|
73
73
|
|
74
74
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Clientele
|
2
|
+
class Resource
|
3
|
+
module Pagination
|
4
|
+
|
5
|
+
def paginate(method_name, &implementation)
|
6
|
+
mixin = Module.new do
|
7
|
+
define_method method_name do |*args, &block|
|
8
|
+
super(*args, &block).tap do |request|
|
9
|
+
request.extend(Iterator)
|
10
|
+
request.class_eval(&default_implementation)
|
11
|
+
request.class_eval(&implementation) if implementation
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
const_set :"#{method_name.to_s.capitalize}Pagination", mixin
|
16
|
+
singleton_class.prepend mixin
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def default_implementation
|
22
|
+
Proc.new do
|
23
|
+
|
24
|
+
def next_page(request)
|
25
|
+
request.query[:page] += 1
|
26
|
+
end
|
27
|
+
|
28
|
+
def total(response)
|
29
|
+
response.headers.fetch('x-total-count', Float::INFINITY)
|
30
|
+
end
|
31
|
+
|
32
|
+
def pages(response)
|
33
|
+
response.body.values.first
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module Iterator
|
40
|
+
|
41
|
+
def each_with_builder(builder)
|
42
|
+
return enum_for(:each_with_builder) unless block_given?
|
43
|
+
|
44
|
+
counter = 0
|
45
|
+
current_response = builder.call
|
46
|
+
|
47
|
+
until counter == total(current_response) do
|
48
|
+
if pages(current_response).empty?
|
49
|
+
current_response = builder.tap do |builder|
|
50
|
+
next_page(builder.stack.last)
|
51
|
+
end.call
|
52
|
+
else
|
53
|
+
counter +=1
|
54
|
+
yield pages(current_response).shift
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/clientele/resource.rb
CHANGED
data/lib/clientele/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clientele
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Keele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- lib/clientele/request.rb
|
160
160
|
- lib/clientele/request_builder.rb
|
161
161
|
- lib/clientele/resource.rb
|
162
|
+
- lib/clientele/resource/pagination.rb
|
162
163
|
- lib/clientele/utils.rb
|
163
164
|
- lib/clientele/version.rb
|
164
165
|
homepage: https://github.com/christhekeele/clientele
|
@@ -186,3 +187,4 @@ signing_key:
|
|
186
187
|
specification_version: 4
|
187
188
|
summary: DSL for creating RESTful API clients for external services.
|
188
189
|
test_files: []
|
190
|
+
has_rdoc:
|