Reputation: 2900
I'm using jira-ruby gem to create an jira issue using my app. The ticket on Jira board should be created in one or two board depend on which action was triggered before. For example when new member was added to the repo it should create two tickets in two separate boards (support and security). I've to use client.Issue.build twice (this is a gem method) so I need to do something like:
def call
if SUPPORTBOARD_WEBHOOKS_CLASSES.include?(webhook.action_type_class)
issue = client.Issue.build
issue.save(support_ticket_class.new(webhook))
end
if SECURIY_WEBHOOKS_CLASSES.include?(webhook.action_type_class)
issue = client.Issue.build
issue.save(security_ticket_class.new(webhook))
end
end
Because of RSpec issues with stubbing client.Issue.build
(here is related topic) I want to extract to separate method below part from if bloc:
issue = client.Issue.build
issue.save(...)
How to pass support_ticket_class.new(webhook)
or security_ticket_class.new(webhook)
in issue.save()
depended of if block above and extract it to another method.
Upvotes: 0
Views: 145
Reputation: 114178
Seems like you have duplication in your code.
Why not create a hash that maps action_type_class
to the corresponding ticket class(es)? Something like this should work: (replace MAPPING
with a more descriptive name)
MAPPING = {
foo_action: [support_ticket_class, security_ticket_class],
bar_action: [support_ticket_class],
baz_action: [security_ticket_class]
}
You can then fetch the ticket classes for the action type and create the corresponding tickets in a loop:
def call
MAPPING.fetch(webhook.action_type_class, []).each do |ticket_class|
issue = client.Issue.build
issue.save(ticket_class.new(webhook))
end
end
Upvotes: 1
Reputation: 15985
create a method accepting ticket class and webhook
def create_issue(ticket_class, webhook)
issue = client.Issue.build
issue.save(ticket_class.new(webhook))
end
and then call this method passing ticket class and webhook
if SUPPORTBOARD_WEBHOOKS_CLASSES.include?(webhook.action_type_class)
create_issue(support_ticket_class, webhook)
end
if SECURIY_WEBHOOKS_CLASSES.include?(webhook.action_type_class)
create_issue(security_ticket_class, webhook)
end
Upvotes: 2