circle_orbit 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.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Orbit Labs, Inc.
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,105 @@
1
+ # Circle Interactions to Orbit Workspace
2
+
3
+ ![Build Status](https://github.com/orbit-love/community-ruby-circle-orbit/workflows/CI/badge.svg)
4
+ [![Gem Version](https://badge.fury.io/rb/circle_orbit.svg)](https://badge.fury.io/rb/circle_orbit)
5
+ [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](.github/CODE_OF_CONDUCT.md)
6
+
7
+ Add your Circle interactions into your Orbit workspace with this community-built integration.
8
+
9
+ ![Screenshot of Activity in Orbit Workspace](readme-images/screenshot_of_activity.png)
10
+
11
+ |<p align="left">:sparkles:</p> This is a *community project*. The Orbit team does its best to maintain it and keep it up to date with any recent API changes.<br/><br/>We welcome community contributions to make sure that it stays current. <p align="right">:sparkles:</p>|
12
+ |-----------------------------------------|
13
+
14
+ ![There are three ways to use this integration. Install package - build and run your own applications. Run the CLI - run on-demand directly from your terminal. Schedule an automation with GitHub - get started in minutes - no coding required](readme-images/ways-to-use.png)
15
+
16
+ ## First Time Setup
17
+
18
+ To set up this integration you will need your Circle API key and your Circle community URL. See the below table for instructions on where to find it, along with your Orbit API credentials.
19
+ ## Application Credentials
20
+
21
+ The application requires the following environment variables:
22
+
23
+ | Variable | Description | More Info
24
+ |---|---|--|
25
+ | `CIRCLE_API_KEY` | Circle API key | Create a new API key in the API section in your User Settings
26
+ | `CIRCLE_URL` | Circle community URL | This is your Circle custom URL, for example: `https://orbit.circle.so`
27
+ | `ORBIT_API_KEY` | API key for Orbit | Found in `Account Settings` in your Orbit workspace
28
+ | `ORBIT_WORKSPACE_ID` | ID for your Orbit workspace | Last part of the Orbit workspace URL, i.e. `https://app.orbit.love/my-workspace`, the ID is `my-workspace`
29
+
30
+ ## Package Usage
31
+
32
+ Install the package with the following command
33
+
34
+ ```
35
+ $ gem install circle_orbit
36
+ ```
37
+
38
+ Then, run `bundle install` from your terminal.
39
+
40
+ You can instantiate a client by either passing in the required credentials during instantiation or by providing them in your `.env` file.
41
+
42
+ ### Instantiation with credentials:
43
+
44
+ ```ruby
45
+ client = CircleOrbit::Client.new(
46
+ orbit_api_key: YOUR_API_KEY,
47
+ orbit_workspace_id: YOUR_ORBIT_WORKSPACE_ID,
48
+ circle_api_key: YOUR_CIRCLE_API_KEY,
49
+ circle_url: YOUR_CIRCLE_URL
50
+ )
51
+ ```
52
+
53
+ ### Instantiation with credentials in dotenv file:
54
+
55
+ ```ruby
56
+ client = CircleOrbit::Client.new
57
+ ```
58
+ ### Post New Spaces Posts to Orbit Workspace
59
+
60
+ You can use the gem to get new posts in your Circle Spaces and share them as custom activities to your Orbit workspace by using the `#posts` method on your `client instance`:
61
+
62
+ ```ruby
63
+ client.posts
64
+ ```
65
+
66
+ ### Post New Spaces Comments to Orbit Workspace
67
+
68
+ You can use the gem to get new comments on posts in your Circle Spaces and share them as custom activities to your Orbit workspace by using the `#posts` method on your `client instance`:
69
+
70
+ ```ruby
71
+ client.comments
72
+ ```
73
+ ## CLI Usage
74
+
75
+ You can also use this package with the included CLI. To use the CLI pass in the required environment variables on the command line before invoking the CLI.
76
+
77
+ To check for new posts:
78
+
79
+ ```bash
80
+ $ ORBIT_API_KEY=... ORBIT_WORKSPACE_ID=... CIRCLE_API_KEY=... CIRCLE_URL=... bundle exec circle_orbit --check-posts
81
+ ```
82
+
83
+ To check for new comments on posts:
84
+
85
+ ```bash
86
+ $ ORBIT_API_KEY=... ORBIT_WORKSPACE_ID=... CIRCLE_API_KEY=... CIRCLE_URL=... bundle exec circle_orbit --check-comments
87
+ ```
88
+
89
+ ## GitHub Actions Automation Setup
90
+
91
+ ⚡ You can set up this integration in a matter of minutes using our GitHub Actions template. It will run regularly to add new activities to your Orbit workspace. All you need is a GitHub account.
92
+
93
+ [See our guide for setting up this automation](https://github.com/orbit-love/github-actions-templates/blob/main/Circle/README.md)
94
+
95
+ ## Contributing
96
+
97
+ We 💜 contributions from everyone! Check out the [Contributing Guidelines](.github/CONTRIBUTING.md) for more information.
98
+
99
+ ## License
100
+
101
+ This project is under the [MIT License](./LICENSE).
102
+
103
+ ## Code of Conduct
104
+
105
+ This project uses the [Contributor Code of Conduct](.github/CODE_OF_CONDUCT.md). We ask everyone to please adhere by its guidelines.
data/bin/circle_orbit ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ check_comments = false
5
+ check_posts = false
6
+
7
+ options = {}
8
+ choices = OptionParser.new do |opts|
9
+ opts.banner = "Usage: circle_orbit --check-comments"
10
+ opts.on("-h", "--help", "Prints help instructions") do
11
+ puts opts
12
+ exit
13
+ end
14
+ opts.on("--check-comments", "Check for new comments on your Circle Spaces posts") do
15
+ check_comments = true
16
+ end
17
+ opts.on("--check-posts", "Check for new posts in your Circle Spaces") do
18
+ check_posts = true
19
+ end
20
+ end.parse!
21
+
22
+ $LOAD_PATH.unshift(File.expand_path('../lib/circle_orbit', __dir__))
23
+
24
+ require_relative '../lib/circle_orbit'
25
+ require_relative '../scripts/check_comments'
26
+ require_relative '../scripts/check_posts'
27
+
28
+ if check_comments
29
+ puts "Checking for new Circle Spaces post comments within the past day and sending them to your Orbit workspace..."
30
+ ARGV[0] = 'render'
31
+ CircleOrbit::Scripts::CheckComments.start(ARGV)
32
+ end
33
+
34
+ if check_posts
35
+ puts "Checking for new Circle Spaces posts and sending them to your Orbit workspace..."
36
+ ARGV[0] = 'render'
37
+ CircleOrbit::Scripts::CheckPosts.start(ARGV)
38
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/circle_orbit/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "circle_orbit"
7
+ spec.version = CircleOrbit::VERSION
8
+ spec.authors = ["Orbit DevRel", "Ben Greenberg"]
9
+ spec.email = ["devrel@orbit.love"]
10
+
11
+ spec.summary = "Integrate Circle interactions into Orbit"
12
+ spec.description = "This gem brings in Circle activities to your Orbit workspace"
13
+ spec.homepage = "https://github.com/orbit-love/community-ruby-circle-orbit"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.2")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/orbit-love/community-ruby-circle-orbit/blob/main/CHANGELOG.md"
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+
25
+ spec.bindir = "bin"
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "actionview", "~> 6.1"
30
+ spec.add_dependency "activesupport", "~> 6.1"
31
+ spec.add_dependency "dotenv", "~> 2.7"
32
+ spec.add_dependency "http", "~> 4.4"
33
+ spec.add_dependency "json", "~> 2.5"
34
+ spec.add_dependency "orbit_activities", "~> 0.1"
35
+ spec.add_dependency "thor", "~> 1.1"
36
+ spec.add_dependency "zeitwerk", "~> 2.4"
37
+ spec.add_development_dependency "rspec", "~> 3.4"
38
+ spec.add_development_dependency "webmock", "~> 3.12"
39
+ end
data/docs/setup.md ADDED
@@ -0,0 +1,11 @@
1
+ <small><a href="../README.md">Back to README</a></small>
2
+
3
+ # First Time Setup
4
+
5
+ <!-- This is a summary section. Explain what we need to do at a high-level. For example... -->
6
+
7
+ During the setup, we will need to create a new application on Stack Apps which will allow this integration to interact with the platform. The application will contain a Key which we should take note of for later.
8
+
9
+ 1. Head to [Stack Apps](https://stackapps.com) and login in to your account.
10
+ 2. [Register a new Stack Apps application](https://stackapps.com/apps/oauth/register). The oauth domain can be anything, as can the application website.
11
+ 3. In your application settings, take note of your `Key` value.
Binary file
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ require "orbit_activities"
5
+ require_relative "circle_orbit/version"
6
+
7
+ module CircleOrbit
8
+ loader = Zeitwerk::Loader.new
9
+ loader.tag = File.basename(__FILE__, ".rb")
10
+ loader.push_dir(__dir__)
11
+ loader.setup
12
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CircleOrbit
4
+ class Circle
5
+ def initialize(params = {})
6
+ @circle_api_key = params.fetch(:circle_api_key)
7
+ @circle_community_id = params.fetch(:circle_community_id)
8
+ @circle_url = params.fetch(:circle_url)
9
+ @orbit_api_key = params.fetch(:orbit_api_key)
10
+ @orbit_workspace = params.fetch(:orbit_workspace)
11
+ end
12
+
13
+ def process_posts
14
+ spaces = get_spaces
15
+
16
+ spaces.each do |space|
17
+ posts = get_posts(space["id"])
18
+
19
+ posts.each do |post|
20
+ CircleOrbit::Orbit.call(
21
+ type: "post",
22
+ data: post,
23
+ orbit_api_key: @orbit_api_key,
24
+ orbit_workspace: @orbit_workspace
25
+ )
26
+ end
27
+ end
28
+ end
29
+
30
+ def process_comments
31
+ comments = get_comments
32
+
33
+ return if comments.nil? || comments.empty?
34
+
35
+ comments.each do |comment|
36
+ CircleOrbit::Orbit.call(
37
+ type: "comment",
38
+ data: comment,
39
+ orbit_api_key: @orbit_api_key,
40
+ orbit_workspace: @orbit_workspace
41
+ )
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def get_spaces
48
+ url = URI("#{@circle_url}/api/v1/spaces?community_id=#{@circle_community_id}")
49
+
50
+ https = Net::HTTP.new(url.host, url.port)
51
+ https.use_ssl = true
52
+
53
+ request = Net::HTTP::Get.new(url)
54
+ request["Authorization"] = "Token #{@circle_api_key}"
55
+
56
+ response = https.request(request)
57
+
58
+ response = JSON.parse(response.body)
59
+ end
60
+
61
+ def get_posts(id)
62
+ url = URI("#{@circle_url}/api/v1/posts?community_id=#{@circle_community_id}&space_id=#{id}")
63
+
64
+ https = Net::HTTP.new(url.host, url.port)
65
+ https.use_ssl = true
66
+
67
+ request = Net::HTTP::Get.new(url)
68
+ request["Authorization"] = "Token #{@circle_api_key}"
69
+
70
+ response = https.request(request)
71
+
72
+ response = JSON.parse(response.body)
73
+ end
74
+
75
+ def get_comments
76
+ url = URI("#{@circle_url}/api/v1/comments?community_id=#{@circle_community_id}")
77
+
78
+ https = Net::HTTP.new(url.host, url.port)
79
+ https.use_ssl = true
80
+
81
+ request = Net::HTTP::Get.new(url)
82
+ request["Authorization"] = "Token #{@circle_api_key}"
83
+
84
+ response = https.request(request)
85
+
86
+ response = JSON.parse(response.body)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dotenv/load"
4
+ require "net/http"
5
+ require "json"
6
+
7
+ # Create a client to log Circle activities in your Orbit workspace
8
+ # Credentials can either be passed in to the instance or be loaded
9
+ # from environment variables
10
+ #
11
+ # @example
12
+ # client = CircleOrbit::Client.new
13
+ #
14
+ # @option params [String] :orbit_api_key
15
+ # The API key for the Orbit API
16
+ #
17
+ # @option params [String] :orbit_workspace
18
+ # The workspace ID for the Orbit workspace
19
+ #
20
+ # @option params [String] :circle_api_key
21
+ # The API key for the Circle API
22
+ #
23
+ # @option params [String] :circle_url
24
+ # The URL of the Circle community
25
+ #
26
+ # @param [Hash] params
27
+ #
28
+ # @return [CircleOrbit::Client]
29
+ #
30
+ module CircleOrbit
31
+ class Client
32
+ attr_accessor :orbit_api_key, :orbit_workspace, :circle_api_key, :circle_url, :circle_community_id
33
+
34
+ def initialize(params = {})
35
+ @orbit_api_key = params.fetch(:orbit_api_key, ENV["ORBIT_API_KEY"])
36
+ @orbit_workspace = params.fetch(:orbit_workspace, ENV["ORBIT_WORKSPACE_ID"])
37
+ @circle_api_key = params.fetch(:circle_api_key, ENV["CIRCLE_API_KEY"])
38
+ @circle_url = sanitize_url(params.fetch(:circle_url, ENV["CIRCLE_URL"]))
39
+ @circle_community_id = circle_community_id
40
+ end
41
+
42
+ def posts
43
+ CircleOrbit::Circle.new(
44
+ circle_api_key: @circle_api_key,
45
+ circle_url: @circle_url,
46
+ circle_community_id: @circle_community_id,
47
+ orbit_api_key: @orbit_api_key,
48
+ orbit_workspace: @orbit_workspace
49
+ ).process_posts
50
+ end
51
+
52
+ def comments
53
+ CircleOrbit::Circle.new(
54
+ circle_api_key: @circle_api_key,
55
+ circle_url: @circle_url,
56
+ circle_community_id: @circle_community_id,
57
+ orbit_api_key: @orbit_api_key,
58
+ orbit_workspace: @orbit_workspace
59
+ ).process_comments
60
+ end
61
+
62
+ private
63
+
64
+ def sanitize_url(url)
65
+ return url.delete_suffix("/") if url[-1, 1] == "/"
66
+
67
+ url
68
+ end
69
+
70
+ def circle_community_id
71
+ @circle_community_id ||= begin
72
+ return ENV["CIRCLE_COMMUNITY_ID"] if ENV["CIRCLE_COMMUNITY_ID"]
73
+
74
+ url = URI("#{@circle_url}/api/v1/communities")
75
+
76
+ https = Net::HTTP.new(url.host, url.port)
77
+ https.use_ssl = true
78
+
79
+ request = Net::HTTP::Get.new(url)
80
+ request["Authorization"] = "Token #{@circle_api_key}"
81
+
82
+ response = https.request(request)
83
+
84
+ response = JSON.parse(response.body)
85
+
86
+ response[0]["id"]
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "action_view"
5
+
6
+ module CircleOrbit
7
+ module Interactions
8
+ class Comment
9
+ def initialize(post_title:, body:, created_at:, id:, space:, url:, author:, email:, workspace_id:, api_key:)
10
+ @post_title = post_title
11
+ @body = sanitize_body(body)
12
+ @created_at = created_at
13
+ @id = id
14
+ @space = space
15
+ @url = url
16
+ @author = author
17
+ @email = email
18
+ @workspace_id = workspace_id
19
+ @api_key = api_key
20
+
21
+ after_initialize!
22
+ end
23
+
24
+ def after_initialize!
25
+ OrbitActivities::Request.new(
26
+ api_key: @api_key,
27
+ workspace_id: @workspace_id,
28
+ user_agent: "community-ruby-circle-orbit/#{CircleOrbit::VERSION}",
29
+ action: "new_activity",
30
+ body: construct_body.to_json
31
+ )
32
+ end
33
+
34
+ def construct_body
35
+ {
36
+ activity: {
37
+ activity_type: "circle:comment",
38
+ tags: ["channel:circle"],
39
+ key: "circle-comment-#{@id}",
40
+ title: "New comment in the #{@space} Space in Circle",
41
+ description: construct_description(@post_title, @body),
42
+ occurred_at: @created_at,
43
+ link: @url,
44
+ member: {
45
+ name: @author,
46
+ email: @email
47
+ }
48
+ },
49
+ identity: {
50
+ source: "circle",
51
+ email: @email
52
+ }
53
+ }
54
+ end
55
+
56
+ def construct_description(title, body)
57
+ return body if title == "" || title.nil?
58
+
59
+ <<~HEREDOC
60
+ ### Comment on Post: *#{title}*
61
+
62
+ #{body}
63
+ HEREDOC
64
+ end
65
+
66
+ private
67
+
68
+ def sanitize_body(body)
69
+ body = ActionView::Base.full_sanitizer.sanitize(body)
70
+
71
+ body.gsub("\n", " ")
72
+ end
73
+ end
74
+ end
75
+ end