clientele 0.2.1 → 0.3.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/api.rb +13 -1
- data/lib/clientele/request.rb +26 -4
- data/lib/clientele/request_builder.rb +9 -3
- data/lib/clientele/resource/pagination.rb +10 -7
- data/lib/clientele/resource.rb +29 -5
- data/lib/clientele/response.rb +34 -0
- data/lib/clientele/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45e24ef28f790a89e16cb53f0c44c4fc9176b539
|
4
|
+
data.tar.gz: 5b257efae30faacb896f40d135fa19bd085ff7a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dde13065d7d012d0ab9650baaaba73379bba2572629a97ee3f26efff0f3292f7f99c6fa4dfe8ce66b9f450fed2cff2e5c01fad6e333686ae4a82390c9f1ed88
|
7
|
+
data.tar.gz: b2889a68bc5cc46834faa32fcd40cf2477f9882c1855648c4096f4ce4c3e3763ceb4d84456ac40a0bbc38b303d8e982fdc92c0c50ca111c79236f68500db45b6
|
data/lib/clientele/api.rb
CHANGED
@@ -7,6 +7,7 @@ require 'clientele/configuration'
|
|
7
7
|
require 'clientele/request_builder'
|
8
8
|
require 'clientele/request'
|
9
9
|
require 'clientele/resource'
|
10
|
+
require 'clientele/response'
|
10
11
|
|
11
12
|
module Clientele
|
12
13
|
class API
|
@@ -18,7 +19,7 @@ module Clientele
|
|
18
19
|
def_delegator :configuration, :logger
|
19
20
|
|
20
21
|
class_attribute :resources, instance_predicate: false
|
21
|
-
self.resources =
|
22
|
+
self.resources = Hashie::Rash.new
|
22
23
|
|
23
24
|
class << self
|
24
25
|
|
@@ -39,9 +40,18 @@ module Clientele
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def resource(klass)
|
43
|
+
klass.client = self
|
42
44
|
self.resources = resources.merge(klass.method_name.to_sym => klass)
|
43
45
|
end
|
44
46
|
|
47
|
+
def reset_global_client!
|
48
|
+
@client = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def reconfigure_global_client!(opts={})
|
52
|
+
reset_global_client! and client(opts)
|
53
|
+
end
|
54
|
+
|
45
55
|
private
|
46
56
|
|
47
57
|
def autoconfigure!
|
@@ -68,6 +78,8 @@ module Clientele
|
|
68
78
|
self.configuration.load_hash opts
|
69
79
|
end
|
70
80
|
|
81
|
+
def client; self; end
|
82
|
+
|
71
83
|
private
|
72
84
|
|
73
85
|
def respond_to_missing?(method_name, include_private=false)
|
data/lib/clientele/request.rb
CHANGED
@@ -5,12 +5,25 @@ require 'faraday_middleware'
|
|
5
5
|
require 'rash'
|
6
6
|
|
7
7
|
require 'clientele/utils'
|
8
|
+
require 'clientele/response'
|
8
9
|
|
9
10
|
module Clientele
|
10
|
-
class Request < Struct.new(
|
11
|
+
class Request < Struct.new(*%i[
|
12
|
+
verb
|
13
|
+
path
|
14
|
+
query
|
15
|
+
body
|
16
|
+
headers
|
17
|
+
options
|
18
|
+
callback
|
19
|
+
resource
|
20
|
+
client
|
21
|
+
])
|
22
|
+
|
11
23
|
include Clientele::Utils
|
12
24
|
|
13
|
-
VERBS =
|
25
|
+
VERBS = Faraday::Connection::METHODS
|
26
|
+
|
14
27
|
VERBS.each do |verb|
|
15
28
|
define_singleton_method verb do |path = '', opts = {}, &callback|
|
16
29
|
new(
|
@@ -43,7 +56,8 @@ module Clientele
|
|
43
56
|
end
|
44
57
|
|
45
58
|
def call
|
46
|
-
|
59
|
+
options.deep_merge! client.configuration.to_hash if client
|
60
|
+
callback ? callback.call(result) : result
|
47
61
|
end
|
48
62
|
|
49
63
|
def + other
|
@@ -56,11 +70,18 @@ module Clientele
|
|
56
70
|
options: options.merge(other.options),
|
57
71
|
callback: other.callback || callback,
|
58
72
|
resource: other.resource || resource,
|
73
|
+
client: client || other.client,
|
59
74
|
)
|
60
75
|
end
|
61
76
|
|
77
|
+
def paginateable?; false; end
|
78
|
+
|
62
79
|
private
|
63
80
|
|
81
|
+
def result
|
82
|
+
Response.new response, client: client, resource: resource
|
83
|
+
end
|
84
|
+
|
64
85
|
def response
|
65
86
|
faraday_client.send(verb, ensure_trailing_slash(path)) do |request|
|
66
87
|
request.headers = options.fetch(:headers, {}).merge(headers)
|
@@ -80,7 +101,7 @@ module Clientele
|
|
80
101
|
conn.response :logger
|
81
102
|
conn.response :json, content_type: options[:hashify_content_type], preserve_raw: true
|
82
103
|
|
83
|
-
conn.adapter options[:adapter]
|
104
|
+
conn.adapter options[:adapter] if options[:adapter]
|
84
105
|
end
|
85
106
|
end
|
86
107
|
|
@@ -95,6 +116,7 @@ module Clientele
|
|
95
116
|
options: {},
|
96
117
|
callback: nil,
|
97
118
|
resource: nil,
|
119
|
+
client: nil,
|
98
120
|
}
|
99
121
|
end
|
100
122
|
end
|
@@ -20,7 +20,7 @@ module Clientele
|
|
20
20
|
protected
|
21
21
|
|
22
22
|
def build
|
23
|
-
stack.map(&:to_request).inject(:+).to_request(client
|
23
|
+
stack.map(&:to_request).inject(:+).to_request(client: client)
|
24
24
|
end
|
25
25
|
|
26
26
|
# Compare values only, class doesn't matter
|
@@ -52,6 +52,8 @@ module Clientele
|
|
52
52
|
merge_paths(stack.map(&:path))
|
53
53
|
end
|
54
54
|
|
55
|
+
def paginateable?; false; end
|
56
|
+
|
55
57
|
private
|
56
58
|
|
57
59
|
def method_missing(method_name, *args, &block)
|
@@ -59,8 +61,8 @@ module Clientele
|
|
59
61
|
tap { |builder| builder.stack = builder.stack[0..-2] << builder.stack.last.send(method_name, *args, &block) }
|
60
62
|
elsif client.resources.keys.include? method_name
|
61
63
|
tap { |builder| builder.stack << client.resources[method_name] }
|
62
|
-
elsif stack.last.
|
63
|
-
stack.last.
|
64
|
+
elsif stack.last.paginateable? and enumberable_methods.include? method_name
|
65
|
+
stack.last.send :each, build, &block
|
64
66
|
else; super; end
|
65
67
|
end
|
66
68
|
|
@@ -70,5 +72,9 @@ module Clientele
|
|
70
72
|
or super
|
71
73
|
end
|
72
74
|
|
75
|
+
def enumberable_methods
|
76
|
+
Enumerable.instance_methods - Module.instance_methods
|
77
|
+
end
|
78
|
+
|
73
79
|
end
|
74
80
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module Clientele
|
2
|
-
class Resource
|
2
|
+
class Resource < SimpleDelegator
|
3
3
|
module Pagination
|
4
4
|
|
5
5
|
def paginate(method_name, &implementation)
|
6
6
|
mixin = Module.new do
|
7
7
|
define_method method_name do |*args, &block|
|
8
8
|
super(*args, &block).tap do |request|
|
9
|
+
request.extend(Enumerable) unless request.kind_of? Enumerable
|
9
10
|
request.extend(Iterator)
|
10
11
|
request.class_eval(&default_implementation)
|
11
12
|
request.class_eval(&implementation) if implementation
|
@@ -31,7 +32,7 @@ module Clientele
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def pages(response)
|
34
|
-
response
|
35
|
+
response
|
35
36
|
end
|
36
37
|
|
37
38
|
end
|
@@ -39,16 +40,18 @@ module Clientele
|
|
39
40
|
|
40
41
|
module Iterator
|
41
42
|
|
42
|
-
def
|
43
|
-
|
43
|
+
def paginateable?; true; end
|
44
|
+
|
45
|
+
def each(request = self.to_request)
|
46
|
+
return enum_for(:each, request) unless block_given?
|
44
47
|
|
45
48
|
counter = 0
|
46
|
-
current_response =
|
49
|
+
current_response = request.call
|
47
50
|
|
48
51
|
until counter == total(current_response) do
|
49
52
|
if pages(current_response).empty?
|
50
|
-
current_response =
|
51
|
-
next_page(
|
53
|
+
current_response = request.tap do |builder|
|
54
|
+
next_page(request)
|
52
55
|
end.call
|
53
56
|
else
|
54
57
|
counter +=1
|
data/lib/clientele/resource.rb
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
require 'clientele/request'
|
2
|
+
require 'clientele/response'
|
2
3
|
require 'clientele/utils'
|
3
4
|
require 'clientele/resource/pagination'
|
4
5
|
|
5
6
|
require 'active_support/core_ext/string/inflections'
|
6
7
|
|
7
8
|
module Clientele
|
8
|
-
class Resource
|
9
|
+
class Resource < SimpleDelegator
|
9
10
|
include Clientele::Utils
|
10
11
|
|
11
12
|
@subclasses = []
|
12
13
|
|
14
|
+
class_attribute :client, instance_predicate: false
|
15
|
+
self.client = nil
|
16
|
+
|
13
17
|
class << self
|
14
18
|
include Clientele::Utils
|
15
19
|
|
@@ -17,8 +21,12 @@ module Clientele
|
|
17
21
|
|
18
22
|
Request::VERBS.each do |verb|
|
19
23
|
define_method verb do |path_segment = '', opts = {}, &callback|
|
20
|
-
path_segment, opts = opts[:path].to_s, path_segment if path_segment.is_a? Hash
|
21
|
-
Request.new(opts.merge(
|
24
|
+
path_segment, opts = opts[:path].to_s, path_segment.merge(opts) if path_segment.is_a? Hash
|
25
|
+
Request.new(opts.merge(
|
26
|
+
path: merge_paths(path, path_segment || opts[:path].to_s),
|
27
|
+
resource: self,
|
28
|
+
client: client.client
|
29
|
+
), &callback)
|
22
30
|
end
|
23
31
|
end
|
24
32
|
|
@@ -26,8 +34,8 @@ module Clientele
|
|
26
34
|
send verb, path, opts, &callback
|
27
35
|
end
|
28
36
|
|
29
|
-
def to_request(
|
30
|
-
get
|
37
|
+
def to_request(opts={}, &callback)
|
38
|
+
get opts, &callback
|
31
39
|
end
|
32
40
|
|
33
41
|
def default_path
|
@@ -42,6 +50,22 @@ module Clientele
|
|
42
50
|
@method_name || path
|
43
51
|
end
|
44
52
|
|
53
|
+
def build(data, client: nil)
|
54
|
+
if data.kind_of? Array
|
55
|
+
data.map do |dataset|
|
56
|
+
build dataset, client: client
|
57
|
+
end
|
58
|
+
elsif data.kind_of? Hash and data.keys.map(&:to_s).include? method_name.to_s
|
59
|
+
build data.send method_name, client: client
|
60
|
+
else
|
61
|
+
new(data).tap do |instance|
|
62
|
+
instance.instance_variable_set :@client, client if client
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def paginateable?; false; end
|
68
|
+
|
45
69
|
private
|
46
70
|
|
47
71
|
def inherited(base)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
require 'faraday/response'
|
4
|
+
|
5
|
+
module Clientele
|
6
|
+
|
7
|
+
class Response < SimpleDelegator
|
8
|
+
|
9
|
+
attr_reader :response
|
10
|
+
def initialize(response, client: nil, resource: nil)
|
11
|
+
@response = response
|
12
|
+
super(
|
13
|
+
if resource
|
14
|
+
resource.build response.body, client: client
|
15
|
+
else
|
16
|
+
response.body
|
17
|
+
end
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to_missing?(method_name, include_private=false)
|
22
|
+
@response.respond_to?(method_name, include_private) or super
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(method_name, *args, &block)
|
26
|
+
begin
|
27
|
+
super
|
28
|
+
rescue NoMethodError
|
29
|
+
@response.send method_name, *args, &block
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
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.
|
4
|
+
version: 0.3.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:
|
11
|
+
date: 2015-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -160,6 +160,7 @@ files:
|
|
160
160
|
- lib/clientele/request_builder.rb
|
161
161
|
- lib/clientele/resource.rb
|
162
162
|
- lib/clientele/resource/pagination.rb
|
163
|
+
- lib/clientele/response.rb
|
163
164
|
- lib/clientele/utils.rb
|
164
165
|
- lib/clientele/version.rb
|
165
166
|
homepage: https://github.com/christhekeele/clientele
|