gcf_ruby 0.1.0

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
+ SHA256:
3
+ metadata.gz: a7b31b3606c2a025a1fb06636cef551266eeed086ca8a73c2730fac6b239b3f0
4
+ data.tar.gz: 5d37ff4a6b129cf3332b2f57b68a1ac30518a7103d0aa1b4fc3d75b8d8298a6f
5
+ SHA512:
6
+ metadata.gz: d7ec83834c73014c10124848cac912b78d7b29a83a778064320bafe9d8fc08373fa0757b292f3df836fc911844057e6939d0602a9cdd09143700ef3f878a50ca
7
+ data.tar.gz: e3d5dc124795924244910ed961be382aa6ed92b8d99b6ff835ecc74d73296add19dde374d2205ebff56b74f8820c36182fe0915667cecb94fac96ff6d22033a9
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gcf_ruby.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gcf_ruby (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ crack (0.4.5)
12
+ rexml
13
+ hashdiff (1.0.1)
14
+ minitest (5.14.4)
15
+ public_suffix (4.0.6)
16
+ rake (12.3.3)
17
+ rexml (3.2.5)
18
+ vcr (6.0.0)
19
+ webmock (3.12.2)
20
+ addressable (>= 2.3.6)
21
+ crack (>= 0.3.2)
22
+ hashdiff (>= 0.4.0, < 2.0.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ gcf_ruby!
29
+ minitest (>= 5.14)
30
+ rake (~> 12.0)
31
+ vcr (~> 6.0)
32
+ webmock (~> 3.12)
33
+
34
+ BUNDLED WITH
35
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Flatlooker
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,42 @@
1
+ # GcfRuby : simple predictions with google cloud functions
2
+
3
+ This gem allows interaction within ruby code with various APIs hosted in google cloud functions. It is primarly designed to predict results of machine learning models and works synchronously (for light predictions) or asynchronously (for heavier predictions).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gcf_ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gcf_ruby
20
+
21
+ ## Usage
22
+
23
+ After installing the gem, complete the file in `config/initializers/gcf_ruby.rb` with required `api_url` (for webhooks) and `api_bearer` for authentication.
24
+
25
+ Two methods are available (as of 19/04/2021) :
26
+
27
+ - `predict(model_name, params)` which returns synchronously the result of the model with the corresponding model_name.
28
+ - `predict_async` which returns asynchronously the result of the model. It requires a webhook url (set in `api_url`) on which the response will be sent.
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Flatlooker/gcf_ruby.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gcf_ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/gcf_ruby.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/gcf_ruby/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'gcf_ruby'
7
+ spec.version = GcfRuby::VERSION
8
+ spec.authors = ['Nicolas Goyet']
9
+ spec.email = ['nicolas@flatlooker.com']
10
+
11
+ spec.summary = 'This gem allows interaction within ruby code with various apis hosted in google cloud functions. Full list of available APIs here : https://github.com/Flatlooker/google_cloud_functions.'
12
+ spec.homepage = 'https://github.com/Flatlooker/gcf_ruby'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+ spec.add_development_dependency 'minitest', '>= 5.14'
16
+ spec.add_development_dependency 'vcr', '~> 6.0'
17
+ spec.add_development_dependency 'webmock', '~> 3.12'
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/Flatlooker/gcf_ruby'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
data/lib/gcf_ruby.rb ADDED
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ruby bindings
4
+ require 'json'
5
+ require 'net/http'
6
+
7
+ # Resource and support classes
8
+ require 'gcf_ruby/version'
9
+ require 'gcf_ruby/errors'
10
+
11
+ # Main class for prediction
12
+ module GcfRuby
13
+ class << self
14
+ attr_accessor :configuration
15
+
16
+ def predict(model_name, params)
17
+ post(model_name, formatted_params(params))
18
+ end
19
+
20
+ def predict_async(model_name, params, webhook_url)
21
+ post(model_name, formatted_params(params), webhook_url)
22
+ end
23
+
24
+ private
25
+
26
+ def post(model_name, params, url = nil)
27
+ uri = URI.parse(GcfRuby.configuration.api_url)
28
+ query_params = { model: model_name }
29
+ query_params[:url] = url if url
30
+ uri.query = URI.encode_www_form(query_params)
31
+
32
+ req = Net::HTTP::Post.new(uri)
33
+ req['Authorization'] = "Bearer #{GcfRuby.configuration.api_bearer}"
34
+ req['Content-Type'] = 'application/json'
35
+
36
+ req.body = params.to_json
37
+ generate_https = Net::HTTP.new(uri.host, uri.port)
38
+ generate_https.use_ssl = true
39
+
40
+ res = generate_https.request(req)
41
+ response = JSON.parse(res.body)
42
+
43
+ case res.code
44
+ when '400'
45
+ raise FailedValidationError, "Wrong inputs #{params} in model #{model_name}"
46
+ when '403'
47
+ raise AuthentificationError, 'Authorization error'
48
+ when '404'
49
+ raise WrongModelName, "Wrong model name #{model_name}"
50
+ when '405'
51
+ raise AsyncError, "#{model_name} should be called in an asynchronous manner"
52
+ when '500'
53
+ raise InternalServerError, 'Internal server error in api'
54
+ when '200'
55
+ # it's sync
56
+ response
57
+ when '202'
58
+ # it's ansync
59
+ true
60
+ else
61
+ raise UnhandledError, 'Cannot process error code'
62
+ false
63
+ end
64
+ end
65
+
66
+ def formatted_params(params)
67
+ { instances: params }
68
+ end
69
+ end
70
+
71
+ def self.configure(&block)
72
+ self.configuration ||= Configuration.new
73
+ yield configuration
74
+ end
75
+
76
+ class Configuration
77
+ attr_accessor :api_url
78
+ attr_accessor :api_bearer
79
+
80
+ def initialize
81
+ @api_url = 'default option'
82
+ @api_bearer = 'default option'
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GcfRuby
4
+ # GcfError is the base error from which all other more specific errors
5
+ class GcfError < StandardError
6
+ attr_accessor :message
7
+
8
+ def initialize(message = nil)
9
+ @message = message
10
+ end
11
+ end
12
+
13
+ # in case of 400
14
+ class FailedValidationError < GcfError
15
+ end
16
+
17
+ # in case of 403
18
+ class AuthentificationError < GcfError
19
+ end
20
+
21
+ # in case of 404
22
+ class WrongModelName < GcfError
23
+ end
24
+
25
+ # in case of 405
26
+ class AsyncError < GcfError
27
+ end
28
+
29
+ # in case of 500
30
+ class InternalServerError < GcfError
31
+ end
32
+
33
+ # in case of other errors
34
+ class UnhandledError < GcfError
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module GcfRuby
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GcfRuby
4
+ module Generators
5
+ # Install Generator inherits from Rails::Generator::Base
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../../templates', __dir__)
8
+ desc 'Creates GcfRuby initializer for your application'
9
+
10
+ def copy_initializer
11
+ template 'gcf_initializer.rb', 'config/initializers/gcf_ruby.rb'
12
+
13
+ puts 'Install complete!'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ GcfRuby.configure do |config|
2
+ config.api_url = 'your_gcf_url'
3
+ config.api_bearer = 'your_gcf_bearer'
4
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gcf_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Goyet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: vcr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ description:
56
+ email:
57
+ - nicolas@flatlooker.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - gcf_ruby.gemspec
71
+ - lib/gcf_ruby.rb
72
+ - lib/gcf_ruby/errors.rb
73
+ - lib/gcf_ruby/version.rb
74
+ - lib/generators/gcf_ruby/install_generator.rb
75
+ - lib/generators/templates/gcf_ruby_initializer.rb
76
+ homepage: https://github.com/Flatlooker/gcf_ruby
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/Flatlooker/gcf_ruby
81
+ source_code_uri: https://github.com/Flatlooker/gcf_ruby
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.3.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.9
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: 'This gem allows interaction within ruby code with various apis hosted in
101
+ google cloud functions. Full list of available APIs here : https://github.com/Flatlooker/google_cloud_functions.'
102
+ test_files: []