Class: LinkedIn::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/linked_in/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctoken = LinkedIn.token, csecret = LinkedIn.secret, options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
# File 'lib/linked_in/client.rb', line 6

def initialize(ctoken=LinkedIn.token, csecret=LinkedIn.secret, options={})
  opts = { 
          :request_token_path => "/uas/oauth/requestToken",
          :access_token_path  => "/uas/oauth/accessToken",
          :authorize_path     => "/uas/oauth/authorize"
        }
  @ctoken, @csecret, @consumer_options = ctoken, csecret, opts.merge(options)
end

Instance Attribute Details

#consumer_optionsObject (readonly)

Returns the value of attribute consumer_options.



4
5
6
# File 'lib/linked_in/client.rb', line 4

def consumer_options
  @consumer_options
end

#csecretObject (readonly)

Returns the value of attribute csecret.



4
5
6
# File 'lib/linked_in/client.rb', line 4

def csecret
  @csecret
end

#ctokenObject (readonly)

Returns the value of attribute ctoken.



4
5
6
# File 'lib/linked_in/client.rb', line 4

def ctoken
  @ctoken
end

Instance Method Details

#access_tokenObject



39
40
41
# File 'lib/linked_in/client.rb', line 39

def access_token
  @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
end

#authorize_from_access(atoken, asecret) ⇒ Object



43
44
45
# File 'lib/linked_in/client.rb', line 43

def authorize_from_access(atoken, asecret)
  @atoken, @asecret = atoken, asecret
end

#authorize_from_request(rtoken, rsecret, verifier_or_pin) ⇒ Object

For web apps use params, for desktop apps, use the verifier is the pin that LinkedIn gives users.



33
34
35
36
37
# File 'lib/linked_in/client.rb', line 33

def authorize_from_request(rtoken, rsecret, verifier_or_pin)
  request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
  access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
  @atoken, @asecret = access_token.token, access_token.secret
end

#clear_statusObject



116
117
118
119
# File 'lib/linked_in/client.rb', line 116

def clear_status
  path = "/people/~/current-status"
  delete(path).code
end

#connections(options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/linked_in/client.rb', line 84

def connections(options={})
  path = "#{person_path(options)}/connections"
  
  unless options[:fields].nil?
    if options[:public] 
      path +=":public"
    else
      path +=":(#{options[:fields].map{|f| f.to_s.gsub("_","-")}.join(',')})"
    end
  end
  
  Connections.from_xml(get(path)).profiles
end

#consumerObject



15
16
17
# File 'lib/linked_in/client.rb', line 15

def consumer
  @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'https://api.linkedin.com'}.merge(consumer_options))
end

#current_statusObject



106
107
108
109
# File 'lib/linked_in/client.rb', line 106

def current_status
  path = "/people/~/current-status"
  Crack::XML.parse(get(path))['current_status']
end

#delete(path, options = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/linked_in/client.rb', line 61

def delete(path, options={})
  path = "/v1#{path}"
  response = access_token.delete(path, options)
  raise_errors(response)
  response
end

#get(path, options = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/linked_in/client.rb', line 47

def get(path, options={})
  path = "/v1#{path}"
  response = access_token.get(path, options)
  raise_errors(response)
  response.body
end

#network_statuses(options = {}) ⇒ Object



121
122
123
124
# File 'lib/linked_in/client.rb', line 121

def network_statuses(options={})
  options[:type] = 'STAT'
  network_updates(options)
end

#network_updates(options = {}) ⇒ Object



126
127
128
129
# File 'lib/linked_in/client.rb', line 126

def network_updates(options={})
  path = "/people/~/network"
  Network.from_xml(get(to_uri(path, options)))
end

#profile(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/linked_in/client.rb', line 69

def profile(options={})
  
  path = person_path(options)
  
  unless options[:fields].nil?
    if options[:public] 
      path +=":public"
    else
      path +=":(#{options[:fields].map{|f| f.to_s.gsub("_","-")}.join(',')})"
    end
  end
  
  Profile.from_xml(get(path))
end

#put(path, options = {}) ⇒ Object



54
55
56
57
58
59
# File 'lib/linked_in/client.rb', line 54

def put(path, options={})
  path = "/v1#{path}"
  response = access_token.put(path, options)
  raise_errors(response)
  response
end

#request_token(options = {}) ⇒ Object

Note: If using oauth with a web app, be sure to provide :oauth_callback. Options:

:oauth_callback => String, url that LinkedIn should redirect to


27
28
29
# File 'lib/linked_in/client.rb', line 27

def request_token(options={})
  @request_token ||= consumer.get_request_token(options)
end

#search(options = {}) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/linked_in/client.rb', line 98

def search(options={})
  path = "/people"
  options = {:keywords => options} if options.is_a?(String)
  options = format_options_for_query(options)
  
  People.from_xml(get(to_uri(path, options)))
end

#set_callback_url(url) ⇒ Object



19
20
21
22
# File 'lib/linked_in/client.rb', line 19

def set_callback_url(url)
  clear_request_token
  request_token(:oauth_callback => url)
end

#update_status(text) ⇒ Object



111
112
113
114
# File 'lib/linked_in/client.rb', line 111

def update_status(text)
  path = "/people/~/current-status"
  put(path, status_to_xml(text))
end

#write_fixture(path, filename) ⇒ Object

helpful in making authenticated calls and writing the raw xml to a fixture file



136
137
138
139
140
# File 'lib/linked_in/client.rb', line 136

def write_fixture(path, filename)
  file = File.new("test/fixtures/#{filename}", "w")
  file.puts(access_token.get(path).body)
  file.close
end