causeway 0.0.1 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0c1a608aa0ccb33e5a87613f8b579ccc7f267241f63a3be0b2ba003f886f495
4
- data.tar.gz: 7eca831aa60c63e743e031e7a9b428d73627c67210d9c9862498d0106a3df5ca
3
+ metadata.gz: 564e4b6b42c417e11b7cf838c10b4d90c75bb091b8623f3aeef1b32f1e3852a4
4
+ data.tar.gz: f1df7a7bf3554f6ec3870b7ff1e67883c5de324c3841a4ece1b16edddef9624d
5
5
  SHA512:
6
- metadata.gz: ee4af941f3a3b55171b47bab9c0643d02a4a78d7ba4f307a63ff9b493c1915973c908f44b4b94c87b2c5525f8062555bddd14e32b03f3dea1824a28f2fd404a2
7
- data.tar.gz: d31cf7ac701b0653e268108aac6e6a01d0775912379c8b7952326eb72398bff4ccc69a45460b596211358f66f26d019114b78a4dd86c983d1bf9f5f8a24db70b
6
+ metadata.gz: 710ca15aed6d65e9120c33956290f2ebaa502b66f4cd9c1f4d4864c6d1cd10b25574c99d4c710545d3805e15cd1ebf0011eca60db411de7077e4f8615ca46a6d
7
+ data.tar.gz: 0ad81632b38f10df5aaf9431c0d34ceb99146b0f7206aa1a94c71cd596aff92db9dbb0e11cf433b1ca9f1957eb520475aafa02f219149928a189a0a90a505375
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2026-08-24
3
+ ## [0.1.0] - 2026-12-02
4
+
5
+ - Add the LinkedIn publisher, posting to an organization feed via `linkedin_token`,
6
+ `linkedin_organization_id`, and `linkedin_api_version` (defaults to `202608`).
7
+ Set `linkedin_person_id` to post to a personal account instead.
8
+
9
+ ## [0.0.1] - 2026-08-24
4
10
 
5
11
  - Initial release
