opengraphplus 0.1.4 → 0.1.6
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 +4 -4
- data/README.md +49 -1
- data/lib/generators/opengraphplus/base_generator.rb +21 -0
- data/lib/generators/opengraphplus/credentials/credentials_generator.rb +50 -0
- data/lib/generators/opengraphplus/credentials/templates/initializer.rb.tt +5 -0
- data/lib/generators/opengraphplus/env/env_generator.rb +61 -0
- data/lib/generators/opengraphplus/env/templates/initializer.rb.tt +5 -0
- data/lib/generators/opengraphplus/install/templates/README +2 -42
- data/lib/generators/opengraphplus/install/templates/initializer.rb +3 -1
- data/lib/opengraphplus/configuration.rb +4 -2
- data/lib/opengraphplus/image_generator.rb +11 -2
- data/lib/opengraphplus/namespace.rb +91 -56
- data/lib/opengraphplus/rails/controller.rb +7 -2
- data/lib/opengraphplus/signature/generator.rb +12 -1
- data/lib/opengraphplus/signature/url.rb +21 -38
- data/lib/opengraphplus/tag.rb +2 -2
- data/lib/opengraphplus/tags.rb +2 -2
- data/lib/opengraphplus/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e8fd514b98d00901b6c41cf42ad246f71690c7fb632c2653f1e66571da1c0968
|
|
4
|
+
data.tar.gz: cb7eef25110066ade4cf4b5f05fe5f10448c7e3cf8a9f6d5bb91b785f7687d37
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2f0bb3269f68f0b57ab02e9f583042bae211f5c2f0ca7dec28a7cd35fed2e9e32ac934241380ce856f88a9919dfb2b4bdc8bbc46d2a87d70a2c6694207913a6e
|
|
7
|
+
data.tar.gz: 0b77487f2356101b48750cb729f9b88327678aafabe02fa5da16dc9ea55f2c0d5e5a3a3351ddb1294af14d791e7495fa2012ea82b4cac4afadc90f6d8e4b8770
|
data/README.md
CHANGED
|
@@ -18,7 +18,55 @@ gem install opengraphplus
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
### Get your API key
|
|
22
|
+
|
|
23
|
+
Sign up at [og.plus](https://og.plus) to get your API key.
|
|
24
|
+
|
|
25
|
+
### Configuration
|
|
26
|
+
|
|
27
|
+
#### Using environment variables
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
rails g opengraphplus:env ogp_live_████████████████████
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
This will:
|
|
34
|
+
- Append `OPENGRAPHPLUS__API_KEY=ogp_live_████████████████████` to your `.env` file (or the first env file found)
|
|
35
|
+
- Create `config/initializers/opengraphplus.rb`
|
|
36
|
+
|
|
37
|
+
To specify a different env file:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
rails g opengraphplus:env ogp_live_████████████████████ -e .envrc
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Using Rails credentials
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
rails g opengraphplus:credentials ogp_live_████████████████████
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This will:
|
|
50
|
+
- Add `opengraphplus.api_key` to your encrypted `credentials.yml.enc`
|
|
51
|
+
- Create `config/initializers/opengraphplus.rb`
|
|
52
|
+
|
|
53
|
+
#### Manual configuration
|
|
54
|
+
|
|
55
|
+
Run the basic install generator for a commented template:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
rails g opengraphplus:install
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Then configure manually in `config/initializers/opengraphplus.rb`:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
OpenGraphPlus.configure do |config|
|
|
65
|
+
config.api_key = ENV["OPENGRAPHPLUS__API_KEY"]
|
|
66
|
+
# or
|
|
67
|
+
config.api_key = Rails.application.credentials.opengraphplus.api_key
|
|
68
|
+
end
|
|
69
|
+
```
|
|
22
70
|
|
|
23
71
|
## Development
|
|
24
72
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
|
|
5
|
+
module Opengraphplus
|
|
6
|
+
module Generators
|
|
7
|
+
class BaseGenerator < Rails::Generators::Base
|
|
8
|
+
API_KEY_PREFIX = "ogp_"
|
|
9
|
+
|
|
10
|
+
argument :api_key, type: :string, required: true,
|
|
11
|
+
desc: "Your OpenGraphPlus API key"
|
|
12
|
+
|
|
13
|
+
def validate_api_key
|
|
14
|
+
unless api_key.start_with?(API_KEY_PREFIX)
|
|
15
|
+
say_status :error, "Invalid API key: must start with '#{API_KEY_PREFIX}'", :red
|
|
16
|
+
raise SystemExit
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
require "active_support/core_ext/hash/keys"
|
|
5
|
+
require_relative "../base_generator"
|
|
6
|
+
|
|
7
|
+
module Opengraphplus
|
|
8
|
+
module Generators
|
|
9
|
+
class CredentialsGenerator < BaseGenerator
|
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
|
11
|
+
|
|
12
|
+
desc "Configures OpenGraphPlus using Rails encrypted credentials"
|
|
13
|
+
|
|
14
|
+
def add_to_credentials
|
|
15
|
+
credentials = Rails.application.credentials
|
|
16
|
+
|
|
17
|
+
unless credentials.key?
|
|
18
|
+
say_status :error, "No credentials key found. Run `rails credentials:edit` first.", :red
|
|
19
|
+
return
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Read existing content, merge, write back
|
|
23
|
+
yaml_content = credentials.read.presence || ""
|
|
24
|
+
config = parse_yaml(yaml_content)
|
|
25
|
+
|
|
26
|
+
config["opengraphplus"] ||= {}
|
|
27
|
+
config["opengraphplus"]["api_key"] = api_key
|
|
28
|
+
|
|
29
|
+
credentials.write(yaml_dump(config))
|
|
30
|
+
say_status :insert, "credentials.yml.enc (opengraphplus.api_key)", :green
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def create_initializer
|
|
34
|
+
template "initializer.rb.tt", "config/initializers/opengraphplus.rb"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def parse_yaml(content)
|
|
40
|
+
return {} if content.blank?
|
|
41
|
+
YAML.safe_load(content, permitted_classes: [Symbol], aliases: true) || {}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def yaml_dump(config)
|
|
45
|
+
# Preserve nice formatting
|
|
46
|
+
YAML.dump(config.deep_stringify_keys)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_generator"
|
|
4
|
+
|
|
5
|
+
module Opengraphplus
|
|
6
|
+
module Generators
|
|
7
|
+
class EnvGenerator < BaseGenerator
|
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
|
9
|
+
|
|
10
|
+
desc "Configures OpenGraphPlus using environment variables"
|
|
11
|
+
|
|
12
|
+
class_option :envfile, type: :string, aliases: "-e",
|
|
13
|
+
desc: "Specific env file to write to (e.g., .env, .envrc)"
|
|
14
|
+
|
|
15
|
+
ENV_FILES = %w[.env .env.local .env.development .env.development.local .envrc].freeze
|
|
16
|
+
ENV_VAR_NAME = "OPENGRAPHPLUS__API_KEY"
|
|
17
|
+
|
|
18
|
+
def append_to_env_file
|
|
19
|
+
if options[:envfile]
|
|
20
|
+
write_to_env_file(options[:envfile], create: true)
|
|
21
|
+
else
|
|
22
|
+
env_file = detect_env_file
|
|
23
|
+
if env_file
|
|
24
|
+
write_to_env_file(env_file)
|
|
25
|
+
else
|
|
26
|
+
say_status :skip, "No env file found (create one or use -e)", :yellow
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def create_initializer
|
|
32
|
+
template "initializer.rb.tt", "config/initializers/opengraphplus.rb"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def detect_env_file
|
|
38
|
+
ENV_FILES.find { |f| File.exist?(f) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def write_to_env_file(env_file, create: false)
|
|
42
|
+
env_line = "#{ENV_VAR_NAME}=#{api_key}"
|
|
43
|
+
|
|
44
|
+
unless File.exist?(env_file)
|
|
45
|
+
if create
|
|
46
|
+
create_file env_file, "#{env_line}\n"
|
|
47
|
+
end
|
|
48
|
+
return
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
if File.read(env_file).include?(ENV_VAR_NAME)
|
|
52
|
+
say_status :skip, "#{env_file} (#{ENV_VAR_NAME} already defined)", :yellow
|
|
53
|
+
else
|
|
54
|
+
content = File.read(env_file)
|
|
55
|
+
prefix = content.end_with?("\n") || content.empty? ? "" : "\n"
|
|
56
|
+
append_to_file env_file, "#{prefix}#{env_line}\n"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -1,46 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
OpenGraphPlus has been installed!
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
1. Add your API key to config/initializers/opengraphplus.rb
|
|
7
|
-
Get your API key at: https://opengraphplus.com/dashboard
|
|
8
|
-
|
|
9
|
-
2. Add the helper to your layout file (e.g., app/views/layouts/application.html.erb):
|
|
10
|
-
|
|
11
|
-
<head>
|
|
12
|
-
<%%= open_graph_meta_tags %>
|
|
13
|
-
</head>
|
|
14
|
-
|
|
15
|
-
3. Customize the default Open Graph tags in ApplicationController:
|
|
16
|
-
|
|
17
|
-
class ApplicationController < ActionController::Base
|
|
18
|
-
open_graph do |og|
|
|
19
|
-
og.type = "website"
|
|
20
|
-
og.url = request.original_url
|
|
21
|
-
og.site_name = "Your Site Name"
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
4. Override tags in specific controllers (inherits from parent):
|
|
26
|
-
|
|
27
|
-
class ArticlesController < ApplicationController
|
|
28
|
-
before_action { @article = Article.find(params[:id]) }
|
|
29
|
-
|
|
30
|
-
open_graph do |og|
|
|
31
|
-
og.title = @article.title
|
|
32
|
-
og.description = @article.excerpt
|
|
33
|
-
og.type = "article"
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
5. Set tags in views:
|
|
38
|
-
|
|
39
|
-
<%% open_graph do |og|
|
|
40
|
-
og.title = "My Page Title"
|
|
41
|
-
og.description = "My Page Description"
|
|
42
|
-
end %>
|
|
43
|
-
|
|
44
|
-
6. Verify it works by checking your page source for og:image meta tags,
|
|
45
|
-
or use the preview tool at: https://opengraphplus.com/previews/new
|
|
4
|
+
For setup instructions, visit https://opengraphplus.com/guides/rails
|
|
46
5
|
|
|
6
|
+
For a website API key, visit https://opengraphplus.com/dashboard
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Get your API key at https://opengraphplus.com/dashboard
|
|
3
4
|
OpenGraphPlus.configure do |config|
|
|
4
|
-
#
|
|
5
|
+
# Use Rails credentials.
|
|
5
6
|
config.api_key = Rails.application.credentials.opengraphplus_api_key
|
|
7
|
+
|
|
6
8
|
# Or use ENV:
|
|
7
9
|
# config.api_key = ENV["OPENGRAPHPLUS_API_KEY"]
|
|
8
10
|
end
|
|
@@ -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
|
-
|
|
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
|
|
12
|
+
def tag(property, value)
|
|
13
|
+
Tag.new(property, value) if value
|
|
14
|
+
end
|
|
22
15
|
|
|
23
|
-
def render_in(
|
|
24
|
-
|
|
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, :
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
|
131
|
-
|
|
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
|
|
10
|
+
attr_reader :base_uri
|
|
12
11
|
|
|
13
|
-
def initialize(
|
|
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
|
|
21
|
-
|
|
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
|
-
|
|
21
|
+
class SignedPath
|
|
22
|
+
attr_reader :prefix, :api_key, :base_uri
|
|
45
23
|
|
|
46
|
-
def
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
52
|
-
|
|
30
|
+
def generator
|
|
31
|
+
@generator ||= Generator.new(api_key)
|
|
53
32
|
end
|
|
54
33
|
|
|
55
|
-
def
|
|
56
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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
|
data/lib/opengraphplus/tag.rb
CHANGED
data/lib/opengraphplus/tags.rb
CHANGED
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
|
+
version: 0.1.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brad Gessler
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 2026-01-08 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: activesupport
|
|
@@ -34,6 +34,11 @@ files:
|
|
|
34
34
|
- LICENSE.txt
|
|
35
35
|
- README.md
|
|
36
36
|
- Rakefile
|
|
37
|
+
- lib/generators/opengraphplus/base_generator.rb
|
|
38
|
+
- lib/generators/opengraphplus/credentials/credentials_generator.rb
|
|
39
|
+
- lib/generators/opengraphplus/credentials/templates/initializer.rb.tt
|
|
40
|
+
- lib/generators/opengraphplus/env/env_generator.rb
|
|
41
|
+
- lib/generators/opengraphplus/env/templates/initializer.rb.tt
|
|
37
42
|
- lib/generators/opengraphplus/install/install_generator.rb
|
|
38
43
|
- lib/generators/opengraphplus/install/templates/README
|
|
39
44
|
- lib/generators/opengraphplus/install/templates/initializer.rb
|