cenit-algorithms 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 303df7e09bf488824064a5c81ee9c03f353e426b
4
+ data.tar.gz: bd611a2c75f312942f80841dac66bebe9ffce587
5
+ SHA512:
6
+ metadata.gz: e3af9671710e1017ac4898928eb069e6efda4589b99036c76d59214f74c285f3fa71d0af7a94d7343187ea2f41ce2e885e6c7fd36feb4eadee8bfc99edd215e6
7
+ data.tar.gz: 9c38ae4e2fb66e0adf2ec8456834bc93643e75803adb5a15d9ca8f2bb1fea3a3ee68587a681266d2a64a5f7691d26114d23b60a29464df1b0a3aa06a8e27c2e9
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'cenit-config'
6
+ gem 'httparty'
7
+ gem 'json'
8
+ gem 'parser'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Maikel Arcia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Cenit Algorithms
2
+
3
+ Run algorithms retrieving their codes from Cenit.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cenit-algorithms'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cenit-algorithms
20
+
21
+ ## Usage
22
+
23
+ Run some algorithms
24
+
25
+ ```ruby
26
+ require 'cenit/algorithms'
27
+
28
+ Cenit::Algorithms('Number Theory').factorial(5) # 120
29
+
30
+ Cenit::Algorithms('Number Theory').gcd(20, 15) # 5
31
+ ```
32
+
33
+ Checkout more shared algorithms at https://cenit.io/algorithms
34
+
35
+ Algorithms codes are loaded on demand and kept stored. If you want to reset your loaded algorithms then execute:
36
+
37
+ ```ruby
38
+ Cenit::Algorithms.reset
39
+ ```
40
+
41
+ If you want to run your own algorithms then configure your Cenit credentials:
42
+
43
+ ```ruby
44
+ Cenit.access_token 'XXXXXXXXXX'
45
+ Cenit.access_key 'KKKKKKKKKK'
46
+ ```
47
+
48
+ If you want to use your own instance of Cenit then configure your host URL by:
49
+
50
+ ```ruby
51
+ Cenit.host 'my-cenit-host-url'
52
+ ```
53
+
54
+ Learn more about Cenit at https://cenit.io
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/cenit-io/cenit-algorithms/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'cenit/algorithms'
5
+
6
+ require 'irb'
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cenit/algorithms/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cenit-algorithms'
8
+ spec.version = Cenit::Algorithms::VERSION
9
+ spec.authors = ['Maikel Arcia']
10
+ spec.email = ['macarci@gmail.com']
11
+
12
+ spec.summary = %q{Runs Cenit algorithms.}
13
+ spec.description = %q{Run algorithms retrieving their codes from Cenit.}
14
+ spec.homepage = 'https://github.com/cenit-io/cenit-algorithms'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.8'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+
25
+ spec.add_runtime_dependency 'cenit-config', '~> 0.0.1'
26
+ spec.add_runtime_dependency 'httparty', '~> 0.13.7'
27
+ spec.add_runtime_dependency 'json', '~> 1.8.3', '>= 1.8.3'
28
+ spec.add_runtime_dependency 'parser', '~> 2.3.1.4', '>= 2.3.1.4'
29
+ end
@@ -0,0 +1,87 @@
1
+ require 'cenit/algorithms/version'
2
+ require 'cenit/config'
3
+ require 'cenit/algorithms/namespace'
4
+ require 'httparty'
5
+
6
+ module Cenit
7
+
8
+ class << self
9
+
10
+ def Algorithms(ns_or_criteria)
11
+ case ns_or_criteria
12
+ when String
13
+ Algorithms::Namespace.new(ns_or_criteria)
14
+ when Hash
15
+ Cenit::Algorithms.find(ns_or_criteria)
16
+ else
17
+ fail "#{String} or #{Hash} expected but #{ns_or_criteria.class} found"
18
+ end
19
+ end
20
+ end
21
+
22
+ module Algorithms
23
+
24
+ class << self
25
+
26
+ def find(criteria)
27
+ id = criteria[:id] || criteria['id']
28
+ if (algorithm = cache[id])
29
+ return algorithm
30
+ end
31
+ namespace = criteria[:namespace] || criteria['namespace']
32
+ name = criteria[:name] || criteria['name']
33
+ if namespace && name && (algorithm = indexed[namespace][name])
34
+ return algorithm
35
+ end
36
+ algorithm = retrieve(criteria)
37
+ indexed[algorithm.namespace][algorithm.name] = cache[algorithm.id] = algorithm
38
+ algorithm
39
+ end
40
+
41
+ def reset
42
+ cache.clear
43
+ indexed.clear
44
+ self
45
+ end
46
+
47
+ private
48
+
49
+ def cache
50
+ @algorithms ||= {}
51
+ end
52
+
53
+ def indexed
54
+ @indexed ||= Hash.new { |h, k| h[k] = {} }
55
+ end
56
+
57
+ def retrieve(criteria)
58
+ query = criteria.dup
59
+ id = query.delete('id')
60
+ id = query.delete(:id) || id
61
+ headers = { 'Content-Type' => 'application/json' }
62
+ if (token = Cenit.access_token) && (key = Cenit.access_key)
63
+ headers['X-User-Access-Key'] = key
64
+ headers['X-User-Access-Token'] = token
65
+ end
66
+ url = "#{Cenit.host}/api/v2/setup/algorithm"
67
+ if id
68
+ url += "/#{id}"
69
+ query = {}
70
+ end
71
+ query[:embedding] = 'snippet'
72
+ response = HTTParty.get url, { headers: headers, query: query }
73
+ if response.code == 200
74
+ json = JSON.parse(response.body)
75
+ if id || json['count'] == 1
76
+ json = id ? json : json['algorithms'][0]
77
+ Algorithm.new(json)
78
+ else
79
+ fail "Algorithm not found with criteria #{criteria}"
80
+ end
81
+ else
82
+ fail response.to_json
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,59 @@
1
+ require 'cenit/algorithms/interpreter'
2
+
3
+ module Cenit
4
+ module Algorithms
5
+ class Algorithm
6
+
7
+ attr_reader :json
8
+
9
+ def initialize(json)
10
+ @json = json
11
+ end
12
+
13
+ def id
14
+ @id ||= json['id'] || object_id.to_s
15
+ end
16
+
17
+ def namespace
18
+ json['namespace']
19
+ end
20
+
21
+ def name
22
+ json['name']
23
+ end
24
+
25
+ def parameters
26
+ json['parameters'] || []
27
+ end
28
+
29
+ def code
30
+ json['snippet']['code']
31
+ end
32
+
33
+ def call_links
34
+ json['call_links'] || []
35
+ end
36
+
37
+ def link?(name)
38
+ name = name.to_s
39
+ call_links.any? { |call_link| call_link['name'] == name }
40
+ end
41
+
42
+ def link(name)
43
+ name = name.to_s
44
+ call_link = call_links.detect { |call_link| call_link['name'] == name }
45
+ if call_link
46
+ Algorithms.find(id: call_link['link']['id'],
47
+ namespace: call_link['link']['namespace'],
48
+ name: call_link['link']['name'])
49
+ else
50
+ fail "Error linking #{name}"
51
+ end
52
+ end
53
+
54
+ def run(*args)
55
+ Interpreter.run(self, *args)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,68 @@
1
+ require 'cenit/algorithms/rewriter'
2
+
3
+ module Cenit
4
+ module Algorithms
5
+ class Interpreter
6
+
7
+ def initialize
8
+ @__prefixes__ = Hash.new do |linkers, linker_id|
9
+ linkers[linker_id] = Hash.new do |methods, method|
10
+ methods[method] = "__#{linker_id}_"
11
+ end
12
+ end
13
+ @__algorithms__ = {}
14
+ @__options__ = {}
15
+ end
16
+
17
+ def method_missing(symbol, *args, &block)
18
+ if (algorithm = @__algorithms__[symbol.to_s])
19
+ if algorithm.is_a?(Proc)
20
+ define_singleton_method(symbol, algorithm)
21
+ else
22
+ instance_eval "define_singleton_method(:#{symbol},
23
+ ->(#{algorithm.parameters.collect { |p| p['name'] }.join(', ')}) {
24
+ #{__parse__(algorithm)}
25
+ })"
26
+ end
27
+ send(symbol, *args, &block)
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ def respond_to?(*args)
34
+ @__algorithms__.has_key?(args[0])
35
+ end
36
+
37
+ def __run__(algorithm, *args)
38
+ run_name ="__run__#{algorithm.name}"
39
+ @__algorithms__[run_name] = algorithm
40
+ send run_name, *args
41
+ end
42
+
43
+ def __prefix__(method, linker)
44
+ prefix = @__prefixes__[linker.id][method]
45
+ @__algorithms__[prefix + method.to_s] = linker.link(method)
46
+ prefix
47
+ end
48
+
49
+ def __parse__(algorithm)
50
+ code = algorithm.code
51
+ algorithm.parameters.each do |p|
52
+ code = "#{p['name']} ||= nil\r\n" + code
53
+ end
54
+ buffer = ::Parser::Source::Buffer.new('code')
55
+ buffer.source = code
56
+ ast = Parser::CurrentRuby.new.parse(buffer)
57
+ Rewriter.new(self_linker: algorithm, interpreter: self).rewrite(buffer, ast)
58
+ end
59
+
60
+ class << self
61
+ def run(algorithm, *args)
62
+ new.__run__(algorithm, *args)
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,18 @@
1
+ require 'cenit/algorithms/algorithm'
2
+
3
+ module Cenit
4
+ module Algorithms
5
+ class Namespace
6
+
7
+ attr_reader :name
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def method_missing(symbol, *args)
14
+ Algorithms.find(namespace: name, name: symbol.to_s).run(*args)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ require 'parser/current'
2
+
3
+ module Cenit
4
+ module Algorithms
5
+ class Rewriter < Parser::Rewriter
6
+
7
+ attr_reader :logs
8
+
9
+ def initialize(options = {})
10
+ @options = options || {}
11
+ @logs = options[:logs] || {}
12
+ @self_linker = options[:self_linker]
13
+ @interpreter = options[:interpreter]
14
+ end
15
+
16
+ def on_send(node)
17
+ super
18
+ if node.children[0].nil? && node.type == :send
19
+ method_name = node.children[1]
20
+ if @self_linker && !@self_linker.link?(method_name)
21
+ report_error("error linking #{method_name}")
22
+ end
23
+ (@logs[:self_sends] ||= Set.new) << method_name
24
+ if @interpreter
25
+ prefix = @interpreter.__prefix__(method_name, @self_linker)
26
+ insert_before(node.location.expression, prefix)
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def report_error(message)
34
+ if @options[:halt_on_error]
35
+ fail message
36
+ else
37
+ (logs[:errors] ||= []) << message
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module Cenit
2
+ module Algorithms
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cenit-algorithms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maikel Arcia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cenit-config
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.7
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.8.3
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 1.8.3
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.3
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.8.3
89
+ - !ruby/object:Gem::Dependency
90
+ name: parser
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 2.3.1.4
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 2.3.1.4
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: 2.3.1.4
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 2.3.1.4
109
+ description: Run algorithms retrieving their codes from Cenit.
110
+ email:
111
+ - macarci@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - CODE_OF_CONDUCT.md
117
+ - Gemfile
118
+ - LICENSE.txt
119
+ - README.md
120
+ - bin/console
121
+ - bin/setup
122
+ - cenit-algorithms.gemspec
123
+ - lib/cenit/algorithms.rb
124
+ - lib/cenit/algorithms/algorithm.rb
125
+ - lib/cenit/algorithms/interpreter.rb
126
+ - lib/cenit/algorithms/namespace.rb
127
+ - lib/cenit/algorithms/rewriter.rb
128
+ - lib/cenit/algorithms/version.rb
129
+ homepage: https://github.com/cenit-io/cenit-algorithms
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.4.6
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Runs Cenit algorithms.
153
+ test_files: []