domoscio_rails_local 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f6318acf62febe79287ac1993cce761558f772a
4
+ data.tar.gz: b8e1579ad39be7fea004f1c4578cb4e16b561332
5
+ SHA512:
6
+ metadata.gz: 3d7d80802aed9e22229cdf46cff84932995ad0d23afa375107fb8cb27168c24e6617c4231f429df5352b48666a2c0483d45af7ab87876841798a653f6a0503f6
7
+ data.tar.gz: c5e89cc49d588353e8c19a5b4d805f35572a9bec2c284a4f1905235a52c0a629ba2ad6f6dbe53579d0a7526de6ab30cff8c9e8d23adce0b3d79a9519d545ecd5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = DomoscioRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DomoscioRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,136 @@
1
+
2
+ require 'net/http'
3
+ require 'cgi/util'
4
+ require 'multi_json'
5
+
6
+ # helpers
7
+ require 'domoscio_rails/version'
8
+ require 'domoscio_rails/json'
9
+ require 'domoscio_rails/errors'
10
+ require 'domoscio_rails/authorization_token'
11
+
12
+ # resources
13
+ require 'domoscio_rails/http_calls'
14
+ require 'domoscio_rails/resource'
15
+ require 'domoscio_rails/admin/user'
16
+ require 'domoscio_rails/student/student'
17
+ require 'domoscio_rails/knowledge/knowledge_graph'
18
+ require 'domoscio_rails/knowledge/knowledge_edge'
19
+ require 'domoscio_rails/knowledge/knowledge_node'
20
+ require 'domoscio_rails/data/knowledge_node_student'
21
+ require 'domoscio_rails/data/result'
22
+
23
+
24
+ module DomoscioRails
25
+
26
+ class Configuration
27
+ attr_accessor :preproduction, :root_url,
28
+ :client_id, :client_passphrase,
29
+ :temp_dir
30
+
31
+ def preproduction
32
+ @preproduction || false
33
+ end
34
+
35
+ def root_url
36
+ @root_url || (@preproduction == true ? "http://localhost:3000" : "")
37
+ end
38
+ end
39
+
40
+ class << self
41
+ attr_accessor :configuration
42
+ end
43
+
44
+ def self.configure
45
+ self.configuration ||= Configuration.new
46
+ yield configuration
47
+ end
48
+
49
+ def self.api_uri(url='')
50
+ URI(configuration.root_url + url)
51
+ end
52
+
53
+ #
54
+ # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
55
+ # - +url+: the part after Configuration#root_url
56
+ # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
57
+ # - +filters+: hash; pagination params etc.; will encode it by URI and assign to URI#query
58
+ # - +headers+: hash; request_headers by default
59
+ # - +before_request_proc+: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
60
+ #
61
+ # Raises DomoscioRails::ResponseError if response code != 200.
62
+ #
63
+ def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
64
+ uri = api_uri(url)
65
+ uri.query = URI.encode_www_form(filters) unless filters.empty?
66
+
67
+ res = Net::HTTP.start(uri.host, uri.port) do |http| # , use_ssl: uri.scheme == 'https') do |http|
68
+ req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
69
+ req.body = DomoscioRails::JSON.dump(params)
70
+ before_request_proc.call(req) if before_request_proc
71
+ http.request req
72
+ end
73
+
74
+ # decode json data
75
+ begin
76
+ data = DomoscioRails::JSON.load(res.body.nil? ? '' : res.body)
77
+ rescue MultiJson::LoadError
78
+ data = {}
79
+ end
80
+
81
+ ############### TEMP!!!! #######################################################
82
+ #pp method, uri.request_uri, params #, filters, headers
83
+ #pp res, data
84
+ #puts
85
+
86
+ # if (!(res.is_a? Net::HTTPOK))
87
+ # ex = DomoscioRails::ResponseError.new(uri, res.code, data)
88
+ # ############## TEMP!!!! ########################################################
89
+ # #pp ex, data
90
+ # raise ex
91
+ # end
92
+
93
+ # copy pagination info if any
94
+ # ['x-number-of-pages', 'x-number-of-items'].each { |k|
95
+ # filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
96
+ # }
97
+
98
+ data
99
+ end
100
+
101
+ private
102
+
103
+ def self.user_agent
104
+ @uname ||= get_uname
105
+
106
+ {
107
+ bindings_version: DomoscioRails::VERSION,
108
+ lang: 'ruby',
109
+ lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
110
+ platform: RUBY_PLATFORM,
111
+ uname: @uname
112
+ }
113
+ end
114
+
115
+ def self.get_uname
116
+ `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
117
+ rescue Errno::ENOMEM
118
+ 'uname lookup failed'
119
+ end
120
+
121
+ def self.request_headers
122
+ auth_token = DomoscioRails::AuthorizationToken::Manager.get_token
123
+ headers = {
124
+ 'user_agent' => "DomoscioRails V2 RubyBindings/#{DomoscioRails::VERSION}",
125
+ 'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",#"#{auth_token['token_type']} #{auth_token['access_token']}",
126
+ 'Content-Type' => 'application/json'
127
+ }
128
+ # begin
129
+ # headers.update('x_mangopay_client_user_agent' => DomoscioRails::JSON.dump(user_agent))
130
+ # rescue => e
131
+ # headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
132
+ # end
133
+ end
134
+
135
+
136
+ end
Binary file
@@ -0,0 +1,7 @@
1
+ module DomoscioRails
2
+ # A company.
3
+ class Company < Resource
4
+ include DomoscioRails::HTTPCalls::Fetch
5
+ include DomoscioRails::HTTPCalls::Update
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module DomoscioRails
2
+ # A user of the API.
3
+ class User < Resource
4
+
5
+ include DomoscioRails::HTTPCalls::Create
6
+ include DomoscioRails::HTTPCalls::Fetch
7
+ include DomoscioRails::HTTPCalls::Update
8
+
9
+ end
10
+ end
@@ -0,0 +1,79 @@
1
+ module DomoscioRails
2
+ module AuthorizationToken
3
+
4
+ class Manager
5
+
6
+ class << self
7
+ def storage
8
+ @@storage ||= StaticStorage.new
9
+ end
10
+
11
+ def storage= (storage)
12
+ @@storage = storage
13
+ end
14
+
15
+ def get_token
16
+ # token = storage.get
17
+ # if token.nil? || token['timestamp'].nil? || token['timestamp'] <= Time.now
18
+ # token = DomoscioRails.request(:post, '/api/oauth/token', {}, {}, {}, Proc.new do |req|
19
+ # cfg = DomoscioRails.configuration
20
+ # req.basic_auth cfg.client_id, cfg.client_passphrase
21
+ # req.body = 'grant_type=client_credentials'
22
+ # end)
23
+ # token['timestamp'] = Time.now + token['expires_in'].to_i
24
+ # storage.store token
25
+ # end
26
+ token = DomoscioRails.configuration.client_passphrase
27
+ token
28
+ end
29
+ end
30
+ end
31
+
32
+ class StaticStorage
33
+ def get
34
+ @@token ||= nil
35
+ end
36
+
37
+ def store(token)
38
+ @@token = token
39
+ end
40
+ end
41
+
42
+ class FileStorage
43
+ require 'yaml'
44
+ @temp_dir
45
+
46
+ def initialize(temp_dir = nil)
47
+ @temp_dir = temp_dir || DomoscioRails.configuration.temp_dir
48
+ if !@temp_dir
49
+ raise "Path to temporary folder is not defined"
50
+ end
51
+ end
52
+
53
+ def get
54
+ begin
55
+ f = File.open(file_path, File::RDONLY)
56
+ f.flock(File::LOCK_SH)
57
+ txt = f.read
58
+ f.close
59
+ YAML.load(txt) || nil
60
+ rescue Errno::ENOENT
61
+ nil
62
+ end
63
+ end
64
+
65
+ def store(token)
66
+ File.open(file_path, File::RDWR|File::CREAT, 0644) do |f|
67
+ f.flock(File::LOCK_EX)
68
+ f.truncate(0)
69
+ f.rewind
70
+ f.puts(YAML.dump(token))
71
+ end
72
+ end
73
+
74
+ def file_path
75
+ File.join(@temp_dir, "DomoscioRails.AuthorizationToken.FileStore.tmp")
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,10 @@
1
+ module DomoscioRails
2
+ # A Knowledge Edge.
3
+ class KnowledgeNodeStudent < Resource
4
+
5
+ include DomoscioRails::HTTPCalls::Create
6
+ include DomoscioRails::HTTPCalls::Fetch
7
+ include DomoscioRails::HTTPCalls::Destroy
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module DomoscioRails
2
+ # A Student Result on a KnowledgeNode.
3
+ class Result < Resource
4
+
5
+ include DomoscioRails::HTTPCalls::Create
6
+ include DomoscioRails::HTTPCalls::Fetch
7
+ include DomoscioRails::HTTPCalls::Destroy
8
+
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ module DomoscioRails
2
+
3
+ # Generic error superclass for MangoPay specific errors.
4
+ # Currently never instantiated directly.
5
+ # Currently only single subclass used.
6
+ class Error < StandardError
7
+ end
8
+
9
+ # Thrown from any MangoPay API call whenever
10
+ # it returns response with HTTP code != 200.
11
+ class ResponseError < Error
12
+
13
+ attr_reader :request_url, :code, :details
14
+
15
+ def initialize(request_url, code, details)
16
+ @request_url, @code, @details = request_url, code, details
17
+ super(message) if message
18
+ end
19
+
20
+ def message; @details['Message']; end
21
+ def type; @details['Type']; end
22
+ def errors; @details['errors']; end
23
+ end
24
+ end
@@ -0,0 +1,59 @@
1
+ module DomoscioRails
2
+ module HTTPCalls
3
+
4
+ module Create
5
+ module ClassMethods
6
+ def create(*id, params)
7
+ id = id.empty? ? nil : id[0]
8
+ DomoscioRails.request(:post, url(id), params)
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+ end
16
+
17
+ module Update
18
+ module ClassMethods
19
+ def update(id = nil, params = {})
20
+ DomoscioRails.request(:put, url(id), params)
21
+ end
22
+ end
23
+
24
+ def self.included(base)
25
+ base.extend(ClassMethods)
26
+ end
27
+ end
28
+
29
+ module Fetch
30
+ module ClassMethods
31
+ def fetch(id_or_filters = nil)
32
+ id, filters = DomoscioRails::HTTPCalls::Fetch.parse_id_or_filters(id_or_filters)
33
+ response = DomoscioRails.request(:get, url(id), {}, filters)
34
+ end
35
+ end
36
+
37
+ def self.included(base)
38
+ base.extend(ClassMethods)
39
+ end
40
+
41
+ def self.parse_id_or_filters(id_or_filters = nil)
42
+ id_or_filters.is_a?(Hash) ? [nil, id_or_filters] : [id_or_filters, {}]
43
+ end
44
+ end
45
+
46
+ module Destroy
47
+ module ClassMethods
48
+ def destroy(id = nil, params = {})
49
+ DomoscioRails.request(:delete, url(id), params)
50
+ end
51
+ end
52
+
53
+ def self.included(base)
54
+ base.extend(ClassMethods)
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ module DomoscioRails
2
+ module JSON
3
+ class << self
4
+ if MultiJson.respond_to?(:dump)
5
+ def dump(*args)
6
+ MultiJson.dump(*args)
7
+ end
8
+
9
+ def load(*args)
10
+ MultiJson.load(*args)
11
+ end
12
+ else
13
+ def dump(*args)
14
+ MultiJson.encode(*args)
15
+ end
16
+
17
+ def load(*args)
18
+ MultiJson.decode(*args)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module DomoscioRails
2
+ # A Knowledge Edge.
3
+ class KnowledgeEdge < Resource
4
+
5
+ include DomoscioRails::HTTPCalls::Create
6
+ include DomoscioRails::HTTPCalls::Fetch
7
+ include DomoscioRails::HTTPCalls::Update
8
+ include DomoscioRails::HTTPCalls::Destroy
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module DomoscioRails
2
+ # A Knowledge Graph.
3
+ class KnowledgeGraph < Resource
4
+
5
+ include DomoscioRails::HTTPCalls::Create
6
+ include DomoscioRails::HTTPCalls::Fetch
7
+ include DomoscioRails::HTTPCalls::Update
8
+ include DomoscioRails::HTTPCalls::Destroy
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module DomoscioRails
2
+ # A Knowledge Node.
3
+ class KnowledgeNode < Resource
4
+
5
+ include DomoscioRails::HTTPCalls::Create
6
+ include DomoscioRails::HTTPCalls::Fetch
7
+ include DomoscioRails::HTTPCalls::Update
8
+ include DomoscioRails::HTTPCalls::Destroy
9
+
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module DomoscioRails
2
+ # @abstract
3
+ class Resource
4
+ class << self
5
+ def class_name
6
+ name.split('::')[-1]
7
+ end
8
+
9
+ def url(id = nil, nested_model = nil)
10
+ if self == Resource
11
+ raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.')
12
+ end
13
+
14
+ build_url = "/v1/companies/#{DomoscioRails.configuration.client_id}"
15
+ build_url << "/#{class_name.underscore}s"
16
+ if id
17
+ build_url << "/#{CGI.escape(id.to_s)}"
18
+ end
19
+ return build_url
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module DomoscioRails
2
+ # A company.
3
+ class Student < Resource
4
+ include DomoscioRails::HTTPCalls::Create
5
+ include DomoscioRails::HTTPCalls::Fetch
6
+ include DomoscioRails::HTTPCalls::Update
7
+ include DomoscioRails::HTTPCalls::Destroy
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module DomoscioRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :domoscio_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: domoscio_rails_local
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benoit Praly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: Description of DomoscioRails.
28
+ email:
29
+ - benoit.praly@domoscio.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - lib/domoscio_rails.rb
38
+ - lib/domoscio_rails.tbz
39
+ - lib/domoscio_rails/admin/company.rb
40
+ - lib/domoscio_rails/admin/user.rb
41
+ - lib/domoscio_rails/authorization_token.rb
42
+ - lib/domoscio_rails/data/knowledge_node_student.rb
43
+ - lib/domoscio_rails/data/result.rb
44
+ - lib/domoscio_rails/errors.rb
45
+ - lib/domoscio_rails/http_calls.rb
46
+ - lib/domoscio_rails/json.rb
47
+ - lib/domoscio_rails/knowledge/knowledge_edge.rb
48
+ - lib/domoscio_rails/knowledge/knowledge_graph.rb
49
+ - lib/domoscio_rails/knowledge/knowledge_node.rb
50
+ - lib/domoscio_rails/resource.rb
51
+ - lib/domoscio_rails/student/student.rb
52
+ - lib/domoscio_rails/version.rb
53
+ - lib/tasks/domoscio_rails_tasks.rake
54
+ homepage: http://www.domoscio.com
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Summary of DomoscioRails.
78
+ test_files: []