Sha256: 80e739522ffae076854c8bbb6cf216b43a4e4b4a75b45948d9e0656bd700fcec

Contents?: true

Size: 611 Bytes

Versions: 3

Compression:

Stored size: 611 Bytes

Contents

module Convert
  module Converters

    # Convert twitter URL to embedded html
    def twitter(string, options = {})
      @regex = %r{(?<!href=")https://twitter\.com(/#!)?/[A-Za-z0-9_]{1,15}/status(es)?/\d+(/?)}

      string.gsub(@regex) do |match|
        params = {:url => match}.merge(options)

        uri = URI("https://api.twitter.com/1/statuses/oembed.json")
        uri.query = URI.encode_www_form(params)

        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true

        response = JSON.parse(http.get(uri.request_uri).body)
        response["html"]
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
convert-0.1.5 lib/converters/twitter.rb
convert-0.1.4 lib/converters/twitter.rb
convert-0.1.3 lib/converters/twitter.rb