huginn_state_code_agent 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 70b1cc96a4c69a948feda15ec9e0cd9db959254b4c8191d0b9594297ebd29f43
4
+ data.tar.gz: a2d9440fd82d48f7b6d79b56280ffed66ff699c0cce9b38c271fb0214280e6bf
5
+ SHA512:
6
+ metadata.gz: f2f346e3a05041a3a5ff422008aae0129bf04b900291f8009656e18974f5152a7d13819f466a711af9d21f2f4d46c488143f3f6b72fcdec2b290924a75331d3e
7
+ data.tar.gz: e61d7e9ce25a610275dc3084661c9a53a86d69d95015b38d203f6b45aa75e1750e1c7d8bbcd998066dabb3140c2ab96b8214b493590d83c24a1d72227fcd54d1
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2020 Jacob Spizziri
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agents
4
+ class StateCodeAgent < Agent
5
+ include Carmen
6
+
7
+ default_schedule '12h'
8
+
9
+ can_dry_run!
10
+ default_schedule 'never'
11
+
12
+ description <<-MD
13
+ This is a lightweight agent for deriving the abbreviation for a state/province
14
+ given a country name and a region name. The logic of this agent is backed by Carmen
15
+
16
+ ## Agent Options
17
+ The following outlines the available options in this agent
18
+
19
+ * `output_mode` - not required ('clean' or 'merge', defaults to 'clean')
20
+ * `country` - The name of the country
21
+ * `region` - The name of the region
22
+
23
+
24
+ ## Event Status
25
+
26
+ ### 200 (Success)
27
+
28
+ `status: 200` indicates a true success. In this case, Carmen was able to
29
+ return a valid abbreviation with the data provided:
30
+
31
+ ```
32
+ {
33
+ status: 200,
34
+ derived_state_code: 'IL',
35
+ { ... }
36
+ }
37
+ ```
38
+
39
+ ### 404 (Not Found)
40
+
41
+ `status: 404` indicates a potential failure. In this case, Carmen was
42
+ unable to find an abreviation given the data provided.
43
+
44
+ In this scenario, the agent will return the original `region` value as the
45
+ `derived_state_code` for convenience. It is up to the end user to decide
46
+ if this result should be handled as an error:
47
+
48
+ ```
49
+ {
50
+ status: 404,
51
+ derived_state_code: 'Illinois';
52
+ { ... }
53
+ }
54
+ ```
55
+
56
+ ### 500 (Error)
57
+
58
+ `status: 500` indicates a true error. In this case, something has gone
59
+ wrong in the agent's process and we were unable to return a useful result:
60
+
61
+ ```
62
+ {
63
+ status: 500,
64
+ message: 'Failed to derive a state code with the data provided',
65
+ data: { country: 'United States', region: 'Illinois' },
66
+ }
67
+ ```
68
+
69
+ MD
70
+
71
+ def default_options
72
+ {
73
+ 'country' => '',
74
+ 'region' => '',
75
+ 'output_mode' => 'clean',
76
+ }
77
+ end
78
+
79
+ def validate_options
80
+ unless options['country'].present?
81
+ errors.add(:base, 'country is a required field')
82
+ end
83
+
84
+ unless options['region'].present?
85
+ errors.add(:base, 'region is a required field')
86
+ end
87
+
88
+ if options['output_mode'].present? && !options['output_mode'].to_s.include?('{') && !%[clean merge].include?(options['output_mode'].to_s)
89
+ errors.add(:base, "if provided, output_mode must be 'clean' or 'merge'")
90
+ end
91
+
92
+ end
93
+
94
+ def working?
95
+ received_event_without_error?
96
+ end
97
+
98
+ def check
99
+ handle interpolated(event.payload)['payload'].presence || {}
100
+ end
101
+
102
+ def receive(incoming_events)
103
+ incoming_events.each do |event|
104
+ handle(event)
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def handle(event)
111
+ log('------------------------------')
112
+ log(interpolated(event.payload))
113
+ log('------------------------------')
114
+ # Process agent options
115
+ new_event = interpolated(event.payload)['output_mode'].to_s == 'merge' ? event.payload : {}
116
+
117
+ begin
118
+ country = Country.named(interpolated(event.payload)['country'])
119
+ region = country.nil? ? nil : country.subregions.named(interpolated(event.payload)['region'])
120
+
121
+ status = region.nil? ? 404 : 200
122
+ state_code = region.nil? ? interpolated(event.payload)['region'] : region.code
123
+
124
+ create_event payload: new_event.merge(
125
+ status: status,
126
+ derived_state_code: state_code,
127
+ )
128
+
129
+ rescue e
130
+ create_event payload: new_event.merge(
131
+ status: 500,
132
+ message: e.message,
133
+ data: { country: country, region: interpolated(event.payload)['region'] },
134
+ )
135
+ end
136
+ end
137
+
138
+ end
139
+ end
@@ -0,0 +1,3 @@
1
+ require 'huginn_agent'
2
+
3
+ HuginnAgent.register 'huginn_state_code_agent/state_code_agent'
@@ -0,0 +1,15 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ spec_folder = File.expand_path(File.dirname(__FILE__))
5
+
6
+
7
+
8
+ describe Agents::StateCodeAgent do
9
+ before(:each) do
10
+ @valid_options = Agents::StateCodeAgent.new.default_options
11
+ @checker = Agents::StateCodeAgent.new(:name => "StateCodeAgent", :options => @valid_options)
12
+ @checker.user = users(:bob)
13
+ @checker.save!
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_state_code_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - 5 Stones
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A light weight agent that leverages the Carmen gem to derive a state
56
+ code given the name of a country and region.
57
+ email:
58
+ - it@weare5stones.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - lib/huginn_state_code_agent.rb
65
+ - lib/huginn_state_code_agent/state_code_agent.rb
66
+ - spec/state_code_agent_spec.rb
67
+ homepage: https://github.com/5-stones/huginn_state_code_agent
68
+ licenses: []
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Agent that derives a state code from a country & region name using Carmen.
89
+ test_files:
90
+ - spec/state_code_agent_spec.rb