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.5.21 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.20 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.19 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.18 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.17 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.16 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.14 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.13 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.12 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.11 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.10 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.9 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.8 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.7 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.6 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.5 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.3 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.2 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.1 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.5.0 lib/onebox/engine/stack_exchange_onebox.rb