clientele 0.3.9 → 0.3.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f931acbaba3e45fdd2a8d439bd83b3b634ad4ce5
4
- data.tar.gz: 88740b5de27c38468065f86137e3f13324d244ba
3
+ metadata.gz: 6582c99553295f93676db28a306f47c12a618c24
4
+ data.tar.gz: 981a484ad7dda1988ec60df5a2cd1c6a1f82d992
5
5
  SHA512:
6
- metadata.gz: ea94dc080c1fb7e8008dd4d6a53b50d411a3a6f2aa7661f1d93eda728a0f2cf75feb6396ec7b30083b16d02cc7809659070fb8b10b3994aff41b4997791c4c39
7
- data.tar.gz: 9778ad711a7e4ea3058bfea8560941ff08c050224760cca1ec27b33305dc74a5a667fec8e6e45691b0ee80f89141d00304025c428eac8f2e3f0d46f87f6c5f3b
6
+ metadata.gz: f63be792b186af675a00945ff1e999813dfabfd4e8c6bbcf09a98f901b2ae8081971061a4da5adb29f74fd6764c0d58c445048ae062c7c9db78b534d4eba1f94
7
+ data.tar.gz: 7524c40b6373d57b3745e423f5fd9afead437495fab9d5356b580b33c54a80a6a445358caca7df0b2dafac32303f683b80c12a676847a6125db131f9c0983767
@@ -44,6 +44,10 @@ module Clientele
44
44
  self.resources = resources.merge(klass.method_name.to_sym => klass)
45
45
  end
46
46
 
47
+ def has_resource?(resource)
48
+ resources.keys.include? resource.to_s
49
+ end
50
+
47
51
  def reset_global_client!
48
52
  @client = nil
49
53
  end
@@ -74,8 +74,6 @@ module Clientele
74
74
  )
75
75
  end
76
76
 
77
- def paginateable?; false; end
78
-
79
77
  private
80
78
 
81
79
  def result
@@ -98,7 +96,7 @@ module Clientele
98
96
  conn.request :url_encoded
99
97
 
100
98
  conn.response :rashify
101
- conn.response :logger
99
+ conn.response :logger, options[:logger], bodies: true
102
100
  conn.response :json, content_type: options[:hashify_content_type], preserve_raw: true
103
101
 
104
102
  conn.adapter options[:adapter] if options[:adapter]
@@ -6,7 +6,6 @@ module Clientele
6
6
  include Clientele::Utils
7
7
  include Enumerable
8
8
  attr_accessor :stack, :client
9
- alias_method :to_a, :stack
10
9
 
11
10
  def initialize(*request_components, client: API.client)
12
11
  @stack = request_components.flatten
@@ -52,28 +51,44 @@ module Clientele
52
51
  merge_paths(stack.map(&:path))
53
52
  end
54
53
 
55
- def paginateable?; false; end
56
-
57
54
  private
58
55
 
59
56
  def method_missing(method_name, *args, &block)
60
- if stack.last.respond_to? method_name, false
61
- tap { |builder| builder.stack = builder.stack[0..-2] << builder.stack.last.send(method_name, *args, &block) }
62
- elsif client.resources.keys.include? method_name.to_s
63
- tap { |builder| builder.stack << client.resources[method_name.to_s] }
64
- elsif stack.last.paginateable? and enumberable_methods.include? method_name
65
- stack.last.send :each, build, &block
57
+ if chain.respond_to? method_name, :public
58
+ chain_method method_name, *args, &block
59
+ elsif client.has_resource? method_name
60
+ chain_resource method_name
61
+ elsif should_paginate? method_name
62
+ chain.send :each, build, &block
66
63
  else; super; end
67
64
  end
68
65
 
69
66
  def respond_to_missing?(method_name, include_private=false)
70
- stack.last.respond_to?(method_name, include_private) \
71
- or API::resources.keys.include?(method_name) \
67
+ chain.respond_to? method_name, :public \
68
+ or client.has_resource? method_name \
69
+ or should_paginate? method_name \
72
70
  or super
73
71
  end
74
72
 
73
+ def chain
74
+ stack.last
75
+ end
76
+
77
+ def chain_method(method_name, *args, &block)
78
+ stack << stack.pop.send(method_name, *args, &block) and self
79
+ end
80
+
81
+ def chain_resource(resource_name)
82
+ stack << client.resources[method_name.to_s] and self
83
+ end
84
+
85
+ def should_paginate?(method_name)
86
+ chain.instance_variable_get :@paginateable \
87
+ and enumberable_methods.include? method_name
88
+ end
89
+
75
90
  def enumberable_methods
76
- Enumerable.instance_methods - Module.instance_methods
91
+ Enumerable.instance_methods - Module.instance_methods << :each
77
92
  end
78
93
 
79
94
  end
@@ -66,6 +66,10 @@ module Clientele
66
66
  @nested_plural_key || plural_key
67
67
  end
68
68
 
69
+ def nested_plural_key
70
+ @nested_plural_key || plural_key
71
+ end
72
+
69
73
  def build(data, client: nil, response: nil)
70
74
  new(
71
75
  catch(:build) do
@@ -89,8 +93,6 @@ module Clientele
89
93
  end
90
94
  end
91
95
 
92
- def paginateable?; false; end
93
-
94
96
  private
95
97
 
96
98
  def inherited(base)
@@ -23,16 +23,16 @@ module Clientele
23
23
  Proc.new do
24
24
 
25
25
  def next_page(request)
26
- request.query[:page] ||= 0
26
+ request.query[:page] ||= 1
27
27
  request.query[:page] += 1
28
28
  end
29
29
 
30
- def total(response)
31
- response.headers.fetch('x-total-count', Float::INFINITY).to_f
30
+ def total(result)
31
+ Integer(result.response.headers['x-total-count']) or Float::INFINITY
32
32
  end
33
33
 
34
- def pages(response)
35
- response
34
+ def pages(result)
35
+ result
36
36
  end
37
37
 
38
38
  end
@@ -40,7 +40,7 @@ module Clientele
40
40
 
41
41
  module Iterator
42
42
 
43
- def paginateable?; true; end
43
+ @paginateable = true
44
44
 
45
45
  def each(request = self.to_request)
46
46
  return enum_for(:each, request) unless block_given?
@@ -50,9 +50,10 @@ module Clientele
50
50
 
51
51
  until counter == total(current_response) do
52
52
  if pages(current_response).empty?
53
- current_response = request.tap do |builder|
53
+ current_response = request.tap do |request|
54
54
  next_page(request)
55
55
  end.call
56
+
56
57
  else
57
58
  counter +=1
58
59
  yield pages(current_response).shift
@@ -1,3 +1,3 @@
1
1
  module Clientele
2
- VERSION = "0.3.9"
2
+ VERSION = "0.3.10"
3
3
  end
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.3.9
4
+ version: 0.3.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Keele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-29 00:00:00.000000000 Z
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday