opengraphplus 0.1.4 → 0.1.5

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: 48292c717ae45fcea5cd4273eba0baf9b1d9ebeb646ddc2d9c2cf666894c1f74
4
- data.tar.gz: 293b987be6c9eee0193d12e601df3e542713e53f631720ddab150b1374a4055b
3
+ metadata.gz: 7d947ee47bc0e434395f69cceb99321f78ab4f8ac437f5b537b97ffa9a199d51
4
+ data.tar.gz: 308d3466e160b11f935afd3b9f0c891ba3b268f95f3de12400af9e815020a82f
5
5
  SHA512:
6
- metadata.gz: 8e4a720f1974d2177dc7221cbe073dd3d68d57393c6b85d387d602d40bb2cf479c4118f4383ef40973d29c1eeb3fc60323500d29846d296acf64e15c35ff5c58
7
- data.tar.gz: 8655b837fa39a53ba16b9fd2ca1112f99c04fa4b5472b54f8bc35f74915e23b4dd5b4e4fba0b26dba08b2a8d9fbed7f573ad63927d3e5a12c1c3ff475f19dcd2
6
+ metadata.gz: ac3f345867d3cc9e5ad6fa1745f5f6ea45cf505fdd057191c0af0d48ca62d8af6e64aa7dfa26b2097f8d97af95fab8dec9888a37b00a573bc94c5ad68660c6c3
7
+ data.tar.gz: 9a20a378f2e36d594667c2abdba8e194bdcae2bc55a2daf33bf1195162b9c9498ca257e426e645ae85a3087cc0f80b83ff9f7065f730cc240564daf7617e58e7
@@ -2,12 +2,14 @@
2
2
 
3
3
  module OpenGraphPlus
4
4
  class Configuration
5
- attr_reader :api_key
6
-
7
5
  def initialize
8
6
  @api_key = nil
9
7
  end
10
8
 
9
+ def api_key
10
+ @api_key or warn "[OpenGraphPlus] API key not configured. Set OpenGraphPlus.configuration.api_key to enable automatic Open Graph image generation."
11
+ end
12
+
11
13
  def api_key=(value)
12
14
  @api_key = value.is_a?(APIKey) ? value : APIKey.parse(value)
13
15
  end
@@ -6,11 +6,20 @@ module OpenGraphPlus
6
6
 
7
7
  def initialize(request)
8
8
  @request = request
9
- @signature_url = Signature::URL.new
10
9
  end
11
10
 
12
11
  def url
13
- @signature_url.build("/opengraph", url: request.original_url)
12
+ return nil unless api_key
13
+
14
+ Signature::URL.new
15
+ .signed_path("/api/websites/v1", api_key)
16
+ .build("image", url: request.original_url)
17
+ end
18
+
19
+ private
20
+
21
+ def api_key
22
+ @api_key ||= OpenGraphPlus.configuration.api_key
14
23
  end
15
24
  end
16
25
  end
@@ -7,21 +7,14 @@ module OpenGraphPlus
7
7
  class Base
8
8
  include Enumerable
9
9
 
10
- def each(&)
11
- tags.each do |t|
12
- case t
13
- when Base
14
- t.each(&)
15
- when Tag
16
- yield t if t.content
17
- end
18
- end
19
- end
10
+ def each(&block) = tags.each(&block)
20
11
 
21
- def tags = []
12
+ def tag(property, value)
13
+ Tag.new(property, value) if value
14
+ end
22
15
 
23
- def render_in(rails_view_context)
24
- rails_view_context.safe_join(map { |tag| tag.render_in(rails_view_context) }, "\n")
16
+ def render_in(_view_context = nil)
17
+ map { |tag| tag.render_in }.join("\n").html_safe
25
18
  end
26
19
 
27
20
  def update(**kwargs)
@@ -31,23 +24,32 @@ module OpenGraphPlus
31
24
  end
32
25
 
33
26
  class Image < Base
34
- attr_accessor :url, :width, :height, :type, :alt, :secure_url
27
+ attr_accessor :url, :secure_url, :type, :width, :height, :alt
28
+
29
+ def tags
30
+ [
31
+ tag("og:image", url),
32
+ tag("og:image:secure_url", secure_url),
33
+ tag("og:image:type", type),
34
+ tag("og:image:width", width),
35
+ tag("og:image:height", height),
36
+ tag("og:image:alt", alt)
37
+ ].compact
38
+ end
39
+ end
40
+
41
+ class Viewport < Base
42
+ attr_accessor :width
35
43
 
36
44
  def tags
37
45
  [
38
- Tag.new("og:image", url),
39
- Tag.new("og:image:secure_url", secure_url),
40
- Tag.new("og:image:type", type),
41
- Tag.new("og:image:width", width),
42
- Tag.new("og:image:height", height),
43
- Tag.new("og:image:alt", alt),
44
- ]
46
+ tag("og:plus:viewport:width", width)
47
+ ].compact
45
48
  end
46
49
  end
47
50
 
48
51
  class Plus < Base
49
- attr_accessor :selector
50
- attr_reader :style
52
+ attr_accessor :selector, :style
51
53
 
52
54
  def style=(value)
53
55
  @style = case value
@@ -58,11 +60,16 @@ module OpenGraphPlus
58
60
  end
59
61
  end
60
62
 
63
+ def viewport
64
+ @viewport ||= Viewport.new
65
+ end
66
+
61
67
  def tags
62
68
  [
63
- Tag.new("og:plus:selector", selector),
64
- Tag.new("og:plus:style", style),
65
- ]
69
+ tag("og:plus:selector", selector),
70
+ tag("og:plus:style", style),
71
+ *viewport.tags
72
+ ].compact
66
73
  end
67
74
 
68
75
  private
@@ -75,13 +82,18 @@ module OpenGraphPlus
75
82
  class OG < Base
76
83
  attr_accessor :title, :description, :url, :type, :site_name, :locale, :determiner, :audio, :video
77
84
 
78
- def image = @image ||= Image.new
79
- def plus = @plus ||= Plus.new
80
-
81
85
  def initialize
82
86
  @type = "website"
83
87
  end
84
88
 
89
+ def image
90
+ @image ||= Image.new
91
+ end
92
+
93
+ def plus
94
+ @plus ||= Plus.new
95
+ end
96
+
85
97
  def image_url=(url)
86
98
  image.url = url
87
99
  image.secure_url = url
@@ -89,46 +101,73 @@ module OpenGraphPlus
89
101
 
90
102
  def tags
91
103
  [
92
- Tag.new("og:title", title),
93
- Tag.new("og:description", description),
94
- Tag.new("og:url", url),
95
- Tag.new("og:type", type),
96
- Tag.new("og:site_name", site_name),
97
- Tag.new("og:locale", locale),
98
- Tag.new("og:determiner", determiner),
99
- Tag.new("og:audio", audio),
100
- Tag.new("og:video", video),
101
- image,
102
- plus,
103
- ]
104
+ tag("og:title", title),
105
+ tag("og:description", description),
106
+ tag("og:url", url),
107
+ tag("og:type", type),
108
+ tag("og:site_name", site_name),
109
+ tag("og:locale", locale),
110
+ tag("og:determiner", determiner),
111
+ tag("og:audio", audio),
112
+ tag("og:video", video),
113
+ *image.tags,
114
+ *plus.tags
115
+ ].compact
104
116
  end
105
117
  end
106
118
 
107
119
  class Twitter < Base
108
- attr_accessor :card, :site, :creator, :title, :description, :image, :image_alt
120
+ class Image < Base
121
+ attr_accessor :url, :alt
122
+
123
+ def tags
124
+ [
125
+ tag("twitter:image", url),
126
+ tag("twitter:image:alt", alt)
127
+ ].compact
128
+ end
129
+ end
130
+
131
+ attr_accessor :card, :site, :creator, :title, :description
109
132
 
110
133
  def initialize
111
134
  @card = "summary_large_image"
112
135
  end
113
136
 
137
+ def image
138
+ @image ||= Image.new
139
+ end
140
+
141
+ def image_url=(url)
142
+ image.url = url
143
+ end
144
+
114
145
  def tags
115
146
  [
116
- Tag.new("twitter:card", card),
117
- Tag.new("twitter:site", site),
118
- Tag.new("twitter:creator", creator),
119
- Tag.new("twitter:title", title),
120
- Tag.new("twitter:description", description),
121
- Tag.new("twitter:image", image),
122
- Tag.new("twitter:image:alt", image_alt),
123
- ]
147
+ tag("twitter:card", card),
148
+ tag("twitter:site", site),
149
+ tag("twitter:creator", creator),
150
+ tag("twitter:title", title),
151
+ tag("twitter:description", description),
152
+ *image.tags
153
+ ].compact
124
154
  end
125
155
  end
126
156
 
127
157
  class Root < Base
128
158
  extend Forwardable
129
159
 
130
- def og = @og ||= OG.new
131
- def twitter = @twitter ||= Twitter.new
160
+ def og
161
+ @og ||= OG.new
162
+ end
163
+
164
+ def twitter
165
+ @twitter ||= Twitter.new
166
+ end
167
+
168
+ def tags
169
+ [*og.tags, *twitter.tags]
170
+ end
132
171
 
133
172
  def_delegators :og,
134
173
  :title, :title=,
@@ -142,10 +181,6 @@ module OpenGraphPlus
142
181
  :video, :video=,
143
182
  :image, :image_url=,
144
183
  :plus
145
-
146
- def tags
147
- [og, twitter]
148
- end
149
184
  end
150
185
  end
151
186
 
@@ -4,9 +4,11 @@ module OpenGraphPlus
4
4
  module Rails
5
5
  module Controller
6
6
  extend ActiveSupport::Concern
7
+ include OpenGraphPlus::Rails::Helper
7
8
 
8
9
  included do
9
10
  helper_method :open_graph, :open_graph_tags, :open_graph_meta_tags
11
+ before_action :set_default_open_graph
10
12
  append_before_action :set_default_open_graph_image
11
13
  end
12
14
 
@@ -22,14 +24,17 @@ module OpenGraphPlus
22
24
 
23
25
  private
24
26
 
27
+ def set_default_open_graph
28
+ open_graph.type = "website"
29
+ open_graph.url = request.original_url
30
+ end
31
+
25
32
  def set_default_open_graph_image
26
33
  return if open_graph.image.url
27
34
 
28
35
  generated_url = open_graph_image_generator.url
29
36
  open_graph.image.url = generated_url if generated_url
30
37
  end
31
-
32
- include Helper
33
38
  end
34
39
  end
35
40
  end
@@ -6,10 +6,21 @@ require "base64"
6
6
  module OpenGraphPlus
7
7
  module Signature
8
8
  class Generator
9
+ class InvalidAPIKeyError < StandardError; end
10
+
9
11
  attr_reader :api_key
10
12
 
11
13
  def initialize(api_key)
12
- @api_key = api_key
14
+ @api_key = case api_key
15
+ when APIKey
16
+ api_key
17
+ when String
18
+ APIKey.parse(api_key)
19
+ end
20
+
21
+ raise InvalidAPIKeyError, "API key is missing or invalid" unless @api_key
22
+ raise InvalidAPIKeyError, "API key is missing public_key" unless @api_key.public_key
23
+ raise InvalidAPIKeyError, "API key is missing secret_key" unless @api_key.secret_key
13
24
  end
14
25
 
15
26
  def generate(path_and_query)
@@ -6,57 +6,40 @@ module OpenGraphPlus
6
6
  module Signature
7
7
  class URL
8
8
  DEFAULT_BASE_URL = "https://opengraphplus.com"
