jekyll-action-network 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6beafe15785d6af1657202257f0f6db8f73ff9c6eae3b7de10d7a4b91c7ce00d
4
- data.tar.gz: 5b08d9c2b83ff65ae712716f7ea13ac91328a2d0adcb98178c58b0bf1d5cb69d
3
+ metadata.gz: 2947b846e07427841ccd8923055f0bcda8f01f80fc38291fd0b9bf077af007c6
4
+ data.tar.gz: 9ff2f727a3da1e2c31015b3d9fc90f4c94ab1666db54a939473c02f2a1a81aa3
5
5
  SHA512:
6
- metadata.gz: 354a3b9428835b02b85511a1a588b22604f2eb42f066ac6b1e9829217c9cac0d7c286318ab99919f5c65db4d9ba35917a1e5a4f2b8c9980b9ba17df5a8893c04
7
- data.tar.gz: 3e2e89571d35141b49effbab7afd858e5ca2696ed570d80747e317059be616c4751ce26a0f1973cd7e1c5ba9c5b16ed3f5e65e37aad49b071b49b020de225b4f
6
+ metadata.gz: 685c03c3b844d8fdd9cbfe373db626a492175958e3624f2dd1ec685057f05bceb652ec6179d7d6f348f934eb1a88b963601a08a5c0097167635f0bf5bfaca914
7
+ data.tar.gz: d6cc9aaa7040eb21d42458b4f09c2fa27f3668aa959f0196be2eed55ae69d455c510722601df65a3fc0126886e93171f376706e3091eb79090edcf0b9661c126
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module ActionNetwork
5
+ ##
6
+ # An action network action, call the +doc+ method to get a
7
+ # Jekyll::Document for this action
8
+ class Action
9
+ def initialize(site, name, collection, config, action)
10
+ @site = site
11
+ @collection = collection
12
+ @config = config
13
+ @action = action
14
+ @name = name
15
+ @utils = Jekyll::ActionNetwork::Utils.new
16
+ end
17
+
18
+ def doc
19
+ return @doc if @doc
20
+
21
+ @doc = Jekyll::Document.new(path, collection: @collection, site: @site)
22
+ @doc.content = content
23
+ @doc.merge_data!(front_matter)
24
+ @doc
25
+ end
26
+
27
+ def slug
28
+ @slug ||= @action["browser_url"].split("/")[-1] if @action["browser_url"]
29
+ end
30
+
31
+ def path
32
+ @path ||= File.join(@site.source, "_#{@config["collection"]}", "#{slug}.md")
33
+ end
34
+
35
+ def front_matter
36
+ return @front_matter if @front_matter
37
+
38
+ @front_matter = {}
39
+ @config["mappings"].each do |a, d|
40
+ front_matter[d] = @action[a]
41
+ end
42
+ @front_matter["layout"] = @config["layout"]
43
+ @front_matter["slug"] = slug
44
+ @front_matter["embed_code"] = @utils.make_embed_code(@action["browser_url"])
45
+ @front_matter["action_type"] = @name
46
+ @front_matter
47
+ end
48
+
49
+ def content
50
+ @content ||= @action[@config["content"]]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module ActionNetwork
5
+ ##
6
+ # For creating Jekyll::Collections from an Action Network config
7
+ #
8
+ class Collection
9
+ UTILS = Jekyll::ActionNetwork::Utils.new
10
+
11
+ def initialize(site, generator, client, config_init, settings)
12
+ @site = site
13
+ @generator = generator
14
+ @settings = settings
15
+ @client = client
16
+ @config_init = config_init
17
+ end
18
+
19
+ def config
20
+ @name = @config_init["name"]
21
+ @defaults ||= @settings["defaults"]
22
+ @config ||= @defaults[@name].merge!(@config_init)
23
+ end
24
+
25
+ def collection
26
+ return @collection if @collection
27
+
28
+ Jekyll.logger.debug LOG_NAME, "Creating collection #{@name}"
29
+ @collection ||= @site.collections[@name] if @site.collections[@name]
30
+ @collection ||= Jekyll::Collection.new(@site, config["collection"])
31
+ end
32
+
33
+ def documents
34
+ documents = []
35
+ filtered_actions.each do |action_data|
36
+ action = Jekyll::ActionNetwork::Action.new(@site, @name, collection, config, action_data)
37
+ documents << action.doc
38
+ end
39
+ documents
40
+ end
41
+
42
+ def populate
43
+ collection.docs.concat(documents)
44
+ @collection
45
+ end
46
+
47
+ def actions
48
+ @actions ||= @generator.get_full_list(@name)
49
+ end
50
+
51
+ def filters
52
+ filters = @settings["filters"]["all"].merge(@settings["filters"][@name] || {})
53
+ filters.merge(config["filters"] || {})
54
+ end
55
+
56
+ def filtered_actions
57
+ public_actions = []
58
+ actions.each do |action|
59
+ next if action_filtered(action, filters)
60
+
61
+ public_actions << reduce_fields(action)
62
+ end
63
+ end
64
+
65
+ def action_filtered(action, filters)
66
+ filtered = false
67
+ filters.each do |key, value|
68
+ next if action[key] == value
69
+
70
+ next if value == "%" && action[key]
71
+
72
+ Jekyll.logger.debug @log_name, "#{name} filtered because #{action[key]} == #{value}"
73
+ filtered = true
74
+ break
75
+ end
76
+ filtered
77
+ end
78
+
79
+ def reduce_fields(action)
80
+ reduced_action = {}
81
+ @settings["fields"].each do |field|
82
+ reduced_action[field] = action[field] if action[field]
83
+ end
84
+ reduced_action
85
+ end
86
+ end
87
+ end
88
+ end
@@ -5,151 +5,119 @@ require "jekyll"
5
5
  require "yaml"
