<commit_before>require 'httparty'
require 'json'

class Messenger::Slack

  def self.valid_url?(url)
    !!matcher(url)
  end

  # URL format:
  #     slack://displayname@hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX/#room
  # Slack supports the notion of attachments, so the messages can be a little fancier. If body is a string, that
  # string will simply be sent to the room as text. `body` can also be an array of attachments, or a single attachment.
  # Attachment reference: https://api.slack.com/docs/attachments
  def self.deliver(url, body, options={})
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    parsed_url = matcher(url)

    options[:headers] ||= {}
    response = HTTParty.post(
      "https://#{parsed_url[:base_url]}/#{parsed_url[:key_one]}/#{parsed_url[:key_two]}/#{parsed_url[:secret]}",
      :headers => { "Content-Type" => "application/json"}.merge(options[:headers]),
      :body => build_message(parsed_url[:channel], parsed_url[:display_name], body)
    )
    Messenger::Result.new(success?(response), response)
  end

  def self.obfuscate(url)
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    parsed_url = matcher(url)
    "slack://#{parsed_url[:display_name]}@#{parsed_url[:base_url]}/T********/B********/************************/#{parsed_url[:channel]}"
  end


private

  def self.build_message(channel, display_name, body)
    msg = { channel: channel, username: display_name }<commit_msg>added ability to pass icon_url and icon_emoji via options<commit_after>require 'httparty'
require 'json'

class Messenger::Slack

  def self.valid_url?(url)
    !!matcher(url)
  end

  # URL format:
  #     slack://displayname@hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX/#room
  # Slack supports the notion of attachments, so the messages can be a little fancier. If body is a string, that
  # string will simply be sent to the room as text. `body` can also be an array of attachments, or a single attachment.
  # Attachment reference: https://api.slack.com/docs/attachments
  def self.deliver(url, body, options={})
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    parsed_url = matcher(url)

    options[:headers] ||= {}
    response = HTTParty.post(
      "https://#{parsed_url[:base_url]}/#{parsed_url[:key_one]}/#{parsed_url[:key_two]}/#{parsed_url[:secret]}",
      :headers => { "Content-Type" => "application/json"}.merge(options[:headers]),
      :body => build_message(parsed_url[:channel], parsed_url[:display_name], body, options)
    )
    Messenger::Result.new(success?(response), response)
  end

  def self.obfuscate(url)
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    parsed_url = matcher(url)
    "slack://#{parsed_url[:display_name]}@#{parsed_url[:base_url]}/T********/B********/************************/#{parsed_url[:channel]}"
  end


private

  def self.build_message(channel, display_name, body, options)
    msg = { channel: channel, username: display_name }

    msg['icon_emoji'] = options['icon_emoji'] if options['icon_emoji']
    msg['icon_url'] = options['icon_url'] if options['icon_url']
{'fraction_non_alphanumeric': 0.16251154201292706, 'fraction_numerical': 0.009849184364419821, 'mean_word_length': 4.803571428571429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1643020', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1046, 'n_words': 242}