geekier_factory 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,7 @@
1
1
  module GeekierFactory
2
2
  class Action
3
+ AttributesMissing = Class.new(Exception)
4
+
3
5
  def initialize(api, structure)
4
6
  @api = api
5
7
  @structure = structure
@@ -13,6 +15,10 @@ module GeekierFactory
13
15
  params.select{ |p| p['paramType'] == 'body' }
14
16
  end
15
17
 
18
+ def path_params
19
+ params.select{ |p| p['paramType'] == 'path' }
20
+ end
21
+
16
22
  def url_params
17
23
  params.select{ |p| p['paramType'] == 'query' }
18
24
  end
@@ -27,8 +33,21 @@ module GeekierFactory
27
33
  param_values.select{ |k,v| names.include?(k.to_s) }
28
34
  end
29
35
 
36
+ def path_hash(param_values)
37
+ names = path_params.map{ |p| p['name'] }
38
+ param_values.select{ |k,v| names.include?(k.to_s) }
39
+ end
40
+
30
41
  def build_url(param_values)
31
- api_connection.build_url(path, url_hash(param_values))
42
+ vals = path_hash(param_values)
43
+ if (as = (path_variables - vals.keys.map(&:to_s))).any?
44
+ raise AttributesMissing.new(as)
45
+ end
46
+ p = path
47
+ vals.each do |k, v|
48
+ p = p.sub("{#{k}}", v)
49
+ end
50
+ api_connection.build_url(p, url_hash(param_values))
32
51
  end
33
52
 
34
53
  def build_body(param_values)
@@ -42,6 +61,10 @@ module GeekierFactory
42
61
  def path
43
62
  @structure['path'].start_with?('/') ? @structure['path'][1..-1] : @structure['path']
44
63
  end
64
+
65
+ def path_variables
66
+ path.scan(/\{(\w*)\}/).flatten
67
+ end
45
68
 
46
69
  def request_hash(param_values)
47
70
  {
@@ -1,3 +1,3 @@
1
1
  module GeekierFactory
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -40,12 +40,28 @@
40
40
  "name": "b2",
41
41
  "paramType": "body",
42
42
  "required": false
43
+ },
44
+ {
45
+ "allowMultiple": false,
46
+ "dataType": "string",
47
+ "description": "specify action",
48
+ "name": "action",
49
+ "paramType": "path",
50
+ "required": true
51
+ },
52
+ {
53
+ "allowMultiple": false,
54
+ "dataType": "string",
55
+ "description": "specify format",
56
+ "name": "format",
57
+ "paramType": "path",
58
+ "required": true
43
59
  }
44
60
  ],
45
61
  "summary": "Call to test the API"
46
62
  },
47
63
  ],
48
- "path": "/action.do"
64
+ "path": "/{action}.{format}"
49
65
  }
50
66
  ],
51
67
  "errorResponses": [
data/test/test_factory.rb CHANGED
@@ -21,8 +21,8 @@ class TestFactory < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  test "action should have 4 parameters" do
24
- assert_equal 4, @action.params.size
25
- assert_equal ['q1', 'q2', 'b1', 'b2'], @action.params.map{ |p| p['name'] }
24
+ assert_equal 6, @action.params.size
25
+ assert_equal ['q1', 'q2', 'b1', 'b2', 'action', 'format'], @action.params.map{ |p| p['name'] }
26
26
  end
27
27
 
28
28
  test "action should have 2 body and 2 query parameters and they should be different" do
@@ -32,8 +32,8 @@ class TestFactory < Test::Unit::TestCase
32
32
  end
33
33
 
34
34
  test "calling the action should call the right url" do
35
- stub_http_request(:post, "localhost/api/v0.1/action.do").
35
+ stub_http_request(:post, "localhost/api/v0.1/call.json").
36
36
  with(:body => {:b1 => "body", :b2 => "test"}, :query => {:q1 => 'testing', :q2 => 'query'})
37
- @action.call(:b1 => "body", :b2 => "test", :q1 => 'testing', :q2 => 'query')
37
+ @action.call(:b1 => "body", :b2 => "test", :q1 => 'testing', :q2 => 'query', :action => 'call', :format => 'json')
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geekier_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-03 00:00:00.000000000 Z
13
+ date: 2013-03-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler