quber 0.0.5

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: dad5bc8f43648c045e4b99949bd77bd3c3473f639de55aec60f9de1db86e2c94
4
+ data.tar.gz: c10ceb5f6d13e63654c598dcc0d147e46ec595a34acf04bfe03b979135ebcb6d
5
+ SHA512:
6
+ metadata.gz: e4ad80705c6e76608ada0a9b423851a6a137c2e033e3413bf80d7e74797f17a09666f62fdee90735afff36d447ab13342e17d30e26cc534c4c78bf1c23b3c5ec
7
+ data.tar.gz: aeff1641ff99e59d2ebe03f569b790e3dbd6a565e22ee8df58a29be8fc624e4d039b38d33e86a7e0f890a484112a0150782e494cdebee61fbc23dfa97e143c45
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in quber.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ quber (0.0.5)
5
+ net-http-persistent (~> 3.1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ connection_pool (2.2.5)
11
+ minitest (5.14.4)
12
+ net-http-persistent (3.1.0)
13
+ connection_pool (~> 2.2)
14
+ rake (12.3.3)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ minitest (~> 5.0)
21
+ quber!
22
+ rake (~> 12.0)
23
+
24
+ BUNDLED WITH
25
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 creadone
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,72 @@
1
+ # Quber
2
+
3
+ Rails adapter for the [Qube](https://github.com/tnt-qube/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'quber'
11
+ ```
12
+
13
+ And then execute:
14
+ ```sh
15
+ $ bundle install
16
+ ```
17
+
18
+ Make available enviroment variables:
19
+
20
+ * `QUBER_API_URI` — Qube node URI (http://localhost:5672/api/v1/)
21
+ * `QUBER_API_TOKEN` — Qube node authentication token
22
+
23
+ Set in the Rails config Qube adapter:
24
+
25
+ ```ruby
26
+ # config/application.rb
27
+ config.active_job.queue_adapter = :quber
28
+ ```
29
+
30
+ Expose HTTP-endpoint to receive tasks back:
31
+
32
+ ```ruby
33
+ # config/routes.rb
34
+ # ...
35
+ mount Quber::Rack.new, at: '/_jobs'
36
+ # ...
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ Use as usual job:
42
+
43
+ ```ruby
44
+ $ bin/rails generate job welcome_message
45
+ ```
46
+
47
+ ```ruby
48
+ class WelcomeMessageJob < ApplicationJob
49
+ queue_as :default
50
+
51
+ def perform(user)
52
+ # Do something later
53
+ end
54
+ end
55
+ ```
56
+
57
+ Note: when you set `queue_as` as `:some_name` the Qube will create tube with `:some_name` and put the task into this tube.
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ 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).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/quber.
68
+
69
+
70
+ ## License
71
+
72
+ 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 "quber"
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,26 @@
1
+ module ActiveJob
2
+ module QueueAdapters
3
+ class QuberAdapter
4
+
5
+ def initialize options = {}
6
+ @client ||= Quber::Client.new options
7
+ end
8
+
9
+ def enqueue(job, attributes = {})
10
+ task = build_task(job, attributes)
11
+ @client.put task
12
+ end
13
+
14
+ def enqueue_at(job, timestamp)
15
+ enqueue job, scheduled_at: timestamp
16
+ end
17
+
18
+ private
19
+
20
+ def build_task(job, attributes)
21
+ { data: job.serialize, **attributes }
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ module Quber
2
+ class Client
3
+ def initialize options = {}
4
+ @http = HTTP.new options
5
+ end
6
+
7
+ def put options = {}
8
+ @http.post('jobs', options)
9
+ end
10
+ end
11
+ end
data/lib/quber/http.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'net/http/persistent'
5
+
6
+ module Quber
7
+ class HTTP
8
+ Response = Struct.new(:code, :body)
9
+
10
+ VERBS = {
11
+ get: Net::HTTP::Get,
12
+ post: Net::HTTP::Post,
13
+ put: Net::HTTP::Put,
14
+ delete: Net::HTTP::Delete
15
+ }
16
+
17
+ def initialize options = {}
18
+ @defaults = {
19
+ connection: Net::HTTP::Persistent.new,
20
+ api_uri: ENV['QUBER_API_URI'] || 'http://localhost:5672/api/v1/',
21
+ api_token: ENV['QUBER_API_TOKEN'] || '1234567890'
22
+ }.merge!(options)
23
+ end
24
+
25
+ def base_headers
26
+ {
27
+ 'X-Auth-Token' => @defaults[:api_token],
28
+ 'User-Agent' => "Quber/#{Quber::VERSION}",
29
+ 'Content-Type' => 'application/json'
30
+ }
31
+ end
32
+
33
+ def get(path, options = {})
34
+ execute(path, :get, options)
35
+ end
36
+
37
+ def post(path, options = {})
38
+ execute(path, :post, options)
39
+ end
40
+
41
+ def put(path, options = {})
42
+ execute(path, :put, options)
43
+ end
44
+
45
+ def delete(path, options = {})
46
+ execute(path, :delete, options)
47
+ end
48
+
49
+ private
50
+ def execute(path, method, options = {})
51
+ uri = URI.join(@defaults[:api_uri], path)
52
+ req = VERBS[method].new(uri.request_uri)
53
+
54
+ # Build headers and body
55
+ options.transform_keys!(&:to_s) unless options.empty?
56
+ headers = base_headers.merge(options.dig('headers') || options)
57
+ headers.each{ |k,v| req[k] = v }
58
+ req.body = (options.dig('body') || options || {}).to_json
59
+
60
+ # Send request and process response
61
+ resp = @defaults[:connection].request(uri.to_s, req)
62
+ body = resp.body.empty? ? {} : JSON.parse(resp.body)
63
+ Response.new(resp.code.to_i, body)
64
+ end
65
+ end
66
+ end
data/lib/quber/rack.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+ require 'rack'
3
+
4
+ module Quber
5
+ class Rack
6
+ class PayloadError < StandardError; end
7
+
8
+ def call(env)
9
+ request = ::Rack::Request.new(env)
10
+ payload = extract_payload(request)
11
+
12
+ ActiveJob::Base.execute payload
13
+
14
+ [200, {}, ['OK']]
15
+ rescue PayloadError => e
16
+ [400, {}, [e.cause.message]]
17
+ rescue => e
18
+ [500, {}, [e.message]]
19
+ end
20
+
21
+ private
22
+
23
+ def extract_payload(request)
24
+ JSON.parse(request.body.read).fetch('data')
25
+ rescue JSON::ParserError, KeyError
26
+ raise PayloadError
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Quber
2
+ VERSION = "0.0.5"
3
+ end
data/lib/quber.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'quber/http'
2
+ require 'quber/client'
3
+ require 'quber/rack'
4
+ require 'active_job/queue_adapters/quber_adapter'
data/quber.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/quber/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'quber'
5
+ spec.version = Quber::VERSION
6
+ spec.authors = ["creadone"]
7
+ spec.email = ["creadone@gmail.com"]
8
+
9
+ spec.summary = %q{Rails adapter for Qube}
10
+ spec.description = %q{Rails adapter for Qube}
11
+ spec.homepage = "https://github.com/roleus/quber"
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/tnt-qube/quber"
17
+
18
+ spec.add_runtime_dependency 'net-http-persistent', '~> 3.1.0'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - creadone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http-persistent
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ description: Rails adapter for Qube
28
+ email:
29
+ - creadone@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/active_job/queue_adapters/quber_adapter.rb
44
+ - lib/quber.rb
45
+ - lib/quber/client.rb
46
+ - lib/quber/http.rb
47
+ - lib/quber/rack.rb
48
+ - lib/quber/version.rb
49
+ - quber.gemspec
50
+ homepage: https://github.com/roleus/quber
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/roleus/quber
55
+ source_code_uri: https://github.com/tnt-qube/quber
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '2.7'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.4
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Rails adapter for Qube
75
+ test_files: []