Sha256: c6aa5bcc4e427f7ce614543c420219c386af3d61c18c356f611b5bc0c2ee799d

Contents?: true

Size: 1.01 KB

Versions: 22

Compression:

Stored size: 1.01 KB

Contents

module Onebox
  module Engine
    class StackExchangeOnebox
      include Engine
      include LayoutSupport
      include JSON

      def self.domains
        %w(stackexchange stackoverflow superuser serverfault askubuntu)
      end

      matches_regexp /^https?:\/\/(?:(?:(?<subsubdomain>\w*)\.)?(?<subdomain>\w*)\.)?(?<domain>#{domains.join('|')})\.com\/(?:questions|q)\/(?<question>\d*)/

      def always_https?
        uri.host.split('.').length <= 3
      end

      private

      def match
        @match ||= @url.match(@@matcher)
      end

      def url
        domain = URI(@url).host
        "https://api.stackexchange.com/2.1/questions/#{match[:question]}?site=#{domain}"
      end

      def data
        return @data if @data

        result = raw['items'][0]
        if result
          result['creation_date'] =
            Time.at(result['creation_date'].to_i).strftime("%I:%M%p - %d %b %y")

          result['tags'] = result['tags'].take(4).join(', ')
        end
        @data = result
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
onebox-1.5.23 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.22 lib/onebox/engine/stack_exchange_onebox.rb