geekier_factory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 rules.io UG (haftungsbeschraenkt)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ geekier_factory_gem
2
+ ===================
3
+
4
+ Ruby gem for using APIs, based on their Geekier API descriptions.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+ Bundler.setup
4
+
5
+ require "rake/testtask"
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs = %w(lib test)
8
+ test.pattern = "test/**/test_*.rb"
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/geekier_factory/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "geekier_factory"
6
+ s.version = GeekierFactory::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Christian Weis", "David Anderson"]
9
+ s.email = "team@rules.io"
10
+ s.homepage = "http://github.com/rulesio/geekier_factory_gem"
11
+ s.summary = "Generate API objects from swagger definition files"
12
+ s.description = "Generate API objects from swagger definition files"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ s.add_development_dependency "bundler", "~> 1"
17
+ s.add_development_dependency "test-unit", "~> 2.2"
18
+ s.add_development_dependency "mocha"
19
+ s.add_development_dependency "rake"
20
+ s.add_development_dependency "webmock"
21
+
22
+ s.add_dependency "faraday"
23
+ s.add_dependency "faraday_middleware"
24
+ s.add_dependency "faraday_body_logger"
25
+
26
+ s.files = `git ls-files`.split("\n")
27
+ s.require_path = 'lib'
28
+ end
@@ -0,0 +1,83 @@
1
+ module GeekierFactory
2
+ class Action
3
+ def initialize(api, structure)
4
+ @api = api
5
+ @structure = structure
6
+ end
7
+
8
+ def params
9
+ @structure['parameters']
10
+ end
11
+
12
+ def body_params
13
+ params.select{ |p| p['paramType'] == 'body' }
14
+ end
15
+
16
+ def url_params
17
+ params.select{ |p| p['paramType'] == 'query' }
18
+ end
19
+
20
+ def url_hash(param_values)
21
+ names = url_params.map{ |p| p['name'] }
22
+ param_values.select{ |k,v| names.include?(k.to_s) }
23
+ end
24
+
25
+ def body_hash(param_values)
26
+ names = body_params.map{ |p| p['name'] }
27
+ param_values.select{ |k,v| names.include?(k.to_s) }
28
+ end
29
+
30
+ def build_url(param_values)
31
+ api_connection.build_url(path, url_hash(param_values))
32
+ end
33
+
34
+ def build_body(param_values)
35
+ body_hash(param_values)
36
+ end
37
+
38
+ def http_method
39
+ @structure['httpMethod']
40
+ end
41
+
42
+ def path
43
+ @structure['path'].start_with?('/') ? @structure['path'][1..-1] : @structure['path']
44
+ end
45
+
46
+ def request_hash(param_values)
47
+ {
48
+ :verb => http_method.downcase.to_sym,
49
+ :url => build_url(param_values),
50
+ :body => build_body(param_values)
51
+ }
52
+ end
53
+
54
+ def api_connection
55
+ @api.api_connection
56
+ end
57
+
58
+ def call(param_values)
59
+ reqhash = request_hash(param_values)
60
+ response = api_connection.run_request(reqhash[:verb], reqhash[:url], reqhash[:body], reqhash[:headers])
61
+ handle_response!(response)
62
+ {:request => reqhash, :response => response}
63
+ rescue Retry => e
64
+ retry
65
+ end
66
+
67
+ Retry = Class.new(Exception)
68
+ ConnectionException = Class.new(Exception)
69
+ def handle_response!(response)
70
+ if @structure['errorResponses'].map{ |er| er['code'] }.include? response.status
71
+ ex = @structure['errorResponses'].select{ |er| er['code'] == response.status }
72
+ if ex['retry'] == true && (@retries ||= 0) < ex[ ]
73
+ @retries += 1
74
+ raise Retry.new
75
+ else
76
+ raise ConnectionException.new("#{ex[:reason]} (HTTP status code #{ex[:code]})")
77
+ end
78
+ elsif !response.success?
79
+ raise ConnectionException.new
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,50 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'faraday_body_logger'
4
+
5
+ module GeekierFactory
6
+ class API
7
+ def initialize(structure)
8
+ @structure = structure
9
+ @actions = @structure['apis'].map{ |api| api['operations'].map{ |op| Action.new(self, op.merge('path' => api['path'])) } }.flatten
10
+ end
11
+
12
+ def log_body?
13
+ true
14
+ end
15
+
16
+ def api_connection
17
+ @connection ||= begin
18
+ Faraday.new(base_url, ssl: {verify: false}) do |builder|
19
+ builder.request :json
20
+
21
+ # builder.request :oauth, credentials_hash.merge(oauth_consumer_credentials) if oauth? and oauth_consumer_credentials.present?
22
+
23
+ builder.response :body_logger if log_body?
24
+
25
+ builder.adapter Faraday.default_adapter
26
+ end
27
+ end
28
+ end
29
+
30
+ def available_actions
31
+ @actions
32
+ end
33
+
34
+ def base_url
35
+ @structure['basePath'].end_with?('/') ? @structure['basePath'] : (@structure['basePath'] + '/')
36
+ end
37
+
38
+ # def oauth?
39
+ # true
40
+ # end
41
+ #
42
+ # def oauth_consumer_credentials
43
+ # {:consumer_key => 'xxxx', :consumer_secret => 'xxxx'}
44
+ # end
45
+ #
46
+ # def consumer
47
+ # OAuth::Consumer.new(oauth_consumer_credentials[:consumer_key], oauth_consumer_credentials[:consumer_secret], :site => base_url)
48
+ # end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ module GeekierFactory
2
+ class Resource
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module GeekierFactory
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'geekier_factory/api'
2
+ require 'geekier_factory/action'
3
+
4
+ module GeekierFactory
5
+ def self.factorize(filename_with_path)
6
+ description = File.read(filename_with_path)
7
+ structure = YAML.load(description)
8
+ API.new(structure)
9
+ end
10
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,5 @@
1
+ Bundler.require
2
+ require "test/unit"
3
+ # require "mocha"
4
+
5
+ require "geekier_factory"
@@ -0,0 +1,54 @@
1
+ {
2
+ "apis": [
3
+ {
4
+ "description": "test api",
5
+ "operations": [
6
+ {
7
+ "errorResponses": [
8
+ ],
9
+ "httpMethod": "POST",
10
+ "nickname": "test call",
11
+ "parameters": [
12
+ {
13
+ "allowMultiple": false,
14
+ "dataType": "string",
15
+ "description": "first query parameter",
16
+ "name": "q1",
17
+ "paramType": "query",
18
+ "required": true
19
+ },
20
+ {
21
+ "allowMultiple": false,
22
+ "dataType": "string",
23
+ "description": "second query parameter",
24
+ "name": "q2",
25
+ "paramType": "query",
26
+ "required": false
27
+ },
28
+ {
29
+ "allowMultiple": false,
30
+ "dataType": "string",
31
+ "description": "first body parameter",
32
+ "name": "b1",
33
+ "paramType": "body",
34
+ "required": true
35
+ },
36
+ {
37
+ "allowMultiple": false,
38
+ "dataType": "string",
39
+ "description": "second body parameter",
40
+ "name": "b2",
41
+ "paramType": "body",
42
+ "required": false
43
+ }
44
+ ],
45
+ "summary": "Call to test the API"
46
+ },
47
+ ],
48
+ "path": "/action.do"
49
+ }
50
+ ],
51
+ "basePath": "http://localhost/api/v0.1",
52
+ "resourcePath": "",
53
+ "swaggerVersion": "1.1"
54
+ }
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+ require "webmock"
3
+
4
+ class TestFactory < Test::Unit::TestCase
5
+ include WebMock::API
6
+
7
+ setup do
8
+ @api = GeekierFactory.factorize(File.join(File.dirname(File.expand_path(__FILE__)), 'mock_definition.json'))
9
+ @action = @api.available_actions.first
10
+ end
11
+
12
+ test "api should be factorized" do
13
+ assert @api
14
+ assert @api.is_a? GeekierFactory::API
15
+ end
16
+
17
+ test "api should have one action" do
18
+ assert_equal 1, @api.available_actions.size
19
+ end
20
+
21
+ test "action should have 4 parameters" do
22
+ assert_equal 4, @action.params.size
23
+ assert_equal ['q1', 'q2', 'b1', 'b2'], @action.params.map{ |p| p['name'] }
24
+ end
25
+
26
+ test "action should have 2 body and 2 query parameters and they should be different" do
27
+ assert_equal 2, @action.body_params.size
28
+ assert_equal 2, @action.url_params.size
29
+ assert_not_equal @action.body_params, @action.url_params
30
+ end
31
+
32
+ test "calling the action should call the right url" do
33
+ stub_http_request(:post, "localhost/api/v0.1/action.do").
34
+ with(:body => {:b1 => "body", :b2 => "test"}, :query => {:q1 => 'testing', :q2 => 'query'})
35
+ @action.call(:b1 => "body", :b2 => "test", :q1 => 'testing', :q2 => 'query')
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geekier_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Weis
9
+ - David Anderson
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-28 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1'
31
+ - !ruby/object:Gem::Dependency
32
+ name: test-unit
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '2.2'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '2.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: webmock
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: faraday
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: faraday_middleware
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :runtime
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: faraday_body_logger
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ type: :runtime
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ description: Generate API objects from swagger definition files
144
+ email: team@rules.io
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - .gitignore
150
+ - Gemfile
151
+ - LICENSE
152
+ - README.md
153
+ - Rakefile
154
+ - geekier_factory.gemspec
155
+ - lib/geekier_factory.rb
156
+ - lib/geekier_factory/action.rb
157
+ - lib/geekier_factory/api.rb
158
+ - lib/geekier_factory/resource.rb
159
+ - lib/geekier_factory/version.rb
160
+ - test/helper.rb
161
+ - test/mock_definition.json
162
+ - test/test_factory.rb
163
+ homepage: http://github.com/rulesio/geekier_factory_gem
164
+ licenses: []
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ none: false
177
+ requirements:
178
+ - - ! '>='
179
+ - !ruby/object:Gem::Version
180
+ version: 1.3.6
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 1.8.24
184
+ signing_key:
185
+ specification_version: 3
186
+ summary: Generate API objects from swagger definition files
187
+ test_files: []