faastruby-rpc 0.1.2

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
+ SHA256:
3
+ metadata.gz: a2abf85651d49e5de1fb2ab7f62cdad8b2a5033608f93e648a5dd1ccd5cbbf13
4
+ data.tar.gz: 906271fdcd72e2acd967bec7ff12a55d73285056754af0b73d64a300aa006c95
5
+ SHA512:
6
+ metadata.gz: c9cb7ef8be2bb3c0b90b7c8690d913f2d93b619beadea3b5e377ed06f3f97b62a6169cdcfb87b5b96ddd1615415a383fb18696dabc63752aea856a71742ff94d
7
+ data.tar.gz: 92ee278e10983377186ffebabae90579bf37913dfdca841af035b18660309956e53371b22a3033478f5cba99f7dedf88b1fc6b0851a9f6d4ee2bec59cbf61e63
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ before_install:
2
+ - gem update --system
3
+ - gem update bundler
4
+ rvm:
5
+ - 2.5.3
6
+ script: bundle exec rspec
7
+ # Send builds to container-based infrastructure
8
+ # http://docs.travis-ci.com/user/workers/container-based-infrastructure/
9
+ sudo: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## 0.1.2 - Dec 9 2018 - First release
4
+ - Disable SSLv2, v3 and compression when calling invoke.
5
+
6
+ ## 0.1.1 - Dec 9 2018 - First release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in faastruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ faastruby-rpc (0.1.0)
5
+ oj (~> 3.6)
6
+ rest-client (~> 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.5.2)
12
+ public_suffix (>= 2.0.2, < 4.0)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ domain_name (0.5.20180417)
17
+ unf (>= 0.0.5, < 1.0.0)
18
+ hashdiff (0.3.7)
19
+ http-cookie (1.0.3)
20
+ domain_name (~> 0.5)
21
+ mime-types (3.2.2)
22
+ mime-types-data (~> 3.2015)
23
+ mime-types-data (3.2018.0812)
24
+ netrc (0.11.0)
25
+ oj (3.7.4)
26
+ public_suffix (3.0.3)
27
+ rake (10.5.0)
28
+ rest-client (2.0.2)
29
+ http-cookie (>= 1.0.2, < 2.0)
30
+ mime-types (>= 1.16, < 4.0)
31
+ netrc (~> 0.8)
32
+ rspec (3.8.0)
33
+ rspec-core (~> 3.8.0)
34
+ rspec-expectations (~> 3.8.0)
35
+ rspec-mocks (~> 3.8.0)
36
+ rspec-core (3.8.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-expectations (3.8.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-mocks (3.8.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-support (3.8.0)
45
+ safe_yaml (1.0.4)
46
+ unf (0.1.4)
47
+ unf_ext
48
+ unf_ext (0.0.7.5)
49
+ webmock (3.4.2)
50
+ addressable (>= 2.3.6)
51
+ crack (>= 0.3.2)
52
+ hashdiff
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ bundler (~> 1.16)
59
+ faastruby-rpc!
60
+ rake (~> 10.0)
61
+ rspec (~> 3.8)
62
+ webmock (~> 3.4)
63
+
64
+ BUNDLED WITH
65
+ 1.16.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Paulo Arruda
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,63 @@
1
+ # faastruby-rpc
2
+
3
+ Wrapper to make it easy to call FaaStRuby functions.
4
+
5
+ #### What is FaaStRuby?
6
+ FaaStRuby is a serverless platform built for Ruby developers.
7
+
8
+ * [Tutorial](https://faastruby.io/tutorial.html)
9
+
10
+ ## Calling functions from within a function
11
+
12
+ To call a function, use the helper method `invoke`:
13
+
14
+ ```ruby
15
+ # You need the function path, which is WORKSPACE_NAME/FUNCTION_NAME
16
+ function = 'paulo/hello'
17
+ # Invoke the function the get the result
18
+ result = invoke(function).call
19
+ # Or passing arguments:
20
+ result = invoke(function).with('Paulo', likes_ruby: true)
21
+ ```
22
+
23
+ `result` is a Struct with the following attributes:
24
+ * result.body - The response body from the function you called
25
+ * result.code - The HTTP status code returned by the function
26
+ * result.headers - The headers returned by the functions
27
+
28
+ The arguments in `with` are passed as arguments to your function (after the `event`). You can capture them with positional arguments, keyword arguments or just a generic `*args` if you want to have the flexibility of sending a variable number of arguments.
29
+
30
+ Here is the source code of `paulo/hello`:
31
+
32
+ ```ruby
33
+ def handler event, name = nil
34
+ response = name ? "Hello, #{name}!" : 'Hello, there!'
35
+ render text: response
36
+ end
37
+ ```
38
+
39
+ When you call `invoke`, a request is sent with the following properties:
40
+ * method: POST
41
+ * header `Content-Type: application/json`
42
+ * header `Faastruby-Rpc: true`
43
+ * body: JSON array
44
+
45
+ `invoke` is just a helper to the following method:
46
+ ```ruby
47
+ # Calling a function that way defaults to method=GET
48
+ FaaStRuby::RPC::Function.new("FUNCTION_PATH").call(body: nil, query_params: {}, headers: {}, method: 'get')
49
+ ```
50
+
51
+ This gem is already required when you run your functions in FaaStRuby, or using `faastruby server`.
52
+
53
+ ## Handling errors
54
+
55
+ By default, an exception is raised if the invoked function HTTP status code is greater than 400. This is important to make your functions easier to debug, and you will always know what to expect from that function call.
56
+
57
+ To disable this behaviour, pass `raise_errors: false` to the `invoke` method, or to `FaaStRuby::RPC::Function.new`. Example:
58
+
59
+ ```ruby
60
+ invoke('paulo/hello', raise_errors: false).call
61
+ # or
62
+ FaaStRuby::RPC::Function.new("paulo/hello", raise_errors: false).call(body: nil)
63
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubyfaas"
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
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "faastruby-rpc/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "faastruby-rpc"
7
+ spec.version = FaaStRuby::RPC::VERSION
8
+ spec.authors = ["Paulo Arruda"]
9
+ spec.email = ["parrudaj@gmail.com"]
10
+ spec.required_ruby_version = '>= 2.5.0'
11
+ spec.summary = %q{FaaStRuby RPC - Wrapper to make it easy to call FaaStRuby functions.}
12
+ spec.homepage = "https://github.com/faastruby/faastruby-rpc"
13
+ spec.license = "MIT"
14
+ spec.add_runtime_dependency 'oj', '~> 3.6'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ # spec.bindir = "exe"
31
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.8"
37
+ spec.add_development_dependency "webmock", "~> 3.4"
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'net/http'
2
+ require 'oj'
3
+ require 'yaml'
4
+ require 'faastruby-rpc/version'
5
+ require 'faastruby-rpc/function'
6
+
7
+ (Net::HTTP::SSL_IVNAMES << :@ssl_options).uniq!
8
+ (Net::HTTP::SSL_ATTRIBUTES << :options).uniq!
9
+
10
+ Net::HTTP.class_eval do
11
+ attr_accessor :ssl_options
12
+ end
13
+
14
+ def invoke(function, raise_errors: true)
15
+ FaaStRuby::RPC::Function.new(function, raise_errors: raise_errors)
16
+ end
@@ -0,0 +1,88 @@
1
+ module FaaStRuby
2
+ FAASTRUBY_HOST = ENV['FAASTRUBY_HOST'] || "http://localhost:3000"
3
+ module RPC
4
+ class ExecutionError < StandardError
5
+ end
6
+ class Function
7
+ def initialize(path, raise_errors: true)
8
+ @path = path
9
+ @methods = {
10
+ 'post' => Net::HTTP::Post,
11
+ 'get' => Net::HTTP::Get,
12
+ 'put' => Net::HTTP::Put,
13
+ 'patch' => Net::HTTP::Patch,
14
+ 'delete' => Net::HTTP::Delete
15
+ }
16
+ @response = Struct.new(:body, :code, :headers, :klass)
17
+ @raise_errors = raise_errors
18
+ end
19
+ def with(*args)
20
+ call(body: Oj.dump(args), headers: {'Content-Type' => 'application/json', 'Faastruby-Rpc' => 'true'})
21
+ end
22
+
23
+ def call(body: nil, query_params: {}, headers: {}, method: 'post')
24
+ url = "#{FAASTRUBY_HOST}/#{@path}#{convert_query_params(query_params)}"
25
+ uri = URI.parse(url)
26
+ use_ssl = uri.scheme == 'https' ? true : false
27
+ response = fetch(use_ssl: use_ssl, uri: uri, headers: headers, method: @methods[method], body: body)
28
+ resp_headers = {}
29
+ response.each{|k,v| resp_headers[k] = v}
30
+ case resp_headers['content-type']
31
+ when 'application/json'
32
+ begin
33
+ resp_body = Oj.load(response.body)
34
+ rescue Oj::ParseError => e
35
+ if response.body.is_a?(String)
36
+ resp_body = response.body
37
+ else
38
+ raise e if @raise_errors
39
+ resp_body = {
40
+ 'error' => e.message,
41
+ 'location' => e.backtrace&.first
42
+ }
43
+ end
44
+ end
45
+ when 'application/yaml'
46
+ resp_body = YAML.load(response.body)
47
+ else
48
+ resp_body = response.body
49
+ end
50
+ raise FaaStRuby::RPC::ExecutionError.new("Function #{@path} returned status code #{response.code} - #{resp_body['error']} - #{resp_body['location']}") if response.code.to_i >= 400 && @raise_errors
51
+ @response.new(resp_body, response.code.to_i, resp_headers)
52
+ end
53
+
54
+ private
55
+ def convert_query_params(query_params)
56
+ return "" unless query_params.any?
57
+ "?#{URI.encode_www_form(query_params)}"
58
+ end
59
+
60
+ def fetch(use_ssl:, uri:, limit: 10, method: Net::HTTP::Post, headers: {}, body: nil)
61
+ # You should choose a better exception.
62
+ raise ArgumentError, 'too many HTTP redirects' if limit == 0
63
+ http = Net::HTTP.new(uri.host, uri.port)
64
+ if use_ssl
65
+ http.use_ssl = true
66
+ http.ssl_options = OpenSSL::SSL::OP_NO_SSLv2 + OpenSSL::SSL::OP_NO_SSLv3 + OpenSSL::SSL::OP_NO_COMPRESSION
67
+ end
68
+ request = method.new(uri.request_uri, headers)
69
+ request.body = body
70
+ response = http.request(request)
71
+
72
+ case response
73
+ when Net::HTTPSuccess then
74
+ response
75
+ when Net::HTTPRedirection then
76
+ location = URI.parse(response['location'])
77
+ warn "redirected to #{location}"
78
+ fetch(use_ssl: use_ssl, uri: location, limit: limit - 1, method: method, headers: headers, body: body)
79
+ else
80
+ response.value
81
+ end
82
+
83
+ rescue Net::HTTPServerException, Net::HTTPFatalError
84
+ return response
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ module FaaStRuby
2
+ module RPC
3
+ VERSION = '0.1.2'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faastruby-rpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Arruda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ description:
84
+ email:
85
+ - parrudaj@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - faastruby-rpc.gemspec
102
+ - lib/faastruby-rpc.rb
103
+ - lib/faastruby-rpc/function.rb
104
+ - lib/faastruby-rpc/version.rb
105
+ homepage: https://github.com/faastruby/faastruby-rpc
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.5.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.7
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: FaaStRuby RPC - Wrapper to make it easy to call FaaStRuby functions.
129
+ test_files: []