modgen 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +121 -0
- data/Rakefile +7 -0
- data/lib/modgen/api/api_request.rb +35 -0
- data/lib/modgen/api/api_response.rb +9 -0
- data/lib/modgen/api/request.rb +72 -0
- data/lib/modgen/api/response.rb +41 -0
- data/lib/modgen/api.rb +59 -0
- data/lib/modgen/configuration.rb +47 -0
- data/lib/modgen/core_ext/hash.rb +41 -0
- data/lib/modgen/core_ext/object.rb +10 -0
- data/lib/modgen/core_ext/string.rb +9 -0
- data/lib/modgen/default_configuration.rb +29 -0
- data/lib/modgen/discovery/discovery.rb +71 -0
- data/lib/modgen/discovery/method.rb +137 -0
- data/lib/modgen/discovery/resource.rb +53 -0
- data/lib/modgen/errors.rb +21 -0
- data/lib/modgen/session/api_key.rb +42 -0
- data/lib/modgen/session/oauth2.rb +94 -0
- data/lib/modgen/session.rb +26 -0
- data/lib/modgen/version.rb +3 -0
- data/lib/modgen.rb +43 -0
- data/modgen.gemspec +29 -0
- data/spec/lib/01_authenticate_spec.rb +18 -0
- data/spec/lib/02_discovery_spec.rb +99 -0
- data/spec/lib/03_api_spec.rb +86 -0
- data/spec/spec_helper.rb +14 -0
- data/test_api/.gitignore +15 -0
- data/test_api/Gemfile +7 -0
- data/test_api/README.md +11 -0
- data/test_api/Rakefile +7 -0
- data/test_api/app/api/api.rb +30 -0
- data/test_api/app/api/dataset.rb +67 -0
- data/test_api/app/api/files/version_v1.json +98 -0
- data/test_api/app/api/files/versions.json +20 -0
- data/test_api/app/api/profile.rb +47 -0
- data/test_api/app/assets/images/rails.png +0 -0
- data/test_api/app/assets/javascripts/application.js +15 -0
- data/test_api/app/assets/stylesheets/application.css +13 -0
- data/test_api/app/controllers/application_controller.rb +3 -0
- data/test_api/app/helpers/application_helper.rb +2 -0
- data/test_api/app/mailers/.gitkeep +0 -0
- data/test_api/app/models/.gitkeep +0 -0
- data/test_api/app/models/user.rb +3 -0
- data/test_api/app/views/layouts/application.html.erb +14 -0
- data/test_api/config/application.rb +65 -0
- data/test_api/config/boot.rb +6 -0
- data/test_api/config/database.yml +25 -0
- data/test_api/config/environment.rb +5 -0
- data/test_api/config/environments/development.rb +37 -0
- data/test_api/config/environments/production.rb +67 -0
- data/test_api/config/environments/test.rb +37 -0
- data/test_api/config/initializers/backtrace_silencers.rb +7 -0
- data/test_api/config/initializers/inflections.rb +15 -0
- data/test_api/config/initializers/mime_types.rb +5 -0
- data/test_api/config/initializers/reload_api.rb +9 -0
- data/test_api/config/initializers/secret_token.rb +7 -0
- data/test_api/config/initializers/session_store.rb +8 -0
- data/test_api/config/initializers/wrap_parameters.rb +14 -0
- data/test_api/config/locales/en.yml +5 -0
- data/test_api/config/routes.rb +5 -0
- data/test_api/config.ru +4 -0
- data/test_api/db/migrate/20130510113923_create_users.rb +13 -0
- data/test_api/db/schema.rb +25 -0
- data/test_api/db/seeds.rb +7 -0
- data/test_api/lib/assets/.gitkeep +0 -0
- data/test_api/lib/tasks/.gitkeep +0 -0
- data/test_api/lib/tasks/test_data.rake +11 -0
- data/test_api/log/.gitkeep +0 -0
- data/test_api/model.rb +7 -0
- data/test_api/public/404.html +26 -0
- data/test_api/public/422.html +26 -0
- data/test_api/public/500.html +25 -0
- data/test_api/public/favicon.ico +0 -0
- data/test_api/public/index.html +241 -0
- data/test_api/public/robots.txt +5 -0
- data/test_api/script/rails +6 -0
- data/test_api/test/fixtures/users.yml +15 -0
- data/test_api/test/unit/user_test.rb +7 -0
- metadata +228 -0
@@ -0,0 +1,137 @@
|
|
1
|
+
module Modgen
|
2
|
+
module API
|
3
|
+
class Method
|
4
|
+
|
5
|
+
attr_reader :name, :path, :url, :http_method, :description, :method_parameters
|
6
|
+
|
7
|
+
def initialize(name, values)
|
8
|
+
@name = name
|
9
|
+
@values = values
|
10
|
+
|
11
|
+
@path = values['path']
|
12
|
+
@url = URI.join(Modgen.config.api.base_url, "#{Modgen::API.api.version}/", @path)
|
13
|
+
@http_method = values['http_method']
|
14
|
+
@description = values['description']
|
15
|
+
@method_parameters = values['parameters']
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(params)
|
19
|
+
# if params == nil
|
20
|
+
# return self
|
21
|
+
# end
|
22
|
+
|
23
|
+
query(params)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Check if value has property type(s)
|
27
|
+
#
|
28
|
+
def check_type(param, value, *types)
|
29
|
+
types.each do |type|
|
30
|
+
return(true) if value.is_a?(type)
|
31
|
+
end
|
32
|
+
|
33
|
+
raise Modgen::APIRequestError, "Parameter #{param} has invalid type. Must be #{types.join(' or ')}."
|
34
|
+
end
|
35
|
+
|
36
|
+
# Check one parameter
|
37
|
+
#
|
38
|
+
def validate_parameter(param, spec, value)
|
39
|
+
if value.nil?
|
40
|
+
if spec['required']
|
41
|
+
raise Modgen::APIRequestError, "Parameter #{param} is required."
|
42
|
+
end
|
43
|
+
|
44
|
+
return 'next'
|
45
|
+
end
|
46
|
+
|
47
|
+
case spec['type']
|
48
|
+
when 'integer'; check_type(param, value, Integer)
|
49
|
+
when 'float'; check_type(param, value, Integer, Float)
|
50
|
+
when 'string'; check_type(param, value, String)
|
51
|
+
when 'hash'
|
52
|
+
check_type(param, value, Hash)
|
53
|
+
validate_parameters(spec['attributes'], value)
|
54
|
+
when 'file'
|
55
|
+
if !File.file?(value)
|
56
|
+
raise Modgen::APIRequestError, "File #{value} doesn't exists."
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if spec['format']
|
61
|
+
unless value =~ /#{spec['format']}/
|
62
|
+
raise Modgen::APIRequestError, "Parameter #{param} hasn't required format (#{spec['format']})."
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Check all parameters
|
68
|
+
#
|
69
|
+
def validate_parameters(attributes, params = {})
|
70
|
+
if !attributes
|
71
|
+
return nil
|
72
|
+
end
|
73
|
+
|
74
|
+
params.stringify_keys!
|
75
|
+
|
76
|
+
parameters_left = params.keys - attributes.keys
|
77
|
+
if !parameters_left.empty?
|
78
|
+
raise Modgen::APIRequestError, "Parameters: #{parameters_left} are unknow."
|
79
|
+
end
|
80
|
+
|
81
|
+
attributes.each do |param, spec|
|
82
|
+
value = params[param]
|
83
|
+
|
84
|
+
if validate_parameter(param, spec, value) == 'next'
|
85
|
+
next
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def build_file(path)
|
91
|
+
mime = MimeMagic.by_path(path)
|
92
|
+
Faraday::UploadIO.new(path, mime)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Buld request data based on discoverd API
|
96
|
+
#
|
97
|
+
def build_request_data(params = {})
|
98
|
+
result = {
|
99
|
+
'path' => {},
|
100
|
+
'params' => {},
|
101
|
+
'body' => {}
|
102
|
+
}
|
103
|
+
|
104
|
+
params.each do |key, value|
|
105
|
+
case @method_parameters[key]['location']
|
106
|
+
when 'path'; result['path'][key] = value
|
107
|
+
when 'body'; result['body'][key] = value
|
108
|
+
else
|
109
|
+
if @method_parameters[key]['type'] == 'file'
|
110
|
+
result['params'][key] = build_file(value)
|
111
|
+
else
|
112
|
+
result['params'][key] = value
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
result
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create request and call response on it.
|
121
|
+
#
|
122
|
+
def query(params)
|
123
|
+
request_data = {}
|
124
|
+
|
125
|
+
if !params.is_a?(Hash)
|
126
|
+
raise Modgen::TypeError, "Parameters must be Hash. #{params.class.name} inserted."
|
127
|
+
end
|
128
|
+
|
129
|
+
validate_parameters(@method_parameters, params)
|
130
|
+
request_data = build_request_data(params)
|
131
|
+
|
132
|
+
Modgen::API::APIRequest.new(self, request_data).response
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Modgen
|
2
|
+
module API
|
3
|
+
class Resource
|
4
|
+
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(name, values)
|
8
|
+
@name = name
|
9
|
+
@values = values
|
10
|
+
@methods = []
|
11
|
+
|
12
|
+
build_resources
|
13
|
+
end
|
14
|
+
|
15
|
+
def methods
|
16
|
+
@methods
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def build_resources
|
22
|
+
@values.each do |key, value|
|
23
|
+
if key == 'methods'
|
24
|
+
build_methods(value)
|
25
|
+
else
|
26
|
+
build_resource(key, value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_resource(name, values)
|
32
|
+
resource = Modgen::API::Resource.new(name, values)
|
33
|
+
|
34
|
+
@methods << name.to_sym
|
35
|
+
|
36
|
+
metaclass = class << self; self; end
|
37
|
+
metaclass.send(:define_method, name) { resource }
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_methods(value)
|
41
|
+
value.each do |key, value|
|
42
|
+
method = Modgen::API::Method.new(key, value)
|
43
|
+
|
44
|
+
@methods << key.to_sym
|
45
|
+
|
46
|
+
metaclass = class << self; self; end
|
47
|
+
metaclass.send(:define_method, key) { |params={}| method.send(:call, params) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Modgen
|
2
|
+
|
3
|
+
# Errors in API
|
4
|
+
#
|
5
|
+
# e.g. API was not discovered
|
6
|
+
class APIError < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
# Error during request such as missing parameters
|
10
|
+
class APIRequestError < StandardError
|
11
|
+
end
|
12
|
+
|
13
|
+
# Type error
|
14
|
+
class TypeError < StandardError
|
15
|
+
end
|
16
|
+
|
17
|
+
# Configuration error
|
18
|
+
class ConfigurationError < StandardError
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Modgen
|
2
|
+
module Session
|
3
|
+
class APIKey
|
4
|
+
|
5
|
+
def self.config
|
6
|
+
Modgen.config
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.start
|
10
|
+
if config.api_key.key == nil
|
11
|
+
raise Modgen::ConfigurationError, "API key cannot be nil."
|
12
|
+
end
|
13
|
+
|
14
|
+
client = Modgen::Session::APIKey.new
|
15
|
+
|
16
|
+
Modgen::Session.store(client)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@key = Modgen.config.api_key.key
|
21
|
+
end
|
22
|
+
|
23
|
+
def execute(request)
|
24
|
+
conn = Faraday.new(url: request.url)
|
25
|
+
|
26
|
+
if request.data['body'] && request.data['body'].empty?
|
27
|
+
response = conn.send(request.http_method, "", request.data['params']) { |req|
|
28
|
+
req.headers['Api-Key'] = @key
|
29
|
+
}
|
30
|
+
else
|
31
|
+
response = conn.send(@http_method, "") { |req|
|
32
|
+
req.headers['Api-Key'] = @key
|
33
|
+
req.body = request.data['body']
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
response
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
|
3
|
+
module Modgen
|
4
|
+
module Session
|
5
|
+
class Oauth2
|
6
|
+
|
7
|
+
def self.config
|
8
|
+
Modgen.config
|
9
|
+
end
|
10
|
+
|
11
|
+
# Start session
|
12
|
+
#
|
13
|
+
#
|
14
|
+
def self.start
|
15
|
+
client = Modgen::Session::Oauth2.new
|
16
|
+
|
17
|
+
Modgen::Session.store(client)
|
18
|
+
|
19
|
+
if config.oauth2.redirect_uri == nil
|
20
|
+
get_authorize_code
|
21
|
+
else
|
22
|
+
client.authorize_url
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.authorize(auth_code)
|
27
|
+
Modgen::Session.get.authorize(auth_code)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Automaticaly open autorization url a waiting for callback.
|
31
|
+
# Launchy gem is required
|
32
|
+
#
|
33
|
+
# Steps:
|
34
|
+
# 1) create server
|
35
|
+
# 2) launch browser and redirect to google api
|
36
|
+
# 3) confirm and modgen redirect to localhost
|
37
|
+
# 4) server get code and start session
|
38
|
+
# 5) close server - you are login
|
39
|
+
def self.get_authorize_code
|
40
|
+
require "socket" # tcp server
|
41
|
+
|
42
|
+
uri = URI(Modgen::OAUTH2_REDIRECT_URI)
|
43
|
+
|
44
|
+
webserver = TCPServer.new(uri.host, 0) # start webserver
|
45
|
+
# port=0 - automatically choose free port
|
46
|
+
|
47
|
+
uri.port = webserver.addr[1] # get choosen port
|
48
|
+
|
49
|
+
Launchy.open(Modgen::Session.get.authorize_url)
|
50
|
+
|
51
|
+
session = webserver.accept
|
52
|
+
|
53
|
+
# parse header for query.
|
54
|
+
request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
|
55
|
+
request = Hash[URI.decode_www_form(URI(request).query)]
|
56
|
+
|
57
|
+
# failure login
|
58
|
+
to_return = false
|
59
|
+
message = "You have not been logged. Please try again."
|
60
|
+
|
61
|
+
if Modgen::Session.get.authorize(request['code'])
|
62
|
+
message = "You have been successfully logged. Now you can close the browser."
|
63
|
+
to_return = true
|
64
|
+
end
|
65
|
+
|
66
|
+
session.write(message)
|
67
|
+
session.close
|
68
|
+
|
69
|
+
return to_return
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
attr_reader :authorize_url
|
75
|
+
|
76
|
+
def initialize
|
77
|
+
@client = OAuth2::Client.new(client_id, client_secret, site: Modgen::SITE_URL)
|
78
|
+
|
79
|
+
@authorize_url = client.auth_code.authorize_url(redirect_uri: redirect_uri)
|
80
|
+
end
|
81
|
+
|
82
|
+
def authorize(auth_code)
|
83
|
+
@auth_code = auth_code
|
84
|
+
|
85
|
+
@token = @client.auth_code.get_token(auth_code)
|
86
|
+
end
|
87
|
+
|
88
|
+
def execute(request)
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Modgen
|
2
|
+
module Session
|
3
|
+
|
4
|
+
autoload :Oauth2, 'modgen/session/oauth2'
|
5
|
+
autoload :APIKey, 'modgen/session/api_key'
|
6
|
+
|
7
|
+
@@session = nil
|
8
|
+
|
9
|
+
def self.oauth2
|
10
|
+
Modgen::Session::Oauth2
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.api_key
|
14
|
+
Modgen::Session::APIKey
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.store(client)
|
18
|
+
@@session = client
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get
|
22
|
+
@@session
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
data/lib/modgen.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "modgen/core_ext/hash"
|
2
|
+
require "modgen/core_ext/object"
|
3
|
+
require "modgen/core_ext/string"
|
4
|
+
|
5
|
+
require "modgen/version"
|
6
|
+
require "modgen/configuration"
|
7
|
+
require "modgen/default_configuration"
|
8
|
+
require "modgen/errors"
|
9
|
+
|
10
|
+
require "faraday"
|
11
|
+
require "multi_json"
|
12
|
+
require "mimemagic"
|
13
|
+
|
14
|
+
module Modgen
|
15
|
+
|
16
|
+
autoload :Discovery, 'modgen/discovery/discovery'
|
17
|
+
autoload :API, 'modgen/api'
|
18
|
+
autoload :Session, 'modgen/session'
|
19
|
+
|
20
|
+
# Line set and get configuration
|
21
|
+
#
|
22
|
+
# *Set:* Modgen.config.key = "value"
|
23
|
+
#
|
24
|
+
# *Get:* Modgen.config.key
|
25
|
+
#
|
26
|
+
# == Returns:
|
27
|
+
# Top of Modgen::Configuration
|
28
|
+
#
|
29
|
+
def self.config
|
30
|
+
@config ||= Modgen::Configuration.new(DEFAULT_CONFIGURATION)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Cofiguration with DSL
|
34
|
+
#
|
35
|
+
# Modgen.configure do
|
36
|
+
# key "value"
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
def self.configure(&block)
|
40
|
+
config.instance_eval(&block)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/modgen.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'modgen/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "modgen"
|
9
|
+
spec.version = Modgen::VERSION
|
10
|
+
spec.authors = ["Ondřej Moravčík"]
|
11
|
+
spec.email = ["moravcik.ondrej@gmail.com"]
|
12
|
+
spec.description = %q{Modgen allow discover and use modgen's API}
|
13
|
+
spec.summary = %q{Gem for using modgen's API}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "faraday"
|
23
|
+
spec.add_dependency "multi_json"
|
24
|
+
spec.add_dependency "mimemagic"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "rspec"
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Modgen::Session do
|
4
|
+
it "should not be logged" do
|
5
|
+
Modgen::Session.get.should eql(nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "key cannot be nil" do
|
9
|
+
lambda { Modgen::Session.api_key.start }.should raise_error(Modgen::ConfigurationError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should use Api Key" do
|
13
|
+
Modgen.config.api_key.key = 'f0ddaaef2903e6f2ac634e7f6037c9b2'
|
14
|
+
Modgen::Session.api_key.start
|
15
|
+
|
16
|
+
lambda { Modgen::Session.api_key.start }.should_not raise_error(Modgen::ConfigurationError)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Modgen::Discovery do
|
6
|
+
before(:all) {
|
7
|
+
Modgen.configure do
|
8
|
+
api do
|
9
|
+
base_url "#{API_URL}/api/"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
}
|
13
|
+
|
14
|
+
let(:config) { Modgen.config }
|
15
|
+
let(:api) { Modgen::API.api }
|
16
|
+
let(:discovery) { Modgen::Discovery }
|
17
|
+
|
18
|
+
context "configure" do
|
19
|
+
it "api_base_url should be localhost" do
|
20
|
+
config.api.base_url.should eql("#{API_URL}/api/")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "api_discovery_versions_url shoud be base_url + versions" do
|
24
|
+
config.api.discovery_versions_url.to_s.should eql("#{API_URL}/api/discovery/versions")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "api_discovery_version_url shoud be base_url + version/:id" do
|
28
|
+
config.api.discovery_version_url.to_s.should eql("#{API_URL}/api/discovery/version/:id")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "API detils" do
|
33
|
+
it "should show preffered version" do
|
34
|
+
discovery.preffered_version.should eql('v1')
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should show 2 version" do
|
38
|
+
versions = discovery.versions
|
39
|
+
|
40
|
+
versions.status.should eql(200)
|
41
|
+
|
42
|
+
versions.body['versions'].size.should eql(2)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "non exist version" do
|
46
|
+
lambda { discovery.version('v3') }.should raise_error(Modgen::APIError)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "version should take preffered version" do
|
50
|
+
discovery.version.body['version'].should eql('v1')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "verion should have some values" do
|
54
|
+
result = discovery.version('v1').body
|
55
|
+
|
56
|
+
result['name'].should eql('Modgen API')
|
57
|
+
result['version'].should eql('v1')
|
58
|
+
result['created_at'].should eql('16022013')
|
59
|
+
result['updated_at'].should eql('16022013')
|
60
|
+
result['base_url'].should eql('https://modgen.net/api')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "before discover" do
|
65
|
+
it "shoud raise Modgen::APIError" do
|
66
|
+
lambda { api }.should raise_error(Modgen::APIError)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should false" do
|
70
|
+
Modgen::API.discovered?.should eql(false)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "discover" do
|
75
|
+
before(:all) {
|
76
|
+
Modgen::Discovery.discover('v1')
|
77
|
+
}
|
78
|
+
|
79
|
+
it "details about API" do
|
80
|
+
api.name.should eql('Modgen API')
|
81
|
+
api.description.should eql('Predikační platforma')
|
82
|
+
api.version.should eql('v1')
|
83
|
+
api.created_at.should eql('16022013')
|
84
|
+
api.updated_at.should eql('16022013')
|
85
|
+
api.base_url.should eql('https://modgen.net/api')
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should true" do
|
89
|
+
Modgen::API.discovered?.should eql(true)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "some methods should exist" do
|
93
|
+
Modgen::API.methods.should eql([:profile, :dataset])
|
94
|
+
Modgen::API.profile.methods.should eql([:get, :update])
|
95
|
+
Modgen::API.dataset.methods.should eql([:list, :get])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Modgen::API do
|
4
|
+
before(:all) do
|
5
|
+
Modgen.config.api_key.key = 'f0ddaaef2903e6f2ac634e7f6037c9b2'
|
6
|
+
Modgen::Session.api_key.start
|
7
|
+
|
8
|
+
@user = Modgen::API.profile.get.body
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:profile) { Modgen::API.profile }
|
12
|
+
let(:dataset) { Modgen::API.dataset }
|
13
|
+
|
14
|
+
context "profile" do
|
15
|
+
it "get" do
|
16
|
+
result = profile.get
|
17
|
+
|
18
|
+
result.status.should eql(200)
|
19
|
+
|
20
|
+
result.body["id"].should eql(@user["id"])
|
21
|
+
result.body["first_name"].should eql(@user["first_name"])
|
22
|
+
result.body["last_name"].should eql(@user["last_name"])
|
23
|
+
result.body["email"].should eql(@user["email"])
|
24
|
+
result.body["created_at"].should eql(@user["created_at"])
|
25
|
+
result.body["updated_at"].should eql(@user["updated_at"])
|
26
|
+
result.body["last_access"].should eql(@user["last_access"])
|
27
|
+
end
|
28
|
+
|
29
|
+
context "update" do
|
30
|
+
it "should update" do
|
31
|
+
result = profile.update(user: { first_name: "a",
|
32
|
+
last_name: "a",
|
33
|
+
email: "a@a.aa" })
|
34
|
+
|
35
|
+
result.status.should eql(200)
|
36
|
+
|
37
|
+
result.body["id"].should eql(@user["id"])
|
38
|
+
result.body["first_name"].should eql("a")
|
39
|
+
result.body["last_name"].should eql("a")
|
40
|
+
result.body["email"].should eql("a@a.aa")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "missing value" do
|
44
|
+
lambda { profile.update(first_name: "a") }.should raise_error(Modgen::APIRequestError)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "wrong format" do
|
48
|
+
lambda { profile.update(user: {first_name: "."}) }.should raise_error(Modgen::APIRequestError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "dataset" do
|
54
|
+
it "list" do
|
55
|
+
result = dataset.list
|
56
|
+
|
57
|
+
result.status.should eql(200)
|
58
|
+
|
59
|
+
result.body['datasets'].size.should eql(3)
|
60
|
+
end
|
61
|
+
|
62
|
+
context "get" do
|
63
|
+
it "should get" do
|
64
|
+
result = dataset.get(id: 1)
|
65
|
+
|
66
|
+
result.status.should eql(200)
|
67
|
+
|
68
|
+
result.body['id'].should eql(1)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "missing value" do
|
72
|
+
lambda { dataset.get }.should raise_error(Modgen::APIRequestError)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "non exist id" do
|
76
|
+
result = dataset.get(id: 6)
|
77
|
+
|
78
|
+
result.status.should eql(404)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "unknow parameteres" do
|
82
|
+
lambda { dataset.get(a: "a") }.should raise_error(Modgen::APIRequestError)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
require "modgen"
|
3
|
+
|
4
|
+
API_URL = 'http://localhost:3000'
|
5
|
+
|
6
|
+
API_KEY = 'f0ddaaef2903e6f2ac634e7f6037c9b2'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
# config.filter_run :focus
|
12
|
+
config.color_enabled = true
|
13
|
+
config.formatter = 'documentation'
|
14
|
+
end
|
data/test_api/.gitignore
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore_global
|
6
|
+
|
7
|
+
# Ignore bundler config
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore the default SQLite database.
|
11
|
+
/db/*.sqlite3
|
12
|
+
|
13
|
+
# Ignore all logfiles and tempfiles.
|
14
|
+
/log/*.log
|
15
|
+
/tmp
|