6
6
  require "dotenv/load"
7
7
 
8
- ##
9
- # This class is a Jekyll::Generator that creates collections from
10
- # Action Network
11
8
  module Jekyll
12
9
  ##
13
- # This sub class is a Jekyll::Generator that creates collections from
14
- # Action Network
15
- class ActionNetworkGenerator < Jekyll::Generator
16
- def generate(site)
17
- return unless site.config["action_network"]
18
-
19
- @site = site
20
- @log_name = "Action Network:"
21
- @settings ||= settings
22
- @config = site.config["action_network"]
23
- return unless authenticate
24
-
25
- @actions = Jekyll::Collection.new(site, "actions") if @config["actions"]
26
- @config.each do |name, _config|
27
- next unless @defaults[name]
28
-
29
- make_collection(name)
30
- end
31
- site.collections["actions"] = @actions if @actions
32
- end
10
+ # An integration with Action Network for a Jekyll site
11
+ module ActionNetwork
12
+ LOG_NAME = "Action Network: "
33
13
 
34
- def settings
35
- @settings = YAML.load_file("#{File.expand_path(__dir__)}/settings.yaml")
36
- @defaults = @settings["defaults"]
37
- @fields = @settings["fields"]
38
- @filters = @settings["filters"]
39
- @settings
40
- end
14
+ ##
15
+ # Generates Jekyll collections from the Action Network API
16
+ class Generator < Jekyll::Generator
17
+ UTILS = Jekyll::ActionNetwork::Utils.new
18
+
19
+ def generate(site)
20
+ return unless site.config["action_network"]
41
21
 
42
- def make_collection(name)
43
- if @config[name].is_a?(Hash)
44
- config = @defaults[name].merge(@config[name])
45
- config["mappings"] = @defaults[name]["mappings"].merge(@config[name]["mappings"] || {})
22
+ @site = site
23
+ @config = site.config["action_network"]
24
+ @utils = Jekyll::ActionNetwork::Utils.new
25
+ return unless authenticate
26
+
27
+ make_collections
46
28
  end
47
- config ||= @defaults[name]
48
- Jekyll.logger.info @log_name, "Processing #{name}"
49
- actions = get_full_list(name)
50
- Jekyll.logger.warn "#{name} before filter: #{actions.length}"
51
- filtered_actions = filter_actions(name, actions, config)
52
- Jekyll.logger.warn "#{name} after filter: #{filtered_actions.length}"
53
- collection = @site.collections[name] if @site.collections[name]
54
- collection ||= Jekyll::Collection.new(@site, config["collection"])
55
- filtered_actions.each do |action|
56
- slug = action["browser_url"].split("/")[-1]
57
- path = File.join(@site.source, "_#{config["collection"]}", "#{slug}.md")
58
- doc = Jekyll::Document.new(path, collection: collection, site: @site)
59
- doc.content = action[config["content"]]
60
- frontmattter_data = {}
61
- config["mappings"].each do |a, d|
62
- frontmattter_data[d] = action[a]
29
+
30
+ def make_collections
31
+ @config["import"].each do |config|
32
+ col_config = make_collection_config(config)
33
+ name = col_config["name"]
34
+ next unless col_config["name"]
35
+
36
+ make_collection(name, col_config)
63
37
  end
64
- frontmattter_data.merge!({
65
- "layout" => config["layout"],
66
- "slug" => slug,
67
- "embed_code" => make_embed_code(action["browser_url"]),
68
- "action_type" => name
69
- })
70
- doc.merge_data!(frontmattter_data)
71
- collection.docs << doc
72
- @actions.docs << doc if @actions
38
+ @site.collections["actions"] = actions if @config["actions"]
73
39
  end
