jekyll-action-network 0.3.2 → 0.4.0

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: fbd60e9684a0f7d773315ef6918aa6d556252241dfa9bf3d1522b9372f1efef2
4
- data.tar.gz: 483493faafafa23cc318fe7f1524d4957fe3460f3b64ffa274355937da00cc67
3
+ metadata.gz: 65a9afd8b2fcbcc33e2f1f45ea6d25a07acc6bcc28e5b7ac3900443a5c4078dc
4
+ data.tar.gz: ba9411be015b223ded854fec35a3fb2f39c9cdaa60ce9271b0cddb4c80511ed7
5
5
  SHA512:
6
- metadata.gz: 752c465b3ead4623aa1f0c37e0fe7fd41e0a9556ba893c8922e1a29012f969cf673a25703a6d3b43c2beab9d2d54568106b7de9590699a8f6c1d52dd615a29e1
7
- data.tar.gz: a4c144286814cce90c8fad0064eb8475ce4d60c28b0fe2e24d1ca7c4f39898b11fe65b7650be8146b5711f62ea58028f1ce06c6e2a314e1654295068e790c47e
6
+ metadata.gz: e00cde2d4c9d533931f046f11b7272bfa33dfedebd94b26141d721d7990ffb6d8be48be770ccac1ff378504a8856b958987c25ddd0d7e26c2607c76fa6709399
7
+ data.tar.gz: 4b0c39e1b928efce610feb7ba9733853c022a33d4a20eba190d6518f465777324152c6eba58a4294da562f2931ce39298bf4a524d1a4f7c948822f526a44ba1b
@@ -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,87 @@
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, client, config_init, settings)
12
+ @site = site
13
+ @settings = settings
14
+ @client = client
15
+ @config_init = config_init
16
+ end
17
+
18
+ def config
19
+ @name = @config_init["name"]
20
+ @defaults ||= @settings["defaults"]
21
+ @config ||= @defaults[@name].merge!(@config_init)
22
+ end
23
+
24
+ def collection
25
+ return @collection if @collection
26
+
27
+ Jekyll.logger.debug LOG_NAME, "Creating collection #{@name}"
28
+ @collection ||= @site.collections[@name] if @site.collections[@name]
29
+ @collection ||= Jekyll::Collection.new(@site, config["collection"])
30
+ end
31
+
32
+ def documents
33
+ documents = []
34
+ filtered_actions.each do |action_data|
35
+ action = Jekyll::ActionNetwork::Action.new(@site, @name, collection, config, action_data)
36
+ documents << action.doc
37
+ end
38
+ documents
39
+ end
40
+
41
+ def populate
42
+ collection.docs.concat(documents)
43
+ @collection
44
+ end
45
+
46
+ def actions
47
+ @actions ||= UTILS.get_full_list(@client, @name)
48
+ end
49
+
50
+ def filters
51
+ filters = @settings["filters"]["all"].merge(@settings["filters"][@name] || {})
52
+ filters.merge(config["filters"] || {})
53
+ end
54
+
55
+ def filtered_actions
56
+ public_actions = []
57
+ actions.each do |action|
58
+ next if action_filtered(action, filters)
59
+
60
+ public_actions << reduce_fields(action)
61
+ end
62
+ end
63
+
64
+ def action_filtered(action, filters)
65
+ filtered = false
66
+ filters.each do |key, value|
67
+ next if action[key] == value
68
+
69
+ next if value == "%" && action[key]
70
+
71
+ Jekyll.logger.debug @log_name, "#{name} filtered because #{action[key]} == #{value}"
72
+ filtered = true
73
+ break
74
+ end
75
+ filtered
76
+ end
77
+
78
+ def reduce_fields(action)
79
+ reduced_action = {}
80
+ @settings["fields"].each do |field|
81
+ reduced_action[field] = action[field] if action[field]
82
+ end
83
+ reduced_action
84
+ end
85
+ end
86
+ end
87
+ end
@@ -5,151 +5,90 @@ 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
41
18
 
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"] || {})
19
+ def generate(site)
20
+ return unless site.config["action_network"]
21
+
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
+ name = config["name"] if config.is_a? Hash
33
+ name ||= config if config.is_a? String
34
+ next unless name
35
+
36
+ make_collection(name, 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(name, config)
42
+ return unless defaults[name]
43
+
44
+ collection = Jekyll::ActionNetwork::Collection.new(@site, @client, config, settings)
45
+ @site.collections[collection.config["collection"]] = collection.populate
46
+ actions.docs.concat(collection.collection.docs)
47
+ end
86
48
 
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?
49
+ def actions
50
+ return @actions if @actions
93
51
 
94
- actions.concat(action_page)
95
- page += 1
52
+ actions_config = @config["actions"] if @config["actions"].is_a? Hash
53
+ actions_settings = @settings["actions"].merge(actions_config || {})
54
+ @actions ||= Jekyll::Collection.new(@site, actions_settings["collection"])
96
55
  end
97
- actions
98
- end
99
56
 
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
120
- end
121
- public << reduce_fields(action) unless filtered
57
+ def settings
58
+ return @settings if @settings
59
+
60
+ settings = YAML.load_file("#{File.expand_path(__dir__)}/settings.yaml")
61
+ @fields = settings["fields"]
62
+ @filters = settings["filters"]
63
+ @settings = settings
122
64
  end
123
- public
124
- end
125
65
 
126
- def reduce_fields(action)
127
- reduced_action = {}
128
- @fields.each do |field|
129
- reduced_action[field] = action[field] if action[field]
66
+ def defaults
67
+ @defaults ||= settings["defaults"]
130
68
  end
131
- reduced_action
132
- end
133
69
 
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"]
70
+ private
138
71
 
139
- key
140
- end
72
+ def api_key
73
+ key = @config["key"] || @settings["key"]
74
+ return ENV.fetch(key[4..key.length - 1], nil) if key[0..3] == "ENV_"
141
75
 
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
76
+ key
77
+ end
146
78
 
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
79
+ def authenticate
80
+ unless api_key
81
+ Jekyll.logger.warn "Action Network API Key not found."
82
+ return
83
+ end
84
+
85
+ @client = ActionNetworkRest.new(api_key: api_key)
86
+ unless @client.entry_point.authenticated_successfully?
87
+ Jekyll.logger.warn "Action Network Authentication Unsucessful"
88
+ return
89
+ end
90
+ true
151
91
  end
152
- true
153
92
  end
154
93
  end
155
94
  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,44 @@
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
+
30
+ def get_full_list(client, name)
31
+ page = 1
32
+ actions = []
33
+ loop do
34
+ action_page = client.send(name).list(page: page)
35
+ break if action_page.empty?
36
+
37
+ actions.concat(action_page)
38
+ page += 1
39
+ end
40
+ actions
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module ActionNetwork
5
- VERSION = "0.3.2"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll/action-network/version"
4
+ require "jekyll/action-network/utils"
5
+ require "jekyll/action-network/action"
6
+ require "jekyll/action-network/collection"
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.2
4
+ version: 0.4.0
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
@@ -127,10 +127,13 @@ executables: []
127
127
  extensions: []
128
128
  extra_rdoc_files: []
129
129
  files:
130
+ - lib/jekyll-action-network.rb
131
+ - lib/jekyll/action-network/action.rb
132
+ - lib/jekyll/action-network/collection.rb
130
133
  - lib/jekyll/action-network/generator.rb
131
134
  - lib/jekyll/action-network/settings.yaml
135
+ - lib/jekyll/action-network/utils.rb
132
136
  - lib/jekyll/action-network/version.rb
133
- - lib/jekyll_action_network.rb
134
137
  homepage: https://github.com/joe-irving/jekyll-action-network/
135
138
  licenses:
136
139
  - MIT
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "jekyll/action-network/version"
4
- require "jekyll/action-network/generator"