class SlackPomodoroTimer::HTTP
Constants
- REGEXES
Attributes
data[RW]
integration_type[R]
response[RW]
url[RW]
Public Class Methods
new(options={})
click to toggle source
Accepts options for url and data
# File lib/slack_pomodoro_timer/http.rb, line 21 def initialize(options={}) @url = options[:url] @data = options[:data] set_integration_type end
Private Class Methods
valid_url?(url)
click to toggle source
Validates that the URL fits the expected Slackbot or Webhook URL format
# File lib/slack_pomodoro_timer/http.rb, line 116 def self.valid_url?(url) REGEXES.any? { |key, regex| regex.match(url) } end
Public Instance Methods
post()
click to toggle source
Posts the data to the given URL
# File lib/slack_pomodoro_timer/http.rb, line 40 def post data = serialized_data url = url_for_integration_type @response = Net::HTTP.post_form(URI.parse(url), data) end
url=(value)
click to toggle source
Overrides default setter to allow internally setting the integration type based on the URL
# File lib/slack_pomodoro_timer/http.rb, line 32 def url=(value) @url = value set_integration_type @url end
Private Instance Methods
serialize_data_by_integration_type()
click to toggle source
Returns the data format that Slack expects for the integration type See:
https://api.slack.com/slackbot https://api.slack.com/incoming-webhooks
# File lib/slack_pomodoro_timer/http.rb, line 64 def serialize_data_by_integration_type if @integration_type == 'slackbot' serialize_slackbot_data else serialize_webhook_data end end
serialize_slackbot_data()
click to toggle source
Returns only the text of the original data as explain here
https://api.slack.com/slackbot
Slack expects only the raw text for this integration type
# File lib/slack_pomodoro_timer/http.rb, line 79 def serialize_slackbot_data @data[:text] end
serialize_webhook_data()
click to toggle source
Returns the original data serialized into JSON
# File lib/slack_pomodoro_timer/http.rb, line 86 def serialize_webhook_data @data.to_json end
serialized_data()
click to toggle source
Returns a hash with a payload key conforming to what Net::HTTP expects in it’s data parameter
# File lib/slack_pomodoro_timer/http.rb, line 51 def serialized_data { :payload => serialize_data_by_integration_type } end
set_integration_type()
click to toggle source
Sets the integration type based on the format of the URL
# File lib/slack_pomodoro_timer/http.rb, line 103 def set_integration_type if @url.match(REGEXES[:slackbot]) @integration_type = 'slackbot' elsif @url.match(REGEXES[:webhook]) @integration_type = 'webhook' else raise ArgumentError, "Slack URL is invalid" end end
url_for_integration_type()
click to toggle source
Returns the appropriate URL for the integration type
# File lib/slack_pomodoro_timer/http.rb, line 93 def url_for_integration_type if @integration_type == 'slackbot' url = @url url += "&channel=#{URI.encode(@data[:channel])}" end end