streamforce 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
+ SHA256:
3
+ metadata.gz: c34310d1629ebc82065f442b4e4ef30eb91fef7f28aa42b01afbc062b6fd3a1e
4
+ data.tar.gz: 53ae2b66be382330501458097c322c88a70826400af18808ae5f1b062966b6dd
5
+ SHA512:
6
+ metadata.gz: 4fb0fd547750fb0c26cc027407227bec6091e5a640431bbd26b31eac34866914aaa8fe9b2f0a99551b598aea6ae0f11de0f2ff54e85986fc2cd96e20c3e7a1af
7
+ data.tar.gz: 55b68bf69239eba0af1ceabfd225184c2ef8e838154273086614acbad4c685645f37bbb8ed6a60da04033f8cc965191a053a719a864815746ad06bfb868b30a3
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-06-12
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Andrei Maxim
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,9 @@
1
+ # Streamforce
2
+
3
+ ## Contributing
4
+
5
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andreimaxim/streamforce.
6
+
7
+ ## License
8
+
9
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,79 @@
1
+ class Streamforce::Client
2
+ class << self
3
+ def host
4
+ ENV["SALESFORCE_HOST"]
5
+ end
6
+
7
+ def client_id
8
+ ENV["SALESFORCE_CLIENT_ID"]
9
+ end
10
+
11
+ def client_secret
12
+ ENV["SALESFORCE_CLIENT_SECRET"]
13
+ end
14
+
15
+ def username
16
+ ENV["SALESFORCE_USERNAME"]
17
+ end
18
+
19
+ def password
20
+ ENV["SALESFORCE_PASSWORD"]
21
+ end
22
+
23
+ def security_token
24
+ ENV["SALESFORCE_SECURITY_TOKEN"]
25
+ end
26
+
27
+ def version
28
+ ENV.fetch("SALESFORCE_API_VERSION", "61.0")
29
+ end
30
+
31
+ def authentication_url
32
+ URI.parse("https://#{host}/services/oauth2/token")
33
+ end
34
+
35
+ def authentication_params
36
+ {
37
+ grant_type: "password",
38
+ client_id: client_id,
39
+ client_secret: client_secret,
40
+ username: username,
41
+ password: "#{password}#{security_token}"
42
+ }
43
+ end
44
+ end
45
+
46
+ def set_authentication_credentials!
47
+ response = Net::HTTP.post_form(Streamforce::Client.authentication_url, Streamforce::Client.authentication_params)
48
+ credentials = JSON.parse(response.body)
49
+
50
+ @access_token = credentials["access_token"]
51
+ @instance_url = "#{credentials["instance_url"]}/cometd/#{Streamforce::Client.version}"
52
+ end
53
+
54
+ def subscribe(channels = [], &blk)
55
+ channels = Array(channels)
56
+
57
+ set_authentication_credentials!
58
+
59
+ EM.run { subscribe_to_channels(faye, channels, &blk) }
60
+ end
61
+
62
+ def faye
63
+ @faye ||= Faye::Client.new(@instance_url).tap do |client|
64
+ client.set_header "Authorization", "OAuth #{@access_token}"
65
+
66
+ client.add_extension Streamforce::Extension::Replay.new
67
+ client.add_extension Streamforce::Extension::SubscriptionTracking.new
68
+ client.add_extension Streamforce::Extension::Logging.new
69
+ end
70
+ end
71
+
72
+ def subscribe_to_channels(client, channels, &blk)
73
+ return if channels.empty?
74
+
75
+ client
76
+ .subscribe(channels.shift, &blk)
77
+ .callback { subscribe_to_channels(client, channels, &blk)}
78
+ end
79
+ end
@@ -0,0 +1,18 @@
1
+ class Streamforce::Extension::Logging
2
+ def initialize(log_level = Logger::DEBUG)
3
+ @logger = Logger.new($stdout)
4
+ @logger.level = log_level
5
+ end
6
+
7
+ def incoming(message, callback)
8
+ @logger.debug "Receving message: #{message.inspect}"
9
+
10
+ callback.call(message)
11
+ end
12
+
13
+ def outgoing(message, callback)
14
+ @logger.debug "Sending message: #{message.inspect}"
15
+
16
+ callback.call(message)
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ class Streamforce::Extension::Replay
2
+ def initialize(log_level = Logger::INFO)
3
+ @logger = Logger.new($stdout)
4
+ @logger.level = log_level
5
+ end
6
+
7
+ def incoming(message, callback)
8
+ callback.call(message)
9
+ end
10
+
11
+ def outgoing(message, callback)
12
+ callback.call(message)
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ class Streamforce::Extension::SubscriptionTracking
2
+ def initialize(log_level = Logger::DEBUG)
3
+ @logger = Logger.new($stdout)
4
+ @logger.level = log_level
5
+
6
+ @subscriptions = []
7
+ end
8
+
9
+ def incoming(message, callback)
10
+ if subscription?(message)
11
+ log_subscription_status(message)
12
+ else
13
+ log_subscription_payload(message)
14
+ end
15
+
16
+ callback.call(message)
17
+ end
18
+
19
+ def outgoing(message, callback)
20
+ @logger.debug "Requested subscription for #{message["subscription"]}" if subscription?(message)
21
+
22
+ callback.call(message)
23
+ end
24
+
25
+ def log_subscription_status(message)
26
+ subscription = message["subscription"]
27
+
28
+ if message["successful"]
29
+ @subscriptions << subscription
30
+
31
+ @logger.info "Subscription for #{subscription} was successful"
32
+ else
33
+ @logger.warn "Subscription for #{subscription} failed: #{message["error"]}"
34
+ end
35
+ end
36
+
37
+ def log_subscription_payload(message)
38
+ channel = message["channel"]
39
+ payload = message["data"].to_json
40
+
41
+ type = case channel.split("/")[1]
42
+ when "topic"
43
+ "PushTopic"
44
+ when "event"
45
+ "PlatformEvent"
46
+ else
47
+ "Unknown"
48
+ end
49
+
50
+ name = channel.split("/")[2]
51
+
52
+ @logger.info "[#{type}][#{name}]: #{payload}" if @subscriptions.include?(channel)
53
+ end
54
+
55
+ def subscription?(message)
56
+ message["channel"] == "/meta/subscribe"
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Streamforce
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "streamforce/version"
4
+
5
+ require "uri"
6
+ require "net/http"
7
+ require "logger"
8
+ require "json"
9
+ require "base64"
10
+
11
+ require "faye"
12
+ require "relaxed_cookiejar"
13
+
14
+ require "zeitwerk"
15
+ loader = Zeitwerk::Loader.for_gem
16
+ loader.setup
17
+
18
+ module Streamforce
19
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamforce
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Maxim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base64
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
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faye
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: relaxed_cookiejar
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - andrei@andreimaxim.ro
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rubocop.yml"
77
+ - CHANGELOG.md
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/streamforce.rb
82
+ - lib/streamforce/client.rb
83
+ - lib/streamforce/extension/logging.rb
84
+ - lib/streamforce/extension/replay.rb
85
+ - lib/streamforce/extension/subscription_tracking.rb
86
+ - lib/streamforce/version.rb
87
+ homepage: https://github.com/andreimaxim/streamforce
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ homepage_uri: https://github.com/andreimaxim/streamforce
92
+ source_code_uri: https://github.com/andreimaxim/streamforce
93
+ changelog_uri: https://github.com/andreimaxim/streamforce/blob/main/CHANGELOG.md
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 3.3.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.5.9
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Small wrapper over the Salesforce Streaming API
113
+ test_files: []