qube_sync 0.1.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
+ SHA256:
3
+ metadata.gz: b30a43b194aa2be7793bca91ff0af376f35cd0a7a663bcac2cd1297aeddb4cdf
4
+ data.tar.gz: 6866965f28ddb4a2712b14b37db0230b6d21c5c75885f0bc9910cb5f620ffd4a
5
+ SHA512:
6
+ metadata.gz: 046c0dd373ee5c9addf3005be16f5279a33f193be25c47d5c45e922d854d7239c10ce12d2529fbc1b60e65fa8ee13d78b4c845450a4273f06fba8960ba406792
7
+ data.tar.gz: 524e5fb52176085ff61bbd02b79a6765ed53621f6e5b981fb9b26f9c640ff1e4789b7b3ff6511cfaf8fcfd5e6bde6969301ec56fab6bca28c312ba2fa2e04c18
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-01-31
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 qubeintegrations
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # QUBESync
2
+
3
+ The QUBESync API is very simple, so this gem is not necessary if you need to use older versions of Ruby or if you prefer to write your own API client. However, if you are using Ruby 2.6 or later and you want to save time, this gem can help you.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add qube_sync
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install qube_sync
14
+
15
+ ## Configuration
16
+
17
+ You'll need to set two evironment variables in your application to use this gem:
18
+
19
+ `QUBE_API_KEY` - This is your API key, which you can find in your QUBE Sync application's settings.
20
+ `QUBE_WEBHOOK_SECRET` - This is the secret key used to sign the webhook payloads. You can find this in your QUBE Sync application's settings.
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'qube_sync'
26
+
27
+ connection_id = QubeSync.create_connection # creates a connection in QUBE on behalf of your user
28
+ #=> "636d4750-0b07-45f6-a030-e3919c5741ff"
29
+
30
+ # You can also pass a block to create_connection as a success callback
31
+ QubeSync.create_connection do |connection_id|
32
+ QubeConnection.create!(qube_id: connection_id) # save the connection_id to your database
33
+ end
34
+
35
+ QubeSync.delete_connection("asdf-qwer-asdf-zxcv") # deletes the connection in QUBE
36
+ #=> true
37
+
38
+ QubeSync.get_connection(connection_id) # gets the connection in QUBE
39
+ #=> {"id"=>"636d4750-0b07-45f6-a030-e3919c5741ff"}
40
+
41
+ QubeSync.generate_password(connection_id)
42
+ #=> "password123"
43
+
44
+ QubeSync.get_qwc(connection_id)
45
+ # "<?xml version=\"1.0\"?>\n<QBWCXML>...</QBWCXML>\n"
46
+
47
+
48
+
49
+ request_xml = <<~XML
50
+ <?xml version="1.0"?>
51
+ <?qbxml version="16.0"?>
52
+ <QBXML>
53
+ <QBXMLMsgsRq onError="stopOnError">
54
+ <CustomerQueryRq requestID="1">
55
+ <MaxReturned>10</MaxReturned>
56
+ </CustomerQueryRq>
57
+ </QBXMLMsgsRq>
58
+ </QBXML>
59
+ XML
60
+
61
+ QubeSync.queue_request(connection_id, request_xml, webhook_url = "myapp.com/webhook")
62
+ #=> {"id"=>"d401228f-06d4-4981-95d8-bb735c0a2c76",
63
+ # "state"=>"waiting",
64
+ # "response_xml"=>nil,
65
+ # "webhook_url"=>"myapp.com/webhook",
66
+ # "request_xml"=>
67
+ # "<?xml version=\"1.0\"?><?qbxml version=\"16.0\"?><QBXML> ... </QBXMLMsgsRq></QBXML>"}}
68
+
69
+ request_id = _.fetch("id")
70
+
71
+ QubeSync.get_request(request_id)
72
+ #=> {"id"=>"d401228f-06d4-4981-95d8-bb735c0a2c76",
73
+ # "state"=>"webhook_succeeded",
74
+ # "response_xml"=>
75
+ # "<?xml version=\"1.0\" ?> <QBXML> <QBXMLMsgsRs> ... </QBXMLMsgsRs> </QBXML>",
76
+ # "webhook_url"=>"myapp.com/webhook",
77
+ # "request_xml"=>
78
+ # "<?xml version=\"1.0\"?><?qbxml version=\"16.0\"?><QBXML> ... </QBXMLMsgsRq></QBXML>"}}
79
+
80
+ QubeSync.get_requests(connection_id)
81
+ #=> [{"id"=>"d401228f-06d4-4981-95d8-bb735c0a2c76",
82
+ # "state"=>"webhook_succeeded",
83
+ # "response_xml"=>
84
+ # "<?xml version=\"1.0\" ?> <QBXML> <QBXMLMsgsRs> ... </QBXMLMsgsRs> </QBXML>",
85
+ # "webhook_url"=>"myapp.com/webhook",
86
+ # "request_xml"=>
87
+ # "<?xml version=\"1.0\"?><?qbxml version=\"16.0\"?><QBXML> ... </QBXMLMsgsRq></QBXML>"}]
88
+
89
+ QubeSync.delete_request(request_id)
90
+ #=> true
91
+
92
+ QubeSync.verify_and_build_webhook!(request.body.read, request.headers['X-Qube-Signature'])
93
+ #=> {
94
+ # "id"=>"dd8db40a-5169-477a-b9d5-f1a6e5cc96f9",
95
+ # "timestamp"=>1738620998,
96
+ # "response_xml"=>
97
+ # "<?xml version=\"1.0\" ?> <QBXML> <QBXMLMsgsRs> ... </QBXMLMsgsRs> </QBXML>"
98
+ # }
99
+
100
+ # The default max age for a webhook is 500ms (0.5 seconds). You can change this by passing a max_age option:
101
+ QubeSync.verify_and_build_webhook!(request.body.read, request.headers['X-Qube-Signature'], max_age: 1_000)
102
+ ```
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/qubeintegrations/qube_sync.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QubeSync
4
+ VERSION = "0.1.1"
5
+ end
data/lib/qube_sync.rb ADDED
@@ -0,0 +1,170 @@
1
+ require_relative "qube_sync/version"
2
+ require "faraday"
3
+
4
+ module QubeSync
5
+ class Error < StandardError; end
6
+ class StaleWebhookError < Error; end
7
+ class InvalidWebhookSignatureError < Error; end
8
+ class ConfigError < Error; end
9
+
10
+ module_function
11
+
12
+ def base_url
13
+ ENV.fetch("QUBE_URL", 'https://qubesync.com/api/v1')
14
+ end
15
+
16
+ def get(url, headers = {})
17
+ response = connection.get(url) do |request|
18
+ request.headers = default_headers.merge(headers)
19
+ end
20
+
21
+ case response.status
22
+ when 200..299
23
+ JSON.parse(response.body)
24
+ else
25
+ raise Error.new(<<~ERR
26
+ Unexpected QUBE response: #{response.status}\n#{response.body}
27
+
28
+ Request: #{url}
29
+ Headers: #{headers}
30
+ ERR
31
+ )
32
+ end
33
+ end
34
+
35
+ def connection
36
+ Faraday.new(url: base_url) do |conn|
37
+ conn.request :authorization, :basic, api_key, ''
38
+ conn.request :json
39
+ end
40
+ end
41
+
42
+ def post(url, body = nil, headers = {})
43
+ response = connection.post(url) do |request|
44
+ request.headers = default_headers.merge(headers)
45
+ request.body = body.to_json if body
46
+ end
47
+
48
+ case response.status
49
+ when 200..299
50
+ JSON.parse(response.body)
51
+ else
52
+ raise Error.new <<~ERR
53
+ Unexpected QUBE response: #{response.status}\n#{response.body}
54
+ ERR
55
+ end
56
+ end
57
+
58
+ def delete(url, headers = {})
59
+ response = connection.delete(url) do |request|
60
+ request.headers = default_headers.merge(headers)
61
+ end
62
+
63
+ case response.status
64
+ when 200..299
65
+ true
66
+ else
67
+ raise <<~ERR
68
+ Unexpected QUBE response: #{response.status}\n#{response.body}
69
+ ERR
70
+ end
71
+ end
72
+
73
+ def create_connection
74
+ response = post("connections")
75
+ connection_id = response.dig("data", "id") or raise "Connection ID not found: #{response.pretty_inspect}"
76
+ yield connection_id if block_given?
77
+ connection_id
78
+ end
79
+
80
+ def delete_connection(id)
81
+ delete("connections/#{id}")
82
+ end
83
+
84
+ def get_connection(connection_id)
85
+ get("connections/#{connection_id}").fetch("data")
86
+ end
87
+
88
+ def queue_request(connection_id, request_xml, webhook_url)
89
+ url = "connections/#{connection_id}/queued_requests"
90
+
91
+ payload = {
92
+ queued_request: {
93
+ request_xml: request_xml,
94
+ webhook_url: webhook_url,
95
+ }
96
+ }
97
+
98
+ post(url, payload).fetch("data")
99
+ end
100
+
101
+ def get_request(id)
102
+ get("queued_requests/#{id}").fetch("data")
103
+ end
104
+
105
+ def get_requests(connection_id)
106
+ get("connections/#{connection_id}/queued_requests").fetch("data")
107
+ end
108
+
109
+ def delete_request(id)
110
+ delete("queued_requests/#{id}")
111
+ end
112
+
113
+ def get_qwc(connection_id)
114
+ post("connections/#{connection_id}/qwc")
115
+ .fetch('qwc')
116
+ end
117
+
118
+ def generate_password(connection_id)
119
+ password = SecureRandom.hex(16)
120
+ response = post("connections/#{connection_id}/password", { password: password })
121
+ response.dig("data", "password") or raise "Password not found: #{response.pretty_inspect}"
122
+ end
123
+
124
+
125
+ def extract_signature_meta(header)
126
+ header.split(',') => [ts, *signatures]
127
+ {
128
+ timestamp: ts.split("=").last.to_i,
129
+ signatures: signatures
130
+ }
131
+ end
132
+
133
+ def default_headers
134
+ {
135
+ 'Content-Type' => 'application/json',
136
+ 'Accept' => 'application/json',
137
+ }
138
+ end
139
+
140
+ def api_key
141
+ ENV.fetch('QUBE_API_KEY')
142
+ rescue KeyError => e
143
+ raise ConfigError.new("QUBE_API_KEY not set in environment. Your API key can be found/copied from your application page on https://qubesync.com/.")
144
+ end
145
+
146
+ def api_secret
147
+ ENV.fetch('QUBE_WEBHOOK_SECRET')
148
+ rescue KeyError => e
149
+ raise ConfigError.new("QUBE_WEBHOOK_SECRET not set in environment. Your webhook secret can be found/copied from your application page on https://qubesync.com/.")
150
+ end
151
+
152
+ def sign_payload(payload)
153
+ OpenSSL::HMAC.hexdigest('sha256', api_secret, payload)
154
+ end
155
+
156
+ def verify_and_build_webhook!(body, signature, max_age: 500)
157
+ extract_signature_meta(signature) => { timestamp:, signatures:}
158
+
159
+ if timestamp < Time.now.to_i - max_age
160
+ raise StaleWebhookError.new('Timestamp more than #{max_age}ms old. To increase this, pass a different value for max_age.')
161
+ end
162
+
163
+ if signatures.detect { |sig| sign_payload(body) == sig }
164
+ JSON.parse(body)
165
+ else
166
+ raise InvalidWebhookSignatureError.new("Webhook signature mismatch")
167
+ end
168
+
169
+ end
170
+ end
data/qube_sync.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/qube_sync/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "qube_sync"
7
+ spec.version = QubeSync::VERSION
8
+ spec.authors = ["Garrett Lancaster"]
9
+ spec.email = ["support@qubesync.com"]
10
+
11
+ spec.summary = "Ruby library for the QUBE Sync API"
12
+ spec.description = """
13
+ Easily create and manage QUBE Sync API resources in Ruby.
14
+
15
+ Manage connections, queued requests, and more with the QUBE Sync API.
16
+ """
17
+ spec.homepage = "https://github.com/qubeintegrations/qube_sync_rb"
18
+ spec.required_ruby_version = ">= 2.6.0"
19
+
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/qubeintegrations/qube_sync_rb"
24
+ spec.metadata["changelog_uri"] = "https://github.com/qubeintegrations/qube_sync_rb/blob/main/CHANGELOG.md"
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(__dir__) do
29
+ `git ls-files -z`.split("\x0").reject do |f|
30
+ (File.expand_path(f) == __FILE__) ||
31
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
32
+ end
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ # Uncomment to register a new dependency of your gem
39
+ spec.add_dependency "faraday"
40
+
41
+
42
+ # For more information and examples about making a new gem, check out our
43
+ # guide at: https://bundler.io/guides/creating_gem.html
44
+ end
data/sig/qube_sync.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module QubeSync
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qube_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Garrett Lancaster
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: "\n Easily create and manage QUBE Sync API resources in Ruby.\n\n Manage
28
+ connections, queued requests, and more with the QUBE Sync API.\n "
29
+ email:
30
+ - support@qubesync.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - CHANGELOG.md
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/qube_sync.rb
41
+ - lib/qube_sync/version.rb
42
+ - qube_sync.gemspec
43
+ - sig/qube_sync.rbs
44
+ homepage: https://github.com/qubeintegrations/qube_sync_rb
45
+ licenses: []
46
+ metadata:
47
+ homepage_uri: https://github.com/qubeintegrations/qube_sync_rb
48
+ source_code_uri: https://github.com/qubeintegrations/qube_sync_rb
49
+ changelog_uri: https://github.com/qubeintegrations/qube_sync_rb/blob/main/CHANGELOG.md
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.6.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.5.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Ruby library for the QUBE Sync API
69
+ test_files: []