Class: Birdwatcher::KloutClient

Inherits:
HttpClient show all
Defined in:
lib/birdwatcher/klout_client.rb

Constant Summary

Constants inherited from HttpClient

HttpClient::DEFAULT_RETRIES, HttpClient::DEFAULT_TIMEOUT, HttpClient::RETRIABLE_EXCEPTIONS, HttpClient::USER_AGENTS

Instance Method Summary collapse

Methods inherited from HttpClient

#do_delete, #do_get, #do_head, #do_post, #do_put

Constructor Details

#initialize(api_key, options = {}) ⇒ KloutClient

Class initializer

Parameters:

  • api_key (String)

    Klout API key

  • options (defaults to: {})

    Http client options

See Also:



10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#get_id(screen_name) ⇒ String

Get Klout ID of a Twitter user

Parameters:

  • screen_name (String)

    Twitter screen name

Returns:

  • (String)

    Klout ID or nil

See Also:



25
26
27
28
29
30
# 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) ⇒ Hash

Get Klout influence graph of a user

Parameters:

  • klout_id (String)

Returns:

  • (Hash)

    :influencers: contains screen names of influencers, :influencees contains screen names of influencees

See Also:



61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Numeric

Get Klout score of a user

Parameters:

  • klout_id (String)

Returns:

  • (Numeric)

    Klout score or nil

See Also:



37
38
39
40
41
42
# 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) ⇒ Array

Get Klout topics of a user

Parameters:

  • klout_id (String)

Returns:

  • (Array)

    Topics

See Also:



49
50
51
52
53
54
# 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