Sha256: 0b61d0ece570d8c35aa2ec76974c0bc9abcb3b3580e85419785dd12f197e319f

Contents?: true

Size: 1004 Bytes

Versions: 6

Compression:

Stored size: 1004 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 = match[:subsubdomain] || match[:subdomain] || match[:domain]
        "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

6 entries across 6 versions & 1 rubygems

Version Path
onebox-1.2.5 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.2.4 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.2.3 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.2.2 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.2.1 lib/onebox/engine/stack_exchange_onebox.rb
onebox-1.2.0 lib/onebox/engine/stack_exchange_onebox.rb