openapi 0.9.7
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 +7 -0
- data/Changelog +7 -0
- data/License +20 -0
- data/README.md +2 -0
- data/Rakefile +8 -0
- data/lib/openapi/auth_token.rb +115 -0
- data/lib/openapi/client.rb +136 -0
- data/lib/openapi/exceptions.rb +6 -0
- data/lib/openapi/handlers/array.rb +44 -0
- data/lib/openapi/handlers/dummy.rb +31 -0
- data/lib/openapi/handlers/model.rb +30 -0
- data/lib/openapi/handlers/raw.rb +15 -0
- data/lib/openapi/handlers/response.rb +23 -0
- data/lib/openapi/handlers.rb +13 -0
- data/lib/openapi/metaclass.rb +11 -0
- data/lib/openapi/models/base.rb +16 -0
- data/lib/openapi/models.rb +11 -0
- data/lib/openapi/response.rb +34 -0
- data/lib/openapi/route.rb +136 -0
- data/lib/openapi/utils.rb +52 -0
- data/lib/openapi/validators.rb +19 -0
- data/lib/openapi/version.rb +3 -0
- data/lib/openapi.rb +19 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e366b43db91509db13581198cbabd097e4d92af
|
4
|
+
data.tar.gz: 64548818655ac85cdb45735daf9fb3d6a9a38d50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c020ed786a7d6c4722a674af7c9bac4da8c812e11adfca9fbc3e42f37434172566de174b5f2658896176bbd1939b9781613d471df03bafe228f9d133b020147e
|
7
|
+
data.tar.gz: a9e3649c95ba73b23f4eadd6821661665ed1216266edec63a4a6f734b6d6e78b7a1500e6ac79d529591763bc66c69275198a84a8326c48a1a83fe9dabcf35ba5
|
data/Changelog
ADDED
data/License
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Antoine Legrand (ant.legrand@gmail.com)
|
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
data/Rakefile
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
module OpenAPI
|
2
|
+
class AuthToken
|
3
|
+
attr_accessor :header, :key, :options, :header_format, :expires_at, :expires, :expires_in, :token, :refresh_token
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :key
|
7
|
+
|
8
|
+
def fetch_or_create(key, opts)
|
9
|
+
auth_token = fetch(key)
|
10
|
+
if auth_token.nil? || auth_token.renew?
|
11
|
+
self.class.new(opts)
|
12
|
+
else
|
13
|
+
auth_token
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch(key)
|
18
|
+
if OpenAPI.cache
|
19
|
+
begin
|
20
|
+
puts "fetch new: #{OpenAPI.cache.get(key)}"
|
21
|
+
return self.new(JSON.parse(OpenAPI.cache.get(key)))
|
22
|
+
rescue => e
|
23
|
+
puts e.message
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def key
|
29
|
+
@key ||= self.name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def key
|
34
|
+
@key ||= self.class.key
|
35
|
+
end
|
36
|
+
|
37
|
+
def update(opts={}, save=false)
|
38
|
+
@options = @options.merge(opts)
|
39
|
+
@options.each do |k, v|
|
40
|
+
instance_variable_set("@#{k}", v)
|
41
|
+
end
|
42
|
+
@token ||= @options.delete("access_token")
|
43
|
+
@options["token"] = @token
|
44
|
+
if opts.has_key?("expires_at") && !opts["expires_at"].nil?
|
45
|
+
@expires_at = opts["expires_at"].to_i
|
46
|
+
elsif opts.has_key?("expires_in") && !opts["expires_in"].nil?
|
47
|
+
@options.delete("expires_in")
|
48
|
+
@expires_at = Time.now.utc.to_i + opts["expires_in"]
|
49
|
+
@options["expires_at"] = @expires_at
|
50
|
+
end
|
51
|
+
self.save if save
|
52
|
+
end
|
53
|
+
|
54
|
+
def [](key)
|
55
|
+
@options[key.to_s]
|
56
|
+
end
|
57
|
+
|
58
|
+
def initialize(opts={}, save=false)
|
59
|
+
@options = {}
|
60
|
+
opts = {"token" => nil,
|
61
|
+
"refresh_token" => nil,
|
62
|
+
"expires_at" => nil,
|
63
|
+
"expires_in" => nil,
|
64
|
+
"key" => nil,
|
65
|
+
"header" => "Authorization",
|
66
|
+
"header_format" => "Bearer %s"}.merge(opts)
|
67
|
+
update(opts, save)
|
68
|
+
end
|
69
|
+
|
70
|
+
def headers
|
71
|
+
{header => header_format % @token}
|
72
|
+
end
|
73
|
+
|
74
|
+
def expires_in
|
75
|
+
(expires_at.to_i - Time.now.utc.to_i).abs
|
76
|
+
end
|
77
|
+
|
78
|
+
def save
|
79
|
+
if OpenAPI.cache
|
80
|
+
OpenAPI.cache.set(key, to_hash.to_json, expires_in + 2.hours)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_hash
|
85
|
+
@options
|
86
|
+
end
|
87
|
+
|
88
|
+
def token(force_renew=false)
|
89
|
+
if renew? || force_renew
|
90
|
+
renew_token
|
91
|
+
end
|
92
|
+
@token
|
93
|
+
end
|
94
|
+
|
95
|
+
def renew?
|
96
|
+
@token == nil || @expires_at.to_i < Time.now.utc.to_i
|
97
|
+
end
|
98
|
+
|
99
|
+
def new_auth_token()
|
100
|
+
raise NotImplementedError
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
def renew_token
|
105
|
+
new_token = self.class.fetch(self.key)
|
106
|
+
if new_token.nil? || new_token.renew?
|
107
|
+
self.new_auth_token
|
108
|
+
self.save
|
109
|
+
else
|
110
|
+
update(new_token.to_hash)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'time'
|
3
|
+
require 'base64'
|
4
|
+
require 'oauth2'
|
5
|
+
|
6
|
+
require File.join(File.dirname(__FILE__), 'response')
|
7
|
+
|
8
|
+
module OpenAPI
|
9
|
+
begin
|
10
|
+
require 'system_timer'
|
11
|
+
Timer = SystemTimer
|
12
|
+
rescue LoadError
|
13
|
+
require 'timeout'
|
14
|
+
Timer = Timeout
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
attr_accessor :application_key, :application_secret, :max_retry,
|
19
|
+
:logger, :request_timeout, :client_id, :site, :auth_token, :api_version, :api_methods, :use_ssl, :cache
|
20
|
+
|
21
|
+
def api_methods
|
22
|
+
@api_methods ||= []
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_method(name, callback)
|
26
|
+
metaclass.instance_eval do
|
27
|
+
define_method(name, callback)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def call_api(request)
|
32
|
+
Timer.timeout(request_timeout) do
|
33
|
+
start_time = Time.now
|
34
|
+
response = http_client.request(request)
|
35
|
+
log_request_and_response(request, response, Time.now - start_time)
|
36
|
+
|
37
|
+
response = OpenAPI::Response.wrap(response)
|
38
|
+
return response
|
39
|
+
end
|
40
|
+
rescue Timeout::Error
|
41
|
+
unless logger.nil?
|
42
|
+
logger.error "OpenAPI request timed out after #{request_timeout} seconds: [#{request.path} #{request.body}]"
|
43
|
+
end
|
44
|
+
OpenAPI::Response.wrap(nil, :body => {:error => 'Request timeout'}, :code => '503')
|
45
|
+
end
|
46
|
+
|
47
|
+
def build_path(path, params=nil)
|
48
|
+
uri = URI("/#{api_v}/#{path.gsub(/:client_id/, client_id)}")
|
49
|
+
if params != nil
|
50
|
+
uri.query = URI.encode_www_form(params)
|
51
|
+
end
|
52
|
+
return uri
|
53
|
+
end
|
54
|
+
|
55
|
+
def auth_token
|
56
|
+
raise NotImplementedError
|
57
|
+
end
|
58
|
+
|
59
|
+
def do_request(http_method, path, options = {}, retried=0)
|
60
|
+
path = build_path(path, options[:params])
|
61
|
+
|
62
|
+
klass = Net::HTTP.const_get(http_method.to_s.capitalize)
|
63
|
+
request = klass.new(path.to_s)
|
64
|
+
request.add_field "Content-Type", options[:content_type] || "application/json"
|
65
|
+
if !auth_token.nil? && options[:skip_auth] != true
|
66
|
+
request.add_field "Authorization", (options[:access_token] || auth_token.token)
|
67
|
+
end
|
68
|
+
request.add_field "Accept", options[:accept] || "application/json"
|
69
|
+
#Authorization: Basic dWJ1ZHUtYXBpOmZHWTI4aypOZTh2YzA=
|
70
|
+
#Authorization: Bearer
|
71
|
+
request.body = options[:body] if options[:body]
|
72
|
+
response = call_api(request)
|
73
|
+
return response
|
74
|
+
end
|
75
|
+
|
76
|
+
def verify_configuration_values(*symbols)
|
77
|
+
absent_values = symbols.select{|symbol| instance_variable_get("@#{symbol}").nil? }
|
78
|
+
raise("Must configure #{absent_values.join(", ")} before making this request.") unless absent_values.empty?
|
79
|
+
end
|
80
|
+
|
81
|
+
def site
|
82
|
+
@site
|
83
|
+
end
|
84
|
+
|
85
|
+
def http_client
|
86
|
+
Net::HTTP.new(site, 443).tap{|http| http.use_ssl = true}
|
87
|
+
end
|
88
|
+
|
89
|
+
def log_request_and_response(request, response, time)
|
90
|
+
return if logger.nil?
|
91
|
+
time = (time * 1000).to_i
|
92
|
+
http_method = request.class.to_s.split('::')[-1]
|
93
|
+
logger.info "#{self.class.name} (#{time}ms): [#{http_method} #{request.path} #{request.to_hash}, #{request.body}], [#{response.code}, #{response.body}]"
|
94
|
+
logger.flush if logger.respond_to?(:flush)
|
95
|
+
end
|
96
|
+
|
97
|
+
def format_time(time)
|
98
|
+
time = Time.parse(time) if time.is_a?(String)
|
99
|
+
time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
|
100
|
+
end
|
101
|
+
|
102
|
+
def request_timeout
|
103
|
+
@request_timeout || 5.0
|
104
|
+
end
|
105
|
+
|
106
|
+
def api_v()
|
107
|
+
@api_version || "v1"
|
108
|
+
end
|
109
|
+
|
110
|
+
def logger
|
111
|
+
@logger ||= OpenAPI.logger
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
class << self
|
117
|
+
include ClassMethods
|
118
|
+
end
|
119
|
+
|
120
|
+
class Client
|
121
|
+
class << self
|
122
|
+
include ClassMethods
|
123
|
+
end
|
124
|
+
include ClassMethods
|
125
|
+
|
126
|
+
def initialize(options={})
|
127
|
+
@api_version = options[:api_version] || OpenAPI.api_version
|
128
|
+
@logger = options[:logger] || OpenAPI.logger
|
129
|
+
@application_key = options[:application_key] || OpenAPI.application_key
|
130
|
+
@application_secret = options[:application_secret] || OpenAPI.application_secret
|
131
|
+
@site = options[:site] || OpenAPI.site
|
132
|
+
@request_timeout = options[:request_timeout] || OpenAPI.request_timeout || 5
|
133
|
+
@max_retry = options[:max_retry] || OpenAPI.max_retry || 2
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require File.join(File.dirname(__FILE__), '../models')
|
3
|
+
module OpenAPI
|
4
|
+
module Handlers
|
5
|
+
class Array < OpenAPI::Handler
|
6
|
+
class << self
|
7
|
+
|
8
|
+
|
9
|
+
def method_missing(method_symbol, *arguments) #:nodoc:
|
10
|
+
method_name = method_symbol.to_s
|
11
|
+
if method_name =~ /^(\w+)_array$/
|
12
|
+
return self.send :construct_array, $1, *arguments
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def raw_array(response, options)
|
19
|
+
array = []
|
20
|
+
response.each do |model_attr|
|
21
|
+
array << model_attr
|
22
|
+
end
|
23
|
+
array = OpenAPI::Handlers::Response.wrap(array, response)
|
24
|
+
return array
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def construct_array(snake_name, response, options)
|
30
|
+
return self.failed(response) if not response.success?
|
31
|
+
array = []
|
32
|
+
klass_name = ActiveSupport::Inflector.camelize(snake_name, true)
|
33
|
+
klass = OpenAPI::Models.const_get(klass_name)
|
34
|
+
response.each do |model_attr|
|
35
|
+
array << klass.new.from_json(model_attr.to_json)
|
36
|
+
end
|
37
|
+
array = OpenAPI::Handlers::Response.wrap(array, response)
|
38
|
+
return array
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../handlers')
|
2
|
+
|
3
|
+
module OpenAPI
|
4
|
+
module Handlers
|
5
|
+
|
6
|
+
class Dummy < OpenAPI::Handler
|
7
|
+
class << self
|
8
|
+
def titi(id, name)
|
9
|
+
puts "id:#{id.to_s[0..10]}, name:#{name}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Campaigns < OpenAPI::Handler
|
15
|
+
class << self
|
16
|
+
def stats(response, options)
|
17
|
+
return response, options
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Offers < OpenAPI::Handler
|
23
|
+
class << self
|
24
|
+
def index(response, options)
|
25
|
+
return response, options
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require File.join(File.dirname(__FILE__), '../models')
|
3
|
+
module OpenAPI
|
4
|
+
module Handlers
|
5
|
+
class Model < OpenAPI::Handler
|
6
|
+
class << self
|
7
|
+
def method_missing(method_symbol, *arguments) #:nodoc:
|
8
|
+
method_name = method_symbol.to_s
|
9
|
+
if method_name =~ /^(\w+)_model$/
|
10
|
+
return self.send :construct_model, $1, *arguments
|
11
|
+
else
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def construct_model(snake_name, response, options)
|
19
|
+
return self.failed(response) if not response.success?
|
20
|
+
klass_name = ActiveSupport::Inflector.camelize(snake_name, true)
|
21
|
+
klass = OpenAPI::Models.const_get(klass_name)
|
22
|
+
model = klass.new.from_json(response.raw)
|
23
|
+
model = OpenAPI::Handlers::Response.wrap(model, response)
|
24
|
+
return model
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require File.join(File.dirname(__FILE__), '../models')
|
3
|
+
module OpenAPI
|
4
|
+
module Handlers
|
5
|
+
class Raw < OpenAPI::Handler
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def text(response, options)
|
9
|
+
return self.failed(response) if not response.success?
|
10
|
+
return response
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module OpenAPI
|
2
|
+
module Handlers
|
3
|
+
module Response
|
4
|
+
module InstanceMethods
|
5
|
+
attr_accessor :response
|
6
|
+
|
7
|
+
def code
|
8
|
+
response.code
|
9
|
+
end
|
10
|
+
|
11
|
+
def success?
|
12
|
+
response.success?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.wrap(obj, response)
|
17
|
+
obj.extend(OpenAPI::Handlers::Response::InstanceMethods)
|
18
|
+
obj.response = response
|
19
|
+
return obj
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module OpenAPI
|
2
|
+
class Handler
|
3
|
+
def self.failed(response)
|
4
|
+
# error = OpenAPI::Models::Error.new().from_json(response.raw)
|
5
|
+
model = OpenAPI::Handlers::Response.wrap(response.raw, response)
|
6
|
+
return model
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
dir = File.dirname(__FILE__)
|
12
|
+
handlers_dir = File.join(dir, "handlers")
|
13
|
+
Dir[handlers_dir + "/*.rb"].each {|file| require file }
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module OpenAPI
|
2
|
+
module Response
|
3
|
+
module InstanceMethods
|
4
|
+
attr_accessor :response, :options, :raw
|
5
|
+
|
6
|
+
def code
|
7
|
+
response.code.to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
def success?
|
11
|
+
!!(code =~ /^2/)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.wrap(response)
|
16
|
+
OpenAPI.logger.debug(response.body)
|
17
|
+
if !response.to_hash["content-type"].find{|a| a.match /.*json.*/}
|
18
|
+
output = response.body
|
19
|
+
else
|
20
|
+
begin
|
21
|
+
output = JSON.parse(response.body || '{}')
|
22
|
+
rescue JSON::ParserError => e
|
23
|
+
OpenAPI.logger.error(e.message)
|
24
|
+
OpenAPI.logger.error(e.backtrace.join("\n"))
|
25
|
+
output = {}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
output.extend(OpenAPI::Response::InstanceMethods)
|
29
|
+
output.response = response
|
30
|
+
output.raw = response.body
|
31
|
+
return output
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
module OpenAPI
|
3
|
+
module Route
|
4
|
+
# def static
|
5
|
+
# match '/track_coupon/:source/:id' => 'mobile_banner#track_coupon', :as_ => :track_coupon, :defaults => { :format => 'png' }
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
def check_params(path, params, model=nil)
|
9
|
+
keys = {}
|
10
|
+
parts = path.split("/")
|
11
|
+
parts.each do |part|
|
12
|
+
if part.start_with?(":")
|
13
|
+
keys[part] = true
|
14
|
+
name = part[1..-1].to_sym
|
15
|
+
if !params.has_key?(name)
|
16
|
+
if model.respond_to?(name)
|
17
|
+
params[name] = model.send name
|
18
|
+
else
|
19
|
+
# @todo raise real errors
|
20
|
+
raise "Missing params: set '#{part}' to complete '#{path}' request"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
return keys
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
def create_proc(klass, kmethod, path, options, client=nil)
|
31
|
+
proc = Proc.new() do |arg1=nil, arg2={}, p=nil|
|
32
|
+
if p != nil
|
33
|
+
uri_path = p.clone()
|
34
|
+
else
|
35
|
+
uri_path = path.clone()
|
36
|
+
end
|
37
|
+
if client == nil
|
38
|
+
client = OpenAPI::Route.get_client()
|
39
|
+
end
|
40
|
+
model = nil
|
41
|
+
if options.has_key?(:body) && !arg1.instance_of?(Hash)
|
42
|
+
model = arg1
|
43
|
+
params = arg2
|
44
|
+
if !model.instance_of?(options[:body])
|
45
|
+
raise "Expected #{options[:body]} class, got #{model.class} instead"
|
46
|
+
end
|
47
|
+
elsif arg1.instance_of?(Hash) && options.has_key?(:body) && arg1[:body] == nil
|
48
|
+
raise "No body provided"
|
49
|
+
else
|
50
|
+
if arg1 == nil || arg1.instance_of?(Hash)
|
51
|
+
params = arg1 || {}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
if model != nil
|
56
|
+
to_json_options = nil
|
57
|
+
if params.has_key?(:to_json)
|
58
|
+
to_json_options = params[:to_json]
|
59
|
+
elsif options.has_key?(:to_json)
|
60
|
+
to_json_options = options[:to_json]
|
61
|
+
end
|
62
|
+
if to_json_options
|
63
|
+
params[:body] = model.send :to_json, to_json_options
|
64
|
+
else
|
65
|
+
params[:body] = model.to_json
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
if params.has_key?(:display_help) && params[:display_help]
|
70
|
+
return options
|
71
|
+
end
|
72
|
+
|
73
|
+
parameters = params[:params] || {}
|
74
|
+
params.each do |key,v|
|
75
|
+
if options.has_key?(:params) && options[:params].has_key?(key)
|
76
|
+
parameters[key] = v
|
77
|
+
end
|
78
|
+
end
|
79
|
+
params[:params] = parameters
|
80
|
+
|
81
|
+
if options.has_key?(:extra)
|
82
|
+
options[:extra].each do |k,v|
|
83
|
+
if !params.has_key?(k)
|
84
|
+
params[k] = v
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
OpenAPI.logger.debug params
|
90
|
+
if not params.has_key?(:client_id)
|
91
|
+
params[:client_id] = client.client_id
|
92
|
+
end
|
93
|
+
OpenAPI::Route.check_params(uri_path, params, model)
|
94
|
+
|
95
|
+
#1. soon.wrap = do_request opts[:body] => postjson
|
96
|
+
new_path = uri_path.clone()
|
97
|
+
params.each do |key, v|
|
98
|
+
r = Regexp.compile(":" + key.to_s)
|
99
|
+
new_path.gsub!(r, v.to_s)
|
100
|
+
end
|
101
|
+
OpenAPI.logger.debug(new_path)
|
102
|
+
response = client.do_request(options[:via], new_path, params)
|
103
|
+
#2. callback
|
104
|
+
return klass.send kmethod.to_s.to_sym, response, params
|
105
|
+
end
|
106
|
+
return proc
|
107
|
+
end
|
108
|
+
|
109
|
+
def match(path, callback, name, options={:via => :get}, client=nil)
|
110
|
+
klass_name, klass_method = callback.split("#")
|
111
|
+
klass_name = klass_name.classify
|
112
|
+
klass = Object.const_get(klass_name)
|
113
|
+
if client == nil
|
114
|
+
client = get_client()
|
115
|
+
end
|
116
|
+
proc = create_proc(klass, klass_method, path, options , client)
|
117
|
+
client.api_methods << name
|
118
|
+
client.create_method(name.to_s, proc)
|
119
|
+
return true
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_client
|
123
|
+
@client ||= OpenAPI::Client
|
124
|
+
end
|
125
|
+
|
126
|
+
def draw(client=nil, &block)
|
127
|
+
@client = client
|
128
|
+
class_eval &block
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class << self
|
133
|
+
include ClassMethods
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module OpenAPI
|
4
|
+
module Utils
|
5
|
+
class Random
|
6
|
+
class << self
|
7
|
+
def hex(n=16)
|
8
|
+
SecureRandom.hex(n)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Date
|
14
|
+
class << self
|
15
|
+
|
16
|
+
def format(date)
|
17
|
+
date.strftime("%Y%m%d")
|
18
|
+
end
|
19
|
+
|
20
|
+
def now()
|
21
|
+
format(Time.now.utc())
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s()
|
26
|
+
@date_str
|
27
|
+
end
|
28
|
+
|
29
|
+
def date()
|
30
|
+
@date
|
31
|
+
end
|
32
|
+
|
33
|
+
def inititialize(t = nil)
|
34
|
+
if t == nil
|
35
|
+
t = Time.now.utc()
|
36
|
+
end
|
37
|
+
@date_str = OpenAPI::Utils::Date.format(t)
|
38
|
+
@date = t.dup()
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module ActiveRecord
|
45
|
+
module SecureRandom
|
46
|
+
class << self
|
47
|
+
def hex(n=16)
|
48
|
+
OpenAPI::Utils::Random.hex(n)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module OpenAPI
|
2
|
+
module Models
|
3
|
+
module Validation
|
4
|
+
class UrlValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
begin
|
7
|
+
uri = URI.parse(value)
|
8
|
+
resp = uri.kind_of?(URI::HTTP)
|
9
|
+
rescue URI::InvalidURIError
|
10
|
+
resp = false
|
11
|
+
end
|
12
|
+
unless resp == true
|
13
|
+
record.errors[attribute] << (options[:message] || "is not an url")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/openapi.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "active_support/core_ext/numeric/time"
|
2
|
+
require 'openapi/exceptions'
|
3
|
+
require 'openapi/utils'
|
4
|
+
require 'openapi/client'
|
5
|
+
require 'openapi/response'
|
6
|
+
require 'openapi/metaclass'
|
7
|
+
require 'openapi/handlers'
|
8
|
+
require 'openapi/route'
|
9
|
+
require 'openapi/models'
|
10
|
+
require 'openapi/auth_token'
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
## MOVE TO APP ""
|
15
|
+
require 'logger'
|
16
|
+
OpenAPI.logger = Logger.new(STDOUT)
|
17
|
+
OpenAPI.request_timeout = 5 # default
|
18
|
+
OpenAPI.max_retry=2
|
19
|
+
OpenAPI.cache = nil
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Antoine Legrand
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.8.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.8.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: fakeweb
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A lots of service expose a more or less complex API. The library provides
|
84
|
+
common tasks to easily build a ruby wrapper
|
85
|
+
email:
|
86
|
+
- ant.legrand@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- Changelog
|
92
|
+
- License
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/openapi.rb
|
96
|
+
- lib/openapi/auth_token.rb
|
97
|
+
- lib/openapi/client.rb
|
98
|
+
- lib/openapi/exceptions.rb
|
99
|
+
- lib/openapi/handlers.rb
|
100
|
+
- lib/openapi/handlers/array.rb
|
101
|
+
- lib/openapi/handlers/dummy.rb
|
102
|
+
- lib/openapi/handlers/model.rb
|
103
|
+
- lib/openapi/handlers/raw.rb
|
104
|
+
- lib/openapi/handlers/response.rb
|
105
|
+
- lib/openapi/metaclass.rb
|
106
|
+
- lib/openapi/models.rb
|
107
|
+
- lib/openapi/models/base.rb
|
108
|
+
- lib/openapi/response.rb
|
109
|
+
- lib/openapi/route.rb
|
110
|
+
- lib/openapi/utils.rb
|
111
|
+
- lib/openapi/validators.rb
|
112
|
+
- lib/openapi/version.rb
|
113
|
+
homepage: http://github.com/antlegrand/openapi
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.9.2
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.4.5
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: A Ruby library to build api wrapper
|
137
|
+
test_files: []
|
138
|
+
has_rdoc:
|