Sha256: ec0c5a30ab87eeea2bd27e89d9f3824f7001461dbb99c52116c9802cee06376a

Contents?: true

Size: 642 Bytes

Versions: 2

Compression:

Stored size: 642 Bytes

Contents

module Pixy
  class Shorten
    attr_accessor :status, :long_url, :short_url, :counter

    def initialize(key, url)
      uri       = URI('http://p.tl/api/api_simple.php')
      uri.query = URI.encode_www_form({:key => key, :url => url})
      response  = Net::HTTP.get_response(uri)
      result    = JSON.parse(response.body)

      if response.code.to_i == 200
        self.status     = result['status']
        self.long_url   = result['long_url']
        self.short_url  = result['short_url']
        self.counter    = result['counter']
      else
        raise "Server responded with code #{response.code}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pixy-0.0.2 lib/pixy/shorten.rb
pixy-0.0.1 lib/pixy/shorten.rb