class Birdwatcher::KloutClient

Public Class Methods

new(api_key, options = {}) click to toggle source

Class initializer

@param api_key [String] Klout API key @param options Http client options @see Birdwatcher::HttpClient

# File lib/birdwatcher/klout_client.rb, line 10
def initialize(api_key, options = {})
  @api_key = api_key
  @options = {
    :headers => {
      "User-Agent" => "Birdwatcher v#{Birdwatcher::VERSION}",
      "Accept"     => "application/json"
    }
  }.merge(options)
end

Public Instance Methods

get_id(screen_name) click to toggle source

Get Klout ID of a Twitter user

@param screen_name [String] Twitter screen name @return [String] Klout ID or nil @see klout.com/s/developers/v2#identities

# File lib/birdwatcher/klout_client.rb, line 25
def get_id(screen_name)
  response = do_get("/identity.json/twitter?screenName=#{url_encode(screen_name)}&key=#{url_encode(@api_key)}")
  if response.status == 200
    JSON.parse(response.body)["id"]
  end
end
get_influence(klout_id) click to toggle source

Get Klout influence graph of a user

@param klout_id [String] @return [Hash] :influencers: contains screen names of influencers, :influencees contains screen names of influencees @see klout.com/s/developers/v2#influence

# File lib/birdwatcher/klout_client.rb, line 61
def get_influence(klout_id)
  response = do_get("/user.json/#{klout_id}/influence?key=#{url_encode(@api_key)}")
  if response.status == 200
    body = JSON.parse(response.body)
    {
      :influencers => body["myInfluencers"].map { |i| i["entity"]["payload"]["nick"] },
      :influencees => body["myInfluencees"].map { |i| i["entity"]["payload"]["nick"] }
    }
  end
end
get_score(klout_id) click to toggle source

Get Klout score of a user

@param klout_id [String] @return [Numeric] Klout score or nil @see klout.com/s/developers/v2#scores

# File lib/birdwatcher/klout_client.rb, line 37
def get_score(klout_id)
  response = do_get("/user.json/#{klout_id}/score?key=#{url_encode(@api_key)}")
  if response.status == 200
    JSON.parse(response.body)["score"]
  end
end
get_topics(klout_id) click to toggle source

Get Klout topics of a user

@param klout_id [String] @return [Array] Topics @see klout.com/s/developers/v2#topic

# File lib/birdwatcher/klout_client.rb, line 49
def get_topics(klout_id)
  response = do_get("/user.json/#{klout_id}/topics?key=#{url_encode(@api_key)}")
  if response.status == 200
    JSON.parse(response.body).map { |t| t["displayName"] }
  end
end

Private Instance Methods

url_encode(string) click to toggle source

URL encode a string @private

@param string [String] @return [String] URL encoded string

# File lib/birdwatcher/klout_client.rb, line 79
def url_encode(string)
  CGI.escape(string.to_s)
end