<commit_before>module Ruboty
  module PageSpeedInsights
    module Actions
      class PageSpeedInsights < Ruboty::Actions::Base
        def call
          url = message.match_data[:url].strip

          message.reply('Fetching PageSpeed Insights Result...')

          results = %i(
            desktop
            mobile
          ).each_with_object({}) do |strategy, hash|
            google_url = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=#{url}&strategy=#{strategy}&key=#{ENV['GOOGLE_TOKEN']}"
            data = JSON.parse(open(google_url, &:read))

            hash[strategy] = { score: data['ruleGroups']['SPEED']['score'] }.merge(data['pageStats'])
          end

          # for slack_rtm
          attachments = results.map do |strategy, scores|
            {
              color: strategy == :desktop ? '#64b639' : '#1bbee7',
              author_name: strategy.to_s.upcase,
              author_link: "https://developers.google.com/speed/pagespeed/insights/?hl=en&url=#{url}&tab=#{strategy}",
              fields: scores.map {|stats, score| { title: stats, value: score, short: true } },
              ts: Time.now.to_i
            }
          end

          message.reply("PageSpeed Insights Result #{results.map{|k, v| "#{k.capitalize}: #{v[:score]}"}.join(', ')}", attachments: attachments)
        end
      end
    end
  end
end
<commit_msg>Add URL check<commit_after>module Ruboty
  module PageSpeedInsights
    module Actions
      class PageSpeedInsights < Ruboty::Actions::Base
        def call
          url = message.match_data[:url].strip
          unless url =~ %r(https?://[\w/:%#\$&\?\(\)~\.=\+\-]+)
            message.reply("Invalid URL #{url}")
            return
          end

          message.reply('Fetching PageSpeed Insights Result...')

          results = %i(
            desktop
            mobile
          ).each_with_object({}) do |strategy, hash|
            google_url = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=#{url}&strategy=#{strategy}&key=#{ENV['GOOGLE_TOKEN']}"
            data = JSON.parse(open(google_url, &:read))

            hash[strategy] = { score: data['ruleGroups']['SPEED']['score'] }.merge(data['pageStats'])
          end

          # for slack_rtm
          attachments = results.map do |strategy, scores|
            {
              color: strategy == :desktop ? '#64b639' : '#1bbee7',
              author_name: strategy.to_s.upcase,
              author_link: "https://developers.google.com/speed/pagespeed/insights/?hl=en&url=#{url}&tab=#{strategy}",
              fields: scores.map {|stats, score| { title: stats, value: score, short: true } },
              ts: Time.now.to_i
            }
          end

          message.reply("PageSpeed Insights Result #{results.map{|k, v| "#{k.capitalize}: #{v[:score]}"}.join(', ')}", attachments: attachments)
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.1526297696803025, 'fraction_numerical': 0.005500171880371262, 'mean_word_length': 2.2048458149779737, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2767468', 'n_tokens_mistral': 895, 'n_tokens_neox': 865, 'n_words': 166}