lib/onebox/engine/stack_exchange_onebox.rb in onebox-1.1.0 vs lib/onebox/engine/stack_exchange_onebox.rb in onebox-1.2.0

- old
+ new

@@ -1,25 +1,39 @@ module Onebox module Engine class StackExchangeOnebox include Engine - include HTML + include LayoutSupport + include JSON - matches do - http - domain("stackoverflow") - tld("com") - with("/questions/") + 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 - { - url: @url, - title: raw.css(".question-hyperlink").inner_text, - question: raw.css(".question .post-text p").first.inner_text - } + 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