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.43 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.42 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.41 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.40 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.39 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.38 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.37 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.36 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.35 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.34 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.33 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.32 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.31 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.30 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.29 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.28 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.27 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.26 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.25 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.24 lib/onebox/engine/stack_exchange_onebox.rb