data/README.md CHANGED
@@ -16,7 +16,94 @@ gem install causeway
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: Write usage instructions here
19
+ Configure the services you want to post to:
20
+
21
+ ```ruby
22
+ Causeway.configure do |config|
23
+ config.bluesky_handle = "you.bsky.social"
24
+ config.bluesky_app_password = ENV["BLUESKY_APP_PASSWORD"]
25
+
26
+ config.mastodon_token = ENV["MASTODON_TOKEN"]
27
+ config.mastodon_instance = "https://mastodon.social" # the default
28
+
29
+ config.linkedin_token = ENV["LINKEDIN_TOKEN"]
30
+ config.linkedin_person_id = ENV["LINKEDIN_PERSON_ID"] # or linkedin_organization_id
31
+ end
32
+ ```
33
+
34
+ Any setting can also be a callable, which is called each time the value is read. This is useful for secrets that change, like
35
+ expiring tokens:
36
+
37
+ ```ruby
38
+ config.linkedin_token = -> { MySecretStore.fetch("linkedin_token") }
39
+ ```
40
+
41
+ Then post:
42
+
43
+ ```ruby
44
+ result = Causeway::Publishers::Mastodon.new.post("Hello, world!")
45
+ # => { id: "123", url: "https://mastodon.social/@you/123" }
46
+ # or { error: "..." } on failure
47
+ ```
48
+
49
+ The publishers are `Causeway::Publishers::Bluesky`, `Causeway::Publishers::Mastodon`, and `Causeway::Publishers::LinkedIn`.
50
+ They never raise on API errors; check the result for an `:error` key.
51
+
52
+ ### Getting credentials
53
+
54
+ #### Bluesky
55
+
56
+ 1. In Bluesky, go to **Settings → Privacy and security → App passwords**.
57
+ 2. Click **Add App Password**, give it a name, and copy the password.
58
+ 3. Set `bluesky_handle` to your handle (e.g. `you.bsky.social`) and `bluesky_app_password` to the app password.
59
+
60
+ #### Mastodon
61
+
62
+ 1. On your instance, go to **Preferences → Development → New application**.
63
+ 2. Give it a name and make sure the `write:statuses` scope is selected, then submit.
64
+ 3. Open the application and copy **Your access token**.
65
+ 4. Set `mastodon_token` to the token, and `mastodon_instance` to your instance's URL if it isn't `https://mastodon.social`.
66
+
67
+ #### LinkedIn
68
+
69
+ LinkedIn can post to a personal profile or to an organization (company page). Set `linkedin_person_id` for a personal
70
+ profile or `linkedin_organization_id` for a company page. If both are set, the post goes to the personal profile.
71
+
72
+ LinkedIn access tokens expire after about 60 days, after which you'll get a "token expired or invalid" error and need
73
+ to generate a new one.
74
+
75
+ **Personal profile**
76
+
77
+ 1. Create an app at <https://www.linkedin.com/developers/apps>. It must be associated with a company page, but the
78
+ page isn't used for personal posts.
79
+ 2. In the app's **Products** tab, add **Share on LinkedIn** and **Sign In with LinkedIn using OpenID Connect**. Both
80
+ are self-serve.
81
+ 3. Go to the [OAuth token generator](https://www.linkedin.com/developers/tools/oauth/token-generator), select your
82
+ app, tick the `openid`, `profile`, and `w_member_social` scopes, and request a token. Copy it; LinkedIn won't show
83
+ it again.
84
+ 4. Get your person ID (the `sub` field):
85
+
86
+ ```bash
87
+ curl -s https://api.linkedin.com/v2/userinfo -H "Authorization: Bearer $LINKEDIN_TOKEN" | jq -r .sub
88
+ ```
89
+
90
+ 5. Set `linkedin_token` to the token and `linkedin_person_id` to the ID.
91
+
92
+ **Organization (company page)**
93
+
94
+ 1. Create an app at <https://www.linkedin.com/developers/apps>, associated with your company page. A page admin must
95
+ verify the association.
96
+ 2. In the app's **Products** tab, request the **Community Management API**. LinkedIn reviews these requests, which
97
+ can take a while, and requires it to be the only product on the app, so use a separate app from the one for
98
+ personal posts.
99
+ 3. Once approved, use the [OAuth token generator](https://www.linkedin.com/developers/tools/oauth/token-generator)
100
+ to request a token with the `w_organization_social` scope, signed in as an admin of the company page.
101
+ 4. Get the organization ID from your company page's admin URL: in
102
+ `https://www.linkedin.com/company/12345678/admin/`, the ID is `12345678`.
103
+ 5. Set `linkedin_token` to the token and `linkedin_organization_id` to the ID.
104
+
105
+ `linkedin_api_version` sets the `LinkedIn-Version` header and defaults to `202608`. LinkedIn retires old versions
106
+ periodically, so you may need to update it.
20
107
 
21
108
  ## Development
22
109
 
@@ -1,17 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Cross-posts text to social networks.
3
4
  module Causeway
5
+ # Credentials and settings for each publisher. Values may be callables, resolved on read.
4
6
  class Configuration
5
- attr_writer :bluesky_handle, :bluesky_app_password, :mastodon_token, :mastodon_instance
7
+ attr_writer :bluesky_handle, :bluesky_app_password, :mastodon_token, :mastodon_instance,
8
+ :linkedin_token, :linkedin_organization_id, :linkedin_person_id, :linkedin_api_version
6
9
 
7
10
  def initialize
8
11
  @mastodon_instance = "https://mastodon.social"
12
+ @linkedin_api_version = "202608"
9
13
  end
10
14
 
11
15
  def bluesky_handle = resolve(@bluesky_handle)
12
16
  def bluesky_app_password = resolve(@bluesky_app_password)
13
17
  def mastodon_token = resolve(@mastodon_token)
14
18
  def mastodon_instance = resolve(@mastodon_instance)
19
+ def linkedin_token = resolve(@linkedin_token)
20
+ def linkedin_organization_id = resolve(@linkedin_organization_id)
21
+ def linkedin_person_id = resolve(@linkedin_person_id)
22
+ def linkedin_api_version = resolve(@linkedin_api_version)
15
23
 
16
24
  private
17
25
 
@@ -1,7 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "net/http"
4
+ require "json"
5
+ require "uri"
6
+
3
7
  module Causeway
4
8
  module Publishers
9
+ # Interface and shared HTTP helpers for all publishers.
5
10
  class Base
6
11
  def name
7
12
  raise NotImplementedError
@@ -11,6 +16,38 @@ module Causeway
11
16
  def post(text)
12
17
  raise NotImplementedError
13
18
  end
19
+
20
+ private
21
+
22
+ def config
23
+ Causeway.configuration
24
+ end
25
+
26
+ def present?(value)
27
+ !value.nil? && !value.empty?
28
+ end
29
+
30
+ def success?(response)
31
+ response.is_a?(Net::HTTPSuccess)
32
+ end
33
+
34
+ def api_error(response)
35
+ { error: "#{name} API error: #{response.code} #{response.body}" }
36
+ end
37
+
38
+ def post_json(uri, body, headers = {})
39
+ request = Net::HTTP::Post.new(uri)
40
+ request["Content-Type"] = "application/json"
41
+ headers.each { |key, value| request[key] = value }
42
+ request.body = JSON.generate(body)
43
+ perform(uri, request)
44
+ end
45
+
46
+ def perform(uri, request)
47
+ http = Net::HTTP.new(uri.host, uri.port)
48
+ http.use_ssl = uri.scheme == "https"
49
+ http.request(request)
50
+ end
14
51
  end
15
52
  end
16
53
  end
@@ -1,11 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "json"
5
- require "uri"
6
-
7
3
  module Causeway
8
4
  module Publishers
5
+ # Posts to Bluesky using an app password, adding link and hashtag facets.
9
6
  class Bluesky < Base
10
7
  API = "https://bsky.social"
11
8
 
@@ -14,96 +11,79 @@ module Causeway
14
11
  end
15
12
 
16
13
  def post(text)
17
- handle = Causeway.configuration.bluesky_handle
18
- app_password = Causeway.configuration.bluesky_app_password
14
+ handle = config.bluesky_handle
15
+ app_password = config.bluesky_app_password
19
16
 
20
- return { error: "Bluesky handle not configured." } unless handle && !handle.empty?
21
- return { error: "Bluesky app password not configured." } unless app_password && !app_password.empty?
17
+ return { error: "Bluesky handle not configured." } unless present?(handle)
18
+ return { error: "Bluesky app password not configured." } unless present?(app_password)
22
19
 
23
20
  session = create_session(handle, app_password)
24
21
  return session if session[:error]
25
22
 
26
- facets = detect_facets(text)
23
+ create_post(text, handle, session)
24
+ end
27
25
 
28
- record = {
29
- "$type" => "app.bsky.feed.post",
30
- "text" => text,
31
- "createdAt" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ")
32
- }
33
- record["facets"] = facets unless facets.empty?
26
+ private
34
27
 
35
- body = {
36
- "repo" => session[:did],
37
- "collection" => "app.bsky.feed.post",
38
- "record" => record
39
- }
28
+ def create_session(handle, app_password)
29
+ uri = URI("#{API}/xrpc/com.atproto.server.createSession")
30
+ response = post_json(uri, { "identifier" => handle, "password" => app_password })
31
+
32
+ return { error: "Bluesky auth failed: #{response.code} #{response.body}" } unless success?(response)
33
+
34
+ data = JSON.parse(response.body)
35
+ { did: data["did"], access_jwt: data["accessJwt"] }
36
+ end
40
37
 
38
+ def create_post(text, handle, session)
41
39
  uri = URI("#{API}/xrpc/com.atproto.repo.createRecord")
42
- response = https_post_json(uri, body, auth: session[:access_jwt])
40
+ body = { "repo" => session[:did], "collection" => "app.bsky.feed.post", "record" => build_record(text) }
41
+ response = post_json(uri, body, "Authorization" => "Bearer #{session[:access_jwt]}")
43
42
 
44
- unless response.is_a?(Net::HTTPSuccess)
45
- return { error: "Bluesky API error: #{response.code} #{response.body}" }
46
- end
43
+ return api_error(response) unless success?(response)
47
44
 
48
45
  data = JSON.parse(response.body)
49
46
  rkey = data["uri"].split("/").last
50
47
  { id: data["uri"], url: "https://bsky.app/profile/#{handle}/post/#{rkey}" }
51
48
  end
52
49
 
53
- private
54
-
55
- def create_session(handle, app_password)
56
- uri = URI("#{API}/xrpc/com.atproto.server.createSession")
57
- body = { "identifier" => handle, "password" => app_password }
58
- response = https_post_json(uri, body)
59
-
60
- unless response.is_a?(Net::HTTPSuccess)
61
- return { error: "Bluesky auth failed: #{response.code} #{response.body}" }
62
- end
63
-
64
- data = JSON.parse(response.body)
65
- { did: data["did"], access_jwt: data["accessJwt"] }
50
+ def build_record(text)
51
+ record = {
52
+ "$type" => "app.bsky.feed.post",
53
+ "text" => text,
54
+ "createdAt" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ")
55
+ }
56
+ facets = detect_facets(text)
57
+ record["facets"] = facets unless facets.empty?
58
+ record
66
59
  end
67
60
 
68
61
  def detect_facets(text)
69
- facets = []
70
-
71
- # URLs
72
- text.scan(/(https?:\/\/[^\s)]+)/) do
73
- match = Regexp.last_match
74
- byte_start = text[0...match.begin(0)].bytesize
75
- byte_end = byte_start + match[0].bytesize
76
- facets << {
77
- "index" => { "byteStart" => byte_start, "byteEnd" => byte_end },
78
- "features" => [{ "$type" => "app.bsky.richtext.facet#link", "uri" => match[0] }]
79
- }
80
- end
62
+ link_facets(text) + tag_facets(text)
63
+ end
81
64
 
82
- # Hashtags
83
- text.scan(/(?<=\s|^)#(\w+)/) do
84
- match = Regexp.last_match
85
- tag_with_hash = "##{match[1]}"
86
- byte_start = text[0...match.begin(0)].bytesize
87
- byte_end = byte_start + tag_with_hash.bytesize
88
- facets << {
89
- "index" => { "byteStart" => byte_start, "byteEnd" => byte_end },
90
- "features" => [{ "$type" => "app.bsky.richtext.facet#tag", "tag" => match[1] }]
91
- }
65
+ def link_facets(text)
66
+ matches(text, %r{https?://[^\s)]+}).map do |match|
67
+ facet(text, match, { "$type" => "app.bsky.richtext.facet#link", "uri" => match[0] })
92
68
  end
93
-
94
- facets
95
69
  end
96
70
 
97
- def https_post_json(uri, body, auth: nil)
98
- http = Net::HTTP.new(uri.host, uri.port)
99
- http.use_ssl = true
71
+ def tag_facets(text)
72
+ matches(text, /(?<=\s|^)#(\w+)/).map do |match|
73
+ facet(text, match, { "$type" => "app.bsky.richtext.facet#tag", "tag" => match[1] })
74
+ end
75
+ end
100
76
 
101
- request = Net::HTTP::Post.new(uri)
102
- request["Content-Type"] = "application/json"
103
- request["Authorization"] = "Bearer #{auth}" if auth
104
- request.body = JSON.generate(body)
77
+ def matches(text, pattern)
78
+ text.to_enum(:scan, pattern).map { Regexp.last_match }
79
+ end
105
80
 
106
- http.request(request)
81
+ def facet(text, match, feature)
82
+ byte_start = text[0...match.begin(0)].bytesize
83
+ {
84
+ "index" => { "byteStart" => byte_start, "byteEnd" => byte_start + match[0].bytesize },
85
+ "features" => [feature]
86
+ }
107
87
  end
108
88
  end
109
89
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Causeway
4
+ module Publishers
5
+ # Posts to a LinkedIn personal profile or organization feed.
6
+ class LinkedIn < Base
7
+ API = "https://api.linkedin.com/rest/posts"
8
+
9
+ def name
10
+ "LinkedIn"
11
+ end
12
+
13
+ def post(text)
14
+ token = config.linkedin_token
15
+ author = author_urn
16
+
17
+ return { error: "LinkedIn token not configured." } unless present?(token)
18
+ return { error: "LinkedIn person or organization ID not configured." } unless author
19
+
20
+ result(create_post(text, author, token))
21
+ end
22
+
23
+ private
24
+
25
+ def author_urn
26
+ return "urn:li:person:#{config.linkedin_person_id}" if present?(config.linkedin_person_id)
27
+
28
+ "urn:li:organization:#{config.linkedin_organization_id}" if present?(config.linkedin_organization_id)
29
+ end
30
+
31
+ def create_post(text, author, token)
32
+ body = {
33
+ "author" => author,
34
+ "commentary" => text,
35
+ "visibility" => "PUBLIC",
36
+ "distribution" => { "feedDistribution" => "MAIN_FEED" },
37
+ "lifecycleState" => "PUBLISHED"
38
+ }
39
+ post_json(URI(API), body, "Authorization" => "Bearer #{token}",
40
+ "LinkedIn-Version" => config.linkedin_api_version,
41
+ "X-Restli-Protocol-Version" => "2.0.0")
42
+ end
43
+
44
+ def result(response)
45
+ if %w[401 403].include?(response.code)
46
+ { error: "LinkedIn token expired or invalid: #{response.code} #{response.body}" }
47
+ elsif success?(response)
48
+ post_id = response["x-restli-id"]
49
+ { id: post_id, url: "https://www.linkedin.com/feed/update/#{post_id}" }
50
+ else
51
+ api_error(response)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,37 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "json"
5
- require "uri"
6
-
7
3
  module Causeway
8
4
  module Publishers
5
+ # Posts a status to a Mastodon instance.
9
6
  class Mastodon < Base
10
7
  def name
11
8
  "Mastodon"
12
9
  end
13
10
 
14
11
  def post(text)
15
- token = Causeway.configuration.mastodon_token
16
- instance = Causeway.configuration.mastodon_instance || "https://mastodon.social"
12
+ token = config.mastodon_token
13
+ return { error: "Mastodon token not configured." } unless present?(token)
17
14
 
18
- return { error: "Mastodon token not configured." } unless token && !token.empty?
15
+ response = create_status(text, token)
16
+ return api_error(response) unless success?(response)
19
17
 
20
- uri = URI("#{instance}/api/v1/statuses")
21
- http = Net::HTTP.new(uri.host, uri.port)
22
- http.use_ssl = uri.scheme == "https"
18
+ data = JSON.parse(response.body)
19
+ { id: data["id"], url: data["url"] }
20
+ end
21
+
22
+ private
23
23
 
24
+ def create_status(text, token)
25
+ instance = config.mastodon_instance || "https://mastodon.social"
26
+ uri = URI("#{instance}/api/v1/statuses")
24
27
  request = Net::HTTP::Post.new(uri)
25
28
  request["Authorization"] = "Bearer #{token}"
26
29
  request.set_form_data("status" => text)
27
-
28
- response = http.request(request)
29
- unless response.is_a?(Net::HTTPSuccess)
30
- return { error: "Mastodon API error: #{response.code} #{response.body}" }
31
- end
32
-
33
- data = JSON.parse(response.body)
34
- { id: data["id"], url: data["url"] }
30
+ perform(uri, request)
35
31
  end
36
32
  end
37
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Causeway
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
data/lib/causeway.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "causeway/version"
4
4
  require_relative "causeway/configuration"
5
5
  require_relative "causeway/publishers/base"
6
6
  require_relative "causeway/publishers/bluesky"
7
+ require_relative "causeway/publishers/linkedin"
7
8
  require_relative "causeway/publishers/mastodon"
8
9
 
9
10
  module Causeway
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: causeway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc Heiligers
@@ -25,6 +25,7 @@ files:
25
25
  - lib/causeway/configuration.rb
26
26
  - lib/causeway/publishers/base.rb
27
27
  - lib/causeway/publishers/bluesky.rb
28
+ - lib/causeway/publishers/linkedin.rb
28
29
  - lib/causeway/publishers/mastodon.rb
29
30
  - lib/causeway/version.rb
30
31
  - sig/causeway.rbs
@@ -51,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  - !ruby/object:Gem::Version
52
53
  version: '0'
53
54
  requirements: []
54
- rubygems_version: 4.0.16
55
+ rubygems_version: 4.0.20
55
56
  specification_version: 4
56
57
  summary: Publish posts to social media.
57
58
  test_files: []