9
- DEFAULT_PATH_PREFIX = "/v2/:signature"
10
9
 
11
- attr_reader :base_uri, :path_prefix
10
+ attr_reader :base_uri
12
11
 
13
- def initialize(api_key: nil, generator: nil, base_url: nil, path_prefix: DEFAULT_PATH_PREFIX)
14
- @api_key = api_key
15
- @generator = generator
12
+ def initialize(base_url: nil)
16
13
  @base_uri = URI.parse(base_url || ENV.fetch("OPENGRAPHPLUS_URL", DEFAULT_BASE_URL))
17
- @path_prefix = path_prefix
18
14
  end
19
15
 
20
- def build(path, **params)
21
- return nil unless generator
22
-
23
- path_and_query = build_path_and_query(path, params)
24
- signature = generator.generate(path_and_query)
25
-
26
- base_uri.dup.tap do |uri|
27
- uri.path = signed_path(signature, path)
28
- uri.query = URI.encode_www_form(params) unless params.empty?
29
- end.to_s
30
- end
31
-
32
- def generator
33
- @generator ||= begin
34
- api_key = @api_key || OpenGraphPlus.configuration.api_key
35
- if api_key
36
- Generator.new(api_key)
37
- else
38
- warn_missing_api_key
39
- nil
40
- end
41
- end
16
+ def signed_path(prefix, api_key)
17
+ SignedPath.new(prefix:, api_key:, base_uri:)
42
18
  end
19
+ end
43
20
 
44
- private
21
+ class SignedPath
22
+ attr_reader :prefix, :api_key, :base_uri
45
23
 
46
- def build_path_and_query(path, params)
47
- normalized_path = File.join("/", path)
48
- params.empty? ? normalized_path : "#{normalized_path}?#{URI.encode_www_form(params)}"
24
+ def initialize(prefix:, api_key:, base_uri:)
25
+ @prefix = prefix
26
+ @api_key = api_key
27
+ @base_uri = base_uri
49
28
  end
50
29
 
51
- def signed_path(signature, path)
52
- File.join(path_prefix.gsub(":signature", signature), path)
30
+ def generator
31
+ @generator ||= Generator.new(api_key)
53
32
  end
54
33
 
55
- def warn_missing_api_key
56
- return if @warned
34
+ def build(*segments, **params)
35
+ signed_path = File.join("/", *segments.map(&:to_s))
36
+ path_and_query = params.empty? ? signed_path : "#{signed_path}?#{URI.encode_www_form(params)}"
37
+ signature = generator.generate(path_and_query)
57
38
 
58
- warn "[OpenGraphPlus] API key not configured. Set OpenGraphPlus.configuration.api_key to enable automatic Open Graph image generation."
59
- @warned = true
39
+ base_uri.dup.tap do |uri|
40
+ uri.path = File.join(prefix, signature, *segments.map(&:to_s))
41
+ uri.query = URI.encode_www_form(params) unless params.empty?
42
+ end.to_s
60
43
  end
61
44
  end
62
45
  end
@@ -15,8 +15,8 @@ module OpenGraphPlus
15
15
  %(<meta property="#{escape property}" content="#{escape content}">)
16
16
  end
17
17
 
18
- def render_in(rails_view_context)
19
- rails_view_context.raw(meta)
18
+ def render_in(_view_context = nil)
19
+ meta.html_safe
20
20
  end
21
21
 
22
22
  def to_s
@@ -7,11 +7,11 @@ module OpenGraphPlus
7
7
  class Base
8
8
  include Enumerable
9
9
 
10
- def each(&)
10
+ def each(&block)
11
11
  tags.each do |t|
12
12
  case t
13
13
  when Base
14
- t.each(&)
14
+ t.each(&block)
15
15
  when Tag
16
16
  yield t if t.content
17
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenGraphPlus
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opengraphplus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-12-18 00:00:00.000000000 Z
10
+ date: 2026-01-07 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport