Sha256: d6b7e82408c9384cf65f732e685ac9c241a5f91609cb5ee1bf2bcf84cec65efe

Contents?: true

Size: 959 Bytes

Versions: 43

Compression:

Stored size: 959 Bytes

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 /^http:\/\/(?:(?:(?<subsubdomain>\w*)\.)?(?<subdomain>\w*)\.)?(?<domain>#{domains.join('|')})\.com\/(?:questions|q)\/(?<question>\d*)/

      private

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

      def url
        domain = URI(@url).host
        "http://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

43 entries across 43 versions & 1 rubygems

Version Path
onebox-1.4.9 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.8 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.7 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.5 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.4 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.3 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.2 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.1 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.4.0 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.9 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.8 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.7 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.6 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.5 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.4 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.3 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.2 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.1 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.3.0 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.2.9 lib/onebox/engine/stack_exchange_onebox.rb