74
- @site.collections[config["collection"]] = collection
75
- end
76
40
 
77
- def make_embed_code(browser_url, style = nil)
78
- relative_url = browser_url.split("://")[1].sub("actionnetwork.org/", "")
79
- split_url = relative_url.split("/")
80
- css = @settings["embed"]["styles"][style] if style
81
- resource = @settings["embed"]["resources"][split_url[0]]
82
- slug = split_url[1]
83
- "#{css}<script src='https://actionnetwork.org/widgets/v5/#{resource}/#{slug}?format=js&source=widget'></script>
84
- <div id='can-#{resource}-area-#{slug}' style='width: 100%'></div>"
85
- end
41
+ def make_collection_config(col_config_init)
42
+ return col_config_init if col_config_init.is_a? Hash
43
+ return { "name" => col_config_init } if col_config_init.is_a? String
44
+
45
+ Jekyll.logger.warn LOG_NAME, "Config format #{col_config_init} not recognised"
46
+ end
86
47
 
87
- def get_full_list(name)
88
- page = 1
89
- actions = []
90
- loop do
91
- action_page = @client.send(name).list(page: page)
92
- break if action_page.empty?
48
+ def make_collection(name, config)
49
+ return unless defaults[name]
93
50
 
94
- actions.concat(action_page)
95
- page += 1
51
+ collection = Jekyll::ActionNetwork::Collection.new(@site, self, @client, config, settings)
52
+ @site.collections[collection.config["collection"]] = collection.populate
53
+ actions.docs.concat(collection.collection.docs)
96
54
  end
97
- actions
98
- end
99
55
 
100
- def filter_actions(name, actions, _config)
101
- public = []
102
- puts @filters["all"]
103
- puts @filters[name] || {}
104
- filters = @filters["all"].merge(@filters[name] || {})
105
- filters = filters.merge(@config["filters"] || {}) if @config.is_a? Hash
106
- puts filters
107
- actions.each do |action|
108
- filtered = true
109
- filters.each do |key, value|
110
- unless action[key] == value
111
- if value == "%" && action[key]
112
- filtered = false if action[key].length.positive?
113
- else
114
- Jekyll.logger.warn @log_name, "#{name} filtered because #{action[key]} == #{value}"
115
- filtered = true
116
- break
117
- end
118
- end
119
- filtered = false
56
+ def actions
57
+ return @actions if @actions
58
+
59
+ actions_config = @config["actions"] if @config["actions"].is_a? Hash
60
+ actions_settings = settings["actions"].merge(actions_config || {})
61
+ @actions ||= Jekyll::Collection.new(@site, actions_settings["collection"])
62
+ end
63
+
64
+ def settings
65
+ return @settings if @settings
66
+
67
+ settings = YAML.load_file("#{File.expand_path(__dir__)}/settings.yaml")
68
+ @fields = settings["fields"]
69
+ @filters = settings["filters"]
70
+ @settings = settings
71
+ end
72
+
73
+ def defaults
74
+ @defaults ||= settings["defaults"]
75
+ end
76
+
77
+ def api_key
78
+ key = @config["key"] || settings["key"]
79
+ return ENV.fetch(key[4..key.length - 1], nil) if key[0..3] == "ENV_"
80
+
81
+ key
82
+ end
83
+
84
+ def authenticate
85
+ unless api_key
86
+ Jekyll.logger.warn "Action Network API Key not found."
87
+ return
120
88
  end
121
- public << reduce_fields(action) unless filtered
89
+
90
+ unless entry_point.dig("_links", "osdi:tags").present?
91
+ Jekyll.logger.warn "Action Network Authentication Unsucessful"
92
+ return
93
+ end
94
+ true
122
95
  end
123
- public
124
- end
125
96
 
126
- def reduce_fields(action)
127
- reduced_action = {}
128
- @fields.each do |field|
129
- reduced_action[field] = action[field] if action[field]
97
+ def client
98
+ @client ||= ActionNetworkRest.new(api_key: api_key)
130
99
  end
131
- reduced_action
132
- end
133
100
 
134
- def get_api_key(key)
135
- return unless key
136
- return ENV.fetch(key[4..key.length - 1], nil) if key[0..3] == "ENV_"
137
- return ENV["ACTION_NETWORK_API_KEY"] if ENV["ACTION_NETWORK_API_KEY"]
101
+ def entry_point
102
+ @entry_point ||= client.entry_point.get
103
+ end
138
104
 
139
- key
140
- end
105
+ def page_size
106
+ @page_size ||= @entry_point["max_page_size"]
107
+ end
141
108
 
