modgen 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +7 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +121 -0
  5. data/Rakefile +7 -0
  6. data/lib/modgen/api/api_request.rb +35 -0
  7. data/lib/modgen/api/api_response.rb +9 -0
  8. data/lib/modgen/api/request.rb +72 -0
  9. data/lib/modgen/api/response.rb +41 -0
  10. data/lib/modgen/api.rb +59 -0
  11. data/lib/modgen/configuration.rb +47 -0
  12. data/lib/modgen/core_ext/hash.rb +41 -0
  13. data/lib/modgen/core_ext/object.rb +10 -0
  14. data/lib/modgen/core_ext/string.rb +9 -0
  15. data/lib/modgen/default_configuration.rb +29 -0
  16. data/lib/modgen/discovery/discovery.rb +71 -0
  17. data/lib/modgen/discovery/method.rb +137 -0
  18. data/lib/modgen/discovery/resource.rb +53 -0
  19. data/lib/modgen/errors.rb +21 -0
  20. data/lib/modgen/session/api_key.rb +42 -0
  21. data/lib/modgen/session/oauth2.rb +94 -0
  22. data/lib/modgen/session.rb +26 -0
  23. data/lib/modgen/version.rb +3 -0
  24. data/lib/modgen.rb +43 -0
  25. data/modgen.gemspec +29 -0
  26. data/spec/lib/01_authenticate_spec.rb +18 -0
  27. data/spec/lib/02_discovery_spec.rb +99 -0
  28. data/spec/lib/03_api_spec.rb +86 -0
  29. data/spec/spec_helper.rb +14 -0
  30. data/test_api/.gitignore +15 -0
  31. data/test_api/Gemfile +7 -0
  32. data/test_api/README.md +11 -0
  33. data/test_api/Rakefile +7 -0
  34. data/test_api/app/api/api.rb +30 -0
  35. data/test_api/app/api/dataset.rb +67 -0
  36. data/test_api/app/api/files/version_v1.json +98 -0
  37. data/test_api/app/api/files/versions.json +20 -0
  38. data/test_api/app/api/profile.rb +47 -0
  39. data/test_api/app/assets/images/rails.png +0 -0
  40. data/test_api/app/assets/javascripts/application.js +15 -0
  41. data/test_api/app/assets/stylesheets/application.css +13 -0
  42. data/test_api/app/controllers/application_controller.rb +3 -0
  43. data/test_api/app/helpers/application_helper.rb +2 -0
  44. data/test_api/app/mailers/.gitkeep +0 -0
  45. data/test_api/app/models/.gitkeep +0 -0
  46. data/test_api/app/models/user.rb +3 -0
  47. data/test_api/app/views/layouts/application.html.erb +14 -0
  48. data/test_api/config/application.rb +65 -0
  49. data/test_api/config/boot.rb +6 -0
  50. data/test_api/config/database.yml +25 -0
  51. data/test_api/config/environment.rb +5 -0
  52. data/test_api/config/environments/development.rb +37 -0
  53. data/test_api/config/environments/production.rb +67 -0
  54. data/test_api/config/environments/test.rb +37 -0
  55. data/test_api/config/initializers/backtrace_silencers.rb +7 -0
  56. data/test_api/config/initializers/inflections.rb +15 -0
  57. data/test_api/config/initializers/mime_types.rb +5 -0
  58. data/test_api/config/initializers/reload_api.rb +9 -0
  59. data/test_api/config/initializers/secret_token.rb +7 -0
  60. data/test_api/config/initializers/session_store.rb +8 -0
  61. data/test_api/config/initializers/wrap_parameters.rb +14 -0
  62. data/test_api/config/locales/en.yml +5 -0
  63. data/test_api/config/routes.rb +5 -0
  64. data/test_api/config.ru +4 -0
  65. data/test_api/db/migrate/20130510113923_create_users.rb +13 -0
  66. data/test_api/db/schema.rb +25 -0
  67. data/test_api/db/seeds.rb +7 -0
  68. data/test_api/lib/assets/.gitkeep +0 -0
  69. data/test_api/lib/tasks/.gitkeep +0 -0
  70. data/test_api/lib/tasks/test_data.rake +11 -0
  71. data/test_api/log/.gitkeep +0 -0
  72. data/test_api/model.rb +7 -0
  73. data/test_api/public/404.html +26 -0
  74. data/test_api/public/422.html +26 -0
  75. data/test_api/public/500.html +25 -0
  76. data/test_api/public/favicon.ico +0 -0
  77. data/test_api/public/index.html +241 -0
  78. data/test_api/public/robots.txt +5 -0
  79. data/test_api/script/rails +6 -0
  80. data/test_api/test/fixtures/users.yml +15 -0
  81. data/test_api/test/unit/user_test.rb +7 -0
  82. metadata +228 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in modgen.gemspec
4
+ gemspec
5
+
6
+ gem 'oauth2'
7
+ gem 'launchy'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ondřej Moravčík
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # Modgen API klient
2
+
3
+ ## Instalation
4
+
5
+ Install
6
+
7
+ gem install modgen
8
+
9
+ Bundler
10
+
11
+ gem 'modgen'
12
+
13
+ Ruby
14
+
15
+ require 'modgen'
16
+
17
+
18
+
19
+ ## Configuration
20
+
21
+ ```ruby
22
+ Modgen.configure do
23
+ client_id "1"
24
+ end
25
+
26
+ # ------------ or ------------
27
+
28
+ Modgen.config.client_id = "1"
29
+ ```
30
+
31
+ List of all available conf.
32
+
33
+ <table>
34
+ <thead>
35
+ <tr>
36
+ <th><b>key</b></th>
37
+ <th><b>example and description</b></th>
38
+ </tr>
39
+ </thead>
40
+ <tbody>
41
+ <tr>
42
+ <td>oauth2.client_id</td>
43
+ <td>
44
+ Id of client generated by API server.<br><br>
45
+
46
+ <i>e.g. egt45e5t5trh54rth5rth54rt5h4r5t4h5rt4h</i>
47
+ </td>
48
+ </tr>
49
+ <tr>
50
+ <td>oauth2.client_secret</td>
51
+ <td>
52
+ Secret key generated by API server.<br><br>
53
+
54
+ <i>e.g. 4546846846th468684684t684hthrthrt6h871rh78888</i>
55
+ </td>
56
+ </tr>
57
+ <tr>
58
+ <td>oauth2.redirect_uri</td>
59
+ <td>
60
+ Address of web page where request will be redirected for gain access code.<br><br>
61
+
62
+ <i>e.g http://localhost/oauth2callback</i>
63
+ </td>
64
+ </tr>
65
+ </tbody>
66
+ </table>
67
+
68
+
69
+
70
+ ## Usage
71
+
72
+ First you must discovery API. For displaying all available versions.
73
+
74
+
75
+
76
+ ### Discovery
77
+
78
+ ```ruby
79
+ Modgen::Discovery.versions
80
+ ```
81
+
82
+ Details of specific version. If version is nil Modgen show preffered version.
83
+
84
+ ```ruby
85
+ Modgen::Discovery.version(version)
86
+ ```
87
+
88
+ Discover specific version. If version is nil Modgen discover preffered version.
89
+
90
+ ```ruby
91
+ Modgen::Discovery.discover(version)
92
+ ```
93
+
94
+
95
+
96
+ ### API details
97
+
98
+ ```ruby
99
+ Modgen::API.api.name
100
+ .description
101
+ .version
102
+ .created_at
103
+ .updated_at
104
+ .base_url
105
+ ```
106
+
107
+
108
+
109
+ ### API request
110
+
111
+
112
+ ```ruby
113
+ Modgen::API.dataset.get(:id 1)
114
+ ```
115
+
116
+
117
+ ## Test
118
+
119
+ First start testing API in /test_api and than.
120
+
121
+ rake
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,35 @@
1
+ module Modgen
2
+ module API
3
+ class APIRequest < Request
4
+
5
+ attr_reader :api_method
6
+
7
+ # Create APIRequest
8
+ #
9
+ # == Parameters:
10
+ # api_method:: Modgen::API::Method
11
+ # data::
12
+ # Hash
13
+ # {
14
+ # 'path' => {},
15
+ # 'params' => {},
16
+ # 'body' => {}
17
+ # }
18
+ #
19
+ def initialize(api_method, data)
20
+ @api_method = api_method
21
+
22
+ super(@api_method.url, data, @api_method.http_method.downcase)
23
+ end
24
+
25
+ private
26
+
27
+ def _response
28
+ response = Modgen::Session.get.execute(self)
29
+
30
+ Modgen::API::APIResponse.new(response, self)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ module Modgen
2
+ module API
3
+ class APIResponse < Response
4
+
5
+ # APIResponse is now the same as Response
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,72 @@
1
+ module Modgen
2
+ module API
3
+ class Request
4
+
5
+ attr_reader :origin_url, :url, :http_method, :data
6
+
7
+ # Create Request
8
+ #
9
+ # == Data
10
+ # === path
11
+ # data['path'] = {id: 1}
12
+ # url = http://a.a/:id
13
+ #
14
+ # url will be transfered to
15
+ # http://a.a/1
16
+ #
17
+ # === params
18
+ # normal parameters send with request
19
+ #
20
+ # === body
21
+ # this will be posted to the body of request
22
+ #
23
+ # == Parameters:
24
+ # url:: full www adress
25
+ # data::
26
+ # Hash
27
+ # {
28
+ # 'path' => {},
29
+ # 'params' => {},
30
+ # 'body' => {}
31
+ # }
32
+ # http_method::
33
+ # http method
34
+ #
35
+ # *default:* :get
36
+ #
37
+ def initialize(url, data = {}, http_method = :get)
38
+
39
+ @origin_url = url
40
+ @url = url
41
+
42
+ if data['path']
43
+ @url = url.to_s.gsub(/:([a-z][a-z0-9_]*)/) { data['path'][$1] }
44
+ end
45
+
46
+ @data = data
47
+ @http_method = http_method.to_sym
48
+ end
49
+
50
+ # Send request
51
+ #
52
+ def response
53
+ @response ||= _response
54
+ end
55
+
56
+ private
57
+
58
+ def _response
59
+ conn = Faraday.new(url: @url)
60
+
61
+ if @data['body'] && @data['body'].empty?
62
+ response = conn.send(@http_method, "", @data['params'])
63
+ else
64
+ response = conn.send(@http_method, "") { |req| req.body = @data['body'] }
65
+ end
66
+
67
+ Modgen::API::Response.new(response, self)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,41 @@
1
+ module Modgen
2
+ module API
3
+ class Response
4
+
5
+ attr_reader :data, :request, :content_type, :body, :status
6
+
7
+ def initialize(data, request)
8
+ @data = data
9
+ @request = request
10
+
11
+ @status = data.status
12
+ @content_type = data.headers['content-type']
13
+ @body = _parse_body
14
+ end
15
+
16
+ def error?
17
+ @status != 200 && @status != 201
18
+ end
19
+
20
+ def error_message
21
+ if error? && @body
22
+ @body['error']
23
+ end
24
+ end
25
+
26
+ def inspect
27
+ %{#<Modgen::API::Response::0x#{object_id} URL:"#{@request.url}" STATUS_CODE:"#{@status}" BODY:"#{@body}">}
28
+ end
29
+
30
+ private
31
+
32
+ def _parse_body
33
+ case @content_type
34
+ when 'application/json'
35
+ return MultiJson.load(@data.body)
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
data/lib/modgen/api.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'ostruct'
2
+
3
+ module Modgen
4
+ module API
5
+
6
+ autoload :Request, 'modgen/api/request'
7
+ autoload :Response, 'modgen/api/response'
8
+
9
+ autoload :APIRequest, 'modgen/api/api_request'
10
+ autoload :APIResponse, 'modgen/api/api_response'
11
+
12
+ autoload :Resource, 'modgen/discovery/resource'
13
+ autoload :Method, 'modgen/discovery/method'
14
+
15
+ @@api = nil
16
+ @@api_methods = {}
17
+
18
+ def self.api
19
+ @@api || raise(Modgen::APIError, "API has not been discovered yet.")
20
+ end
21
+
22
+ def self.discovered?
23
+ !@@api.nil?
24
+ end
25
+
26
+ # All available API methods on top
27
+ #
28
+ def self.methods
29
+ @@api_methods.methods
30
+ end
31
+
32
+ # All api methods go there
33
+ #
34
+ def self.method_missing(method, *args, &block)
35
+ api
36
+
37
+ @@api_methods.send(method, *args, &block)
38
+ end
39
+
40
+ # Set api from discovery
41
+ #
42
+ # == Parameters:
43
+ # api:: Hash
44
+ #
45
+ def self.set_api(api)
46
+ @@api = OpenStruct.new(api)
47
+ end
48
+
49
+ # Set api methods from discovery
50
+ #
51
+ # == Parameters:
52
+ # api:: Modgen::API::Resource
53
+ #
54
+ def self.set_api_methods(api)
55
+ @@api_methods = api
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,47 @@
1
+ module Modgen
2
+ class Configuration
3
+
4
+ DEFAULT = {}
5
+
6
+ def initialize(config, use_default = true)
7
+
8
+ if use_default
9
+ config = DEFAULT.merge(config)
10
+ end
11
+
12
+ config.each do |key, value|
13
+ eval <<-METHOD
14
+ def #{key}(value = nil, &block)
15
+ if block_given?
16
+ @#{key}.instance_eval(&block)
17
+ end
18
+
19
+ if value.nil?
20
+ if @#{key}.is_a?(Proc)
21
+ return @#{key}.call
22
+ end
23
+ return @#{key}
24
+ end
25
+
26
+ self.#{key} = value
27
+ end
28
+
29
+ def #{key}=(value)
30
+ @#{key} = value
31
+ end
32
+ METHOD
33
+
34
+ self.send("#{key}=", value)
35
+ end
36
+ end
37
+
38
+ def configure(&block)
39
+ if block_given?
40
+ yield self
41
+ end
42
+
43
+ self
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,41 @@
1
+ class Hash
2
+
3
+ if !method_defined? :stringify_keys
4
+ def stringify_keys
5
+ dup.stringify_keys!
6
+ end
7
+ end
8
+
9
+ if !method_defined? :stringify_keys!
10
+ def stringify_keys!
11
+ keys.each do |key|
12
+ self[key.to_s] = delete(key)
13
+ end
14
+ self
15
+ end
16
+ end
17
+
18
+ if !method_defined? :symbolize_keys
19
+ def symbolize_keys
20
+ dup.symbolize_keys!
21
+ end
22
+ end
23
+
24
+ if !method_defined? :symbolize_keys!
25
+ def symbolize_keys!
26
+ keys.each do |key|
27
+ self[(key.to_sym rescue key) || key] = delete(key)
28
+ end
29
+ self
30
+ end
31
+ end
32
+
33
+ if !method_defined? :to_param
34
+ def to_param(namespace = nil)
35
+ collect do |key, value|
36
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
37
+ end.sort * '&'
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,10 @@
1
+ class Object
2
+
3
+ if !method_defined? :to_query
4
+ def to_query(key)
5
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
6
+ "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+
3
+ if !method_defined? :to_param
4
+ def to_param
5
+ to_s
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,29 @@
1
+ require "uri"
2
+
3
+ module Modgen
4
+ SITE_URL = 'http://modgen.net'
5
+
6
+ OAUTH2_REDIRECT_URI = 'http://localhost/oauth2callback'
7
+
8
+ API_CONFIGURATION = {
9
+ base_url: 'http://modgen.net/api/',
10
+ discovery_versions_url: -> { URI.join(Modgen.config.api.base_url, 'discovery/versions') },
11
+ discovery_version_url: -> { URI.join(Modgen.config.api.base_url, 'discovery/version/:id') },
12
+ }
13
+
14
+ APIKEY_CONFIGURATION = {
15
+ key: nil
16
+ }
17
+
18
+ OAUTH2_CONFIGURATION = {
19
+ client_id: nil,
20
+ client_secret: nil,
21
+ redirect_uri: nil
22
+ }
23
+
24
+ DEFAULT_CONFIGURATION = {
25
+ api: Modgen::Configuration.new(API_CONFIGURATION),
26
+ api_key: Modgen::Configuration.new(APIKEY_CONFIGURATION),
27
+ oauth2: Modgen::Configuration.new(OAUTH2_CONFIGURATION)
28
+ }
29
+ end
@@ -0,0 +1,71 @@
1
+ module Modgen
2
+ module Discovery
3
+
4
+ def self.config
5
+ Modgen.config
6
+ end
7
+
8
+ # Download all version and return prefferd version.
9
+ # If there is not preffered, return nil
10
+ #
11
+ def self.preffered_version
12
+ versions.body['versions'].each do |version, details|
13
+ if details['preffered']
14
+ return version
15
+ end
16
+ end
17
+
18
+ nil
19
+ end
20
+
21
+ # All available version on the server
22
+ #
23
+ def self.versions
24
+ Modgen::API::Request.new(config.api.discovery_versions_url).response
25
+ end
26
+
27
+ # Details of one specific version
28
+ # If id=nil, client take preffered
29
+ #
30
+ def self.version(id = :auto)
31
+ if id == :auto
32
+ id = preffered_version
33
+ end
34
+
35
+ params = {'path' => {'id' => id}}
36
+
37
+ response = Modgen::API::Request.new(config.api.discovery_version_url, params).response
38
+
39
+ if response.error?
40
+ raise Modgen::APIError, response.error_message
41
+ end
42
+
43
+ response
44
+ end
45
+
46
+ # Discover selected API
47
+ # If id=nil, client take preffered
48
+ #
49
+ def self.discover(id = :auto)
50
+
51
+ data = version(id).body
52
+
53
+ api = {
54
+ name: data['name'],
55
+ description: data['description'],
56
+ version: data['version'],
57
+ created_at: data['created_at'],
58
+ updated_at: data['updated_at'],
59
+ base_url: data['base_url']
60
+ }
61
+ Modgen::API.set_api(api)
62
+
63
+ resources = Modgen::API::Resource.new('resources', data['resources'])
64
+
65
+ Modgen::API.set_api_methods(resources)
66
+
67
+ nil
68
+ end
69
+
70
+ end
71
+ end