lib/klout/api.rb in klout-1.0.1 vs lib/klout/api.rb in klout-2.0.0

- old
+ new

@@ -1,57 +1,69 @@ module Klout class API - # Blank slate - instance_methods.each do |m| - undef_method m unless m.to_s =~ /^__|object_id|method_missing|respond_to?|to_s|inspect/ + include HTTParty + format :plain + default_timeout 30 + + attr_accessor :api_key, :timeout + + # == Usage + # + # Initialize with your Klout API key: + # + # k = Klout::API.new('api-key') + # + # Or you can set the +ENV['KLOUT_API_KEY']+ environment variable: + # + # k = Klout::API.new + # + def initialize(api_key = nil) + @api_key = api_key || ENV['KLOUT_API_KEY'] || raise(ArgumentError) end - # Initialize - def initialize(api_key, config = {}) - defaults = { - :format => 'json', - :secure => false - } - @config = defaults.merge(config).freeze - @api_key = api_key - protocol = @config[:secure] ? 'https' : 'http' - api_base = URI.parse("#{protocol}://api.klout.com/") - @klout_api = Net::HTTP.new(api_base.host, api_base.port) + def base_api_url # :nodoc: + "http://api.klout.com/v2" end - # Class Methods - def call(api_method, usernames) - path = '/1' - path += '/users' if ['show', 'topics'].include?(api_method.to_s) - path += '/soi' if ['influenced_by', 'influencer_of'].include?(api_method.to_s) - path += "/#{api_method}.#{@config[:format]}" - path += "?key=#{@api_key}" - path += "&users=#{URI.escape(usernames.gsub(/ /,''))}" - url = Net::HTTP::Get.new(path) - response = @klout_api.request(url) - # TODO: Clean this up when Klout fixes their ambiguous 404 responses. - raise APIError.new(response.code, response.message) if response.code.to_i > 202 - return nil if response.body == '' - @config[:format] == 'xml' ? XmlSimple.xml_in(response.body, {'ForceArray' => false}) : JSON.parse(response.body) + # === Identity + # + # Use the +identity+ method to get a user's +klout_id+. + # Pass either a numerical Twitter ID (Integer) or a Twitter screen name (String) + # + # k.identity(500042487) + # k.identity('dhh') + # + # You can also select a different network: + # + # k.identity('dhh', :ks) + # + def identity(id, network = :tw) + path = id.is_a?(Integer) ? "#{network}/#{id}?key=#{@api_key}" : "twitter?screenName=#{id}&key=#{@api_key}" + call("/identity.json/#{path}") end - def method_missing(api_method, *args) # :nodoc: - call(api_method, *args) - # TODO: Need Klout to fix their ambiguous 404 responses. - #rescue Klout::APIError => error - #super if error.message == "<404> Not Found" + # === User + # + # Use the +user+ method to get information about a user. Pass in a trait + # option to get score, influence and topics data: + # + # k.user(635263) + # k.user(635263, :influence) + # k.user(635263, :score) + # k.user(635263, :topics) + # + def user(id, trait = nil) + lookup = trait.nil? ? '' : "/#{trait.to_s}" + call("/user.json/#{id}#{lookup}?key=#{@api_key}") end - def respond_to?(api_method) # :nodoc: - call(api_method, 'twitter') - rescue Klout::APIError => error - error.message == "<404> Not Found" ? false : true + protected + + def call(endpoint) # :nodoc: + response = self.class.get(base_api_url + endpoint) + response.code.to_i == 200 ? JSON.parse(response.body) : raise(API::Error.new("HTTP Response Code: #{response.code}")) end - end - - class APIError < StandardError - # Initialize - def initialize(code, message) - super "<#{code}> #{message}" + + class Error < StandardError end end end \ No newline at end of file