142
- def authenticate
143
- api_key = get_api_key(@config["key"])
144
- Jekyll.logger.warn "Action Network API Key not found." unless api_key
145
- return unless api_key
109
+ def get_full_list(name)
110
+ page = 1
111
+ actions = []
112
+ loop do
113
+ action_page = @client.send(name).list(page: page)
114
+ actions.concat(action_page)
115
+ break if action_page.size < page_size
146
116
 
147
- @client = ActionNetworkRest.new(api_key: api_key)
148
- unless @client.entry_point.authenticated_successfully?
149
- Jekyll.logger.warn "Action Network Authentication Unsucessful"
150
- return
117
+ page += 1
118
+ end
119
+ actions
151
120
  end
152
- true
153
121
  end
154
122
  end
155
123
  end
@@ -35,6 +35,9 @@ embed:
35
35
  styles:
36
36
  standard: <link href='https://actionnetwork.org/css/style-embed-v3.css' rel='stylesheet' type='text/css' />
37
37
  layout_only: <link href='https://actionnetwork.org/css/style-embed-whitelabel-v3.css' rel='stylesheet' type='text/css' />
38
+ actions:
39
+ collection: actions
40
+ key: ENV_ACTION_NETWORK_API_KEY
38
41
  defaults:
39
42
  petitions:
40
43
  collection: petitions
@@ -48,17 +51,6 @@ defaults:
48
51
  featured_image_url: image
49
52
  target: target
50
53
  "action_network:sponsor": sponsor
51
- # campaigns:
52
- # collection: campaigns
53
- # layout: an-campaign
54
- # content: description
55
- # mappings:
56
- # title: title
57
- # description: description
58
- # browser_url: browser_url
59
- # featured_image_url: image
60
- # actions: actions
61
- # "action_network:sponsor": sponsor
62
54
  events:
63
55
  collection: events
64
56
  layout: an-event
@@ -112,4 +104,15 @@ defaults:
112
104
  # call_to_action: call_to_action
113
105
  # browser_url: browser_url
114
106
  # featured_image_url: image
107
+ # "action_network:sponsor": sponsor
108
+ # campaigns:
109
+ # collection: campaigns
110
+ # layout: an-campaign
111
+ # content: description
112
+ # mappings:
113
+ # title: title
114
+ # description: description
115
+ # browser_url: browser_url
116
+ # featured_image_url: image
117
+ # actions: actions
115
118
  # "action_network:sponsor": sponsor
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module Jekyll
6
+ module ActionNetwork
7
+ ##
8
+ # Some commonly used tools for the Action Network generator
9
+ class Utils
10
+ def settings
11
+ return @settings if @settings
12
+
13
+ @settings = YAML.load_file("#{File.expand_path(__dir__)}/settings.yaml")
14
+ @defaults = @settings["defaults"]
15
+ @fields = @settings["fields"]
16
+ @filters = @settings["filters"]
17
+ @settings
18
+ end
19
+
20
+ def make_embed_code(browser_url, style = nil)
21
+ relative_url = browser_url.split("://")[1].sub("actionnetwork.org/", "")
22
+ split_url = relative_url.split("/")
23
+ css = settings["embed"]["styles"][style] if style
24
+ resource = settings["embed"]["resources"][split_url[0]]
25
+ slug = split_url[1]
26
+ "#{css}<script src='https://actionnetwork.org/widgets/v5/#{resource}/#{slug}?format=js&source=widget'></script>
27
+ <div id='can-#{resource}-area-#{slug}' style='width: 100%'></div>"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module ActionNetwork
5
- VERSION = "0.3.3"
5
+ VERSION = "0.4.1"
6
6
  end
7
7
  end
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "jekyll/action-network/version"
4
+ require "jekyll/action-network/utils"
5
+ require "jekyll/action-network/action"
6
+ require "jekyll/action-network/collection"
4
7
  require "jekyll/action-network/generator"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-action-network
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Irving
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-18 00:00:00.000000000 Z
11
+ date: 2022-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: action_network_rest
@@ -128,8 +128,11 @@ extensions: []
128
128
  extra_rdoc_files: []
129
129
  files:
130
130
  - lib/jekyll-action-network.rb
131
+ - lib/jekyll/action-network/action.rb
132
+ - lib/jekyll/action-network/collection.rb
131
133
  - lib/jekyll/action-network/generator.rb
132
134
  - lib/jekyll/action-network/settings.yaml
135
+ - lib/jekyll/action-network/utils.rb
133
136
  - lib/jekyll/action-network/version.rb
134
137
  homepage: https://github.com/joe-irving/jekyll-action-network/
135
138
  licenses: