lazy_ant 0.4.0 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26098317ee3b2d85a22f413947081f79b46d3a10
4
- data.tar.gz: a51b5237282e3e25e16fd81399df763f8997c871
3
+ metadata.gz: dc1b0f698c8286a77d8d2ec94e5de45f3259372c
4
+ data.tar.gz: e1b0e7cf90b6d1f14baf596f12017da9d631d7f4
5
5
  SHA512:
6
- metadata.gz: b2a4199fcd6ef7108ecb322f5ad0ecfba4990ad20947fae836d89f568c7540c977d260a136aeee4c753a3a72cd03fd4b982dc797d73fda14e7e3381ce7cc2192
7
- data.tar.gz: a770fd7c63460a97e75e2fdbb11a912e9e292bba92712de2f6fb59db10852021ed68fd6a11cf44613af635d2623afbcbe25cab45e0c8d2dc0a800a14665292d0
6
+ metadata.gz: 47eae520dbda96471446ad74d84fe0ae03acc4178b6e99de4da1067cb871767ec31c28d41bbb7dfc42d246c9516051d794d3670ff434f1759c924c3d40b0e490
7
+ data.tar.gz: 8def470c1337cecc099870e084802730ca49b605a65a374693657f9e25551c3df795546c3ac72412550fb57c9aa929a8ff0ec22757fdad9f6981f8f6bd8aa5fe
@@ -17,13 +17,6 @@ module LazyAnt
17
17
  con.adapter Faraday.default_adapter
18
18
  instance_exec(con, &default_callback) if default_callback
19
19
  end
20
- fix_params
21
- end
22
-
23
- def fix_params
24
- @default_params = @connection.params.dup
25
- @connection.params.clear
26
- @connection
27
20
  end
28
21
 
29
22
  def use_converter(con)
@@ -8,26 +8,16 @@ module LazyAnt
8
8
  include DSL::Connection
9
9
  include Grouping
10
10
 
11
- def update_params(req)
12
- case req.method
13
- when :get, :head, :delete
14
- default_params.each { |k, v| req.params[k] = v }
15
- when :post, :put
16
- default_params.each { |k, v| req.body[k] = v }
17
- end
18
- yield(req) if block_given?
19
- end
20
-
21
11
  module ClassMethods
22
12
  def api(name, options = {}, &block)
23
13
  method, path = endpoint(options)
14
+ klazz = Class.new(LazyAnt::Endpoint) do
15
+ send(method, path) if method && path
16
+ instance_eval(&block) if block
17
+ end
24
18
  converter = entity_converter(options)
25
19
  define_method name do |*args|
26
- params = args.extract_options!
27
- path = generate_url(path, args)
28
- response = connection.send(method, path, params) do |req|
29
- update_params(req, &block)
30
- end
20
+ response = klazz.new(*args).execute(connection)
31
21
  converter.call(response.body)
32
22
  end
33
23
  end
@@ -36,7 +26,7 @@ module LazyAnt
36
26
 
37
27
  def endpoint(options)
38
28
  method = [:get, :post, :put, :delete].find { |k| options[k] }
39
- fail 'Request url not given' unless method
29
+ return unless method
40
30
  path = options.delete(method)
41
31
  [method, path]
42
32
  end
@@ -46,18 +36,6 @@ module LazyAnt
46
36
  multi ? -> (x) { x.map(&conv) } : -> (x) { conv.call(x) }
47
37
  end
48
38
  end
49
-
50
- protected
51
-
52
- def generate_url(path, args)
53
- arg_names = path.scan(/:([\w_]+)/).map(&:first)
54
- arg_names.each do |k|
55
- arg = args.shift
56
- fail ArgumentError, "missing required key :#{k}" unless arg
57
- path = path.gsub(/:#{k}/, arg.to_s)
58
- end
59
- path
60
- end
61
39
  end
62
40
  end
63
41
  end
@@ -0,0 +1,91 @@
1
+ module LazyAnt
2
+ #
3
+ # Endpoint Definition
4
+ #
5
+ # (in dsl)
6
+ # api :echo do
7
+ # get '/echo'
8
+ # param :hello, default: 'world', required: true, rename: 'SHIT_KEY'
9
+ # end
10
+ #
11
+ # client.echo(hello: 'test')
12
+ # => GET '/echo?SHIT_KEY=test'
13
+ #
14
+ class Endpoint
15
+ extend Forwardable
16
+ def_delegators :'self.class', :verb, :path, :params
17
+
18
+ def initialize(*args)
19
+ @query = default_query.merge(args.extract_options!)
20
+ @url = build_url(*args)
21
+ end
22
+
23
+ def execute(connection)
24
+ @query = connection.params.merge(@query)
25
+ validate!
26
+ connection.send(verb, @url, renamed_query) do |req|
27
+ req.params.clear if [:put, :post].include?(req.method)
28
+ end
29
+ end
30
+
31
+ protected
32
+
33
+ def validate!
34
+ params.select { |_k, v| v[:required] }.each do |k, _v|
35
+ fail ArgumentError, "params[:#{k}] is required!" unless @query[k]
36
+ end
37
+ end
38
+
39
+ def build_url(*args)
40
+ args = Array(args)
41
+ url = path.dup
42
+ arg_names = url.scan(/:([\w_]+)/).map(&:first)
43
+ arg_names.each do |k|
44
+ arg = args.shift
45
+ fail ArgumentError, "missing required key :#{k}" unless arg
46
+ url.gsub!(/:#{k}/, arg.to_s)
47
+ end
48
+ url
49
+ end
50
+
51
+ def default_query
52
+ res = {}
53
+ params.each do |k, v|
54
+ res[k] = v[:default] if v[:default]
55
+ res
56
+ end
57
+ res
58
+ end
59
+
60
+ def renamed_query
61
+ query = @query.dup
62
+ params.each do |k, v|
63
+ if v[:rename]
64
+ query[v[:rename]] = query[k]
65
+ query.delete k
66
+ end
67
+ end
68
+ query
69
+ end
70
+
71
+ class << self
72
+ attr_reader :verb, :path
73
+ %w(get put post delete).each do |verb|
74
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
75
+ def #{verb}(path)
76
+ @verb = :#{verb}
77
+ @path = path
78
+ end
79
+ EOS
80
+ end
81
+
82
+ def params
83
+ @params ||= {}
84
+ end
85
+
86
+ def param(name, options = {})
87
+ params[name] = options
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,3 +1,3 @@
1
1
  module LazyAnt
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
data/lib/lazy_ant.rb CHANGED
@@ -4,3 +4,4 @@ require 'lazy_ant/dsl'
4
4
  require 'lazy_ant/config'
5
5
  require 'lazy_ant/group'
6
6
  require 'lazy_ant/converter'
7
+ require 'lazy_ant/endpoint'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_ant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - masarakki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -248,6 +248,7 @@ files:
248
248
  - lib/lazy_ant/dsl/connection.rb
249
249
  - lib/lazy_ant/dsl/endpoint.rb
250
250
  - lib/lazy_ant/dsl/grouping.rb
251
+ - lib/lazy_ant/endpoint.rb
251
252
  - lib/lazy_ant/group.rb
252
253
  - lib/lazy_ant/version.rb
253
254
  homepage: https://github.com/masarakki/lazy_ant