smee 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/smee.rb +82 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d4738f5a8e0252f02960b7f0aa6b4fc45f2290be33b109c76b7e0c40e653d8f0
4
+ data.tar.gz: 71b7192aac54fbfc1487ff0360fbf36db54fa69934ec84f0b81b9213c0ce1950
5
+ SHA512:
6
+ metadata.gz: 73bbd7bfc79ad9aa10474337cc4bd9cff3df1159f30bf56073eda96321fe4e75d795725d169265c3178f31dc8791e75eb375153d1bee8bd93be39b5c7dd33cfa
7
+ data.tar.gz: 68610992315a9722b3ba83a0160f7c6487d181e249d37aca14a87da0b14373c674bf453237e1d4fa7f77b8110904804b0d1f184bd0c4d828e3e366bbc83e3a01
data/lib/smee.rb ADDED
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'ld-eventsource'
5
+ require 'httparty'
6
+
7
+ # The main Smee client class
8
+ class SmeeClient
9
+ def self.create_channel
10
+ response = HTTParty.head('https://smee.io/new', follow_redirects: false)
11
+ response.headers['location']
12
+ end
13
+
14
+ def initialize(source:, target:)
15
+ @source = source
16
+ @target = URI.parse(target)
17
+ end
18
+
19
+ def start
20
+ puts "[SmeeClient]: Listening for events at #{@source}"
21
+ subscribe
22
+ end
23
+
24
+ def close
25
+ puts '[SmeeClient]: Closing connection'
26
+ @events.close
27
+ end
28
+
29
+ private
30
+
31
+ def subscribe
32
+ @events = SSE::Client.new(@source) do |client|
33
+ client.on_event do |event|
34
+ handle_event event
35
+ end
36
+
37
+ client.on_error do |error|
38
+ puts "Error: #{error}"
39
+ end
40
+ end
41
+ end
42
+
43
+ def handle_event(event)
44
+ json = JSON.parse event.data
45
+ return unless json.key?('body')
46
+
47
+ body = json['body']
48
+ query = json['query']
49
+
50
+ headers = copy_headers(json)
51
+
52
+ post(body, query, headers)
53
+ end
54
+
55
+ def copy_headers(json)
56
+ headers = {
57
+ 'Content-Type': 'application/json'
58
+ }
59
+
60
+ # Don't want to include these as headers
61
+ json.delete 'body'
62
+ json.delete 'query'
63
+
64
+ json.each do |key, value|
65
+ # We need to ensure that headers are strings
66
+ headers[key] = value.to_s
67
+ end
68
+
69
+ headers
70
+ end
71
+
72
+ def post(body, query, headers)
73
+ # Send an HTTP request
74
+ response = HTTParty.post(@target, {
75
+ query: query,
76
+ body: body.to_json,
77
+ headers: headers
78
+ })
79
+
80
+ puts "[SmeeClient]: Pushed event, response: #{response.body}"
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Etcovitch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A programmatic client for Smee. Used to listen for events from Smee.io.
14
+ email: jasonetco@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/smee.rb
20
+ homepage: https://rubygems.org/gems/smee
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.7.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A programmatic client for Smee.
44
+ test_files: []