Sha256: 049fbe9d229a3a7a99b81e92eb5a067e3767ae927baf86ea81249d1681404ee7

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

require 'cgi'

begin
  require "em-synchrony/em-http"
  require "bitly"
rescue LoadError => error
  raise "Missing EM-Synchrony dependencies: gem install em-http-request; gem install bitly -v=0.5.0"
end

module Bitly
  module Utils
    def get_result(request)
      http = EventMachine::HttpRequest.new(request).get(:timeout => 100)

      result = if (http.response_header.status == 200)
        Crack::JSON.parse(http.response)
      else
        {'errorMessage' => 'JSON Parse Error(Bit.ly messed up)', 'errorCode' => 69, 'statusCode' => 'ERROR'}
      end

      if 'OK' == result['statusCode']
        result['results']
      else
        raise BitlyError.new(result['errorMessage'],result['errorCode'])
      end
    end
  end
  
  module V3
    class Client
      class << self
        def get(method, query)
          query_values=[]
          query[:query].each do |key, value|
            query_values << "#{key}=#{CGI::escape(value.to_s)}"
          end
          query_values=query_values.join('&')
          request=(method[0]=='/' ? "#{base_uri}#{method}" : method)
          request=(request.include?('?') ? "#{request}&#{query_values}" :  "#{request}?#{query_values}")
          
          http = EventMachine::HttpRequest.new(request).get(:timeout => 100)
          response = if (http.response_header.status == 200)
            Crack::JSON.parse(http.response)
          else
            {'errorMessage' => 'JSON Parse Error(Bit.ly messed up)', 'errorCode' => 69, 'statusCode' => 'ERROR'}
          end      
          
          if response['status_code'] == 200
            return response
          else
            raise BitlyError.new(response['status_txt'], response['status_code'])
          end
          
        end
      end
    end
  end	
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-synchrony-0.3.0.beta.1 lib/em-synchrony/em-bitly.rb
em-synchrony-0.2.0 lib/em-synchrony/em-bitly.rb