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.



11
12
13
14
15
16
17
18
# File 'lib/linked_in/client.rb', line 11

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)

TODO: @ developer.linkedin.com/docs/DOC-1061 && / DOC-1014 add in client.get(“/people/~:(im-accounts)”)

client.get("/people/~:(twitter-accounts)")
client.get("/people/~:(date-of-birth)")
client.get("/people/~:(main-address)")


9
10
11
# File 'lib/linked_in/client.rb', line 9

def consumer_options
  @consumer_options
end

#csecretObject (readonly)

TODO: @ developer.linkedin.com/docs/DOC-1061 && / DOC-1014 add in client.get(“/people/~:(im-accounts)”)

client.get("/people/~:(twitter-accounts)")
client.get("/people/~:(date-of-birth)")
client.get("/people/~:(main-address)")


9
10
11
# File 'lib/linked_in/client.rb', line 9

def csecret
  @csecret
end

#ctokenObject (readonly)

TODO: @ developer.linkedin.com/docs/DOC-1061 && / DOC-1014 add in client.get(“/people/~:(im-accounts)”)

client.get("/people/~:(twitter-accounts)")
client.get("/people/~:(date-of-birth)")
client.get("/people/~:(main-address)")


9
10
11
# File 'lib/linked_in/client.rb', line 9

def ctoken
  @ctoken
end

Instance Method Details

#access_tokenObject



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

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

#authorize_from_access(atoken, asecret) ⇒ Object



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

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.



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

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

#birthdateObject

usage:

dob = client.birthdate
  returns a birthdate object allowing access to day, month, year
  dob.to_date will return a DATE object


118
119
120
121
# File 'lib/linked_in/client.rb', line 118

def birthdate
  path = "/people/~:(date-of-birth)"
  Birthdate.from_xml(get(path))
end

#clear_statusObject



153
154
155
156
# File 'lib/linked_in/client.rb', line 153

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

#connections(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/linked_in/client.rb', line 99

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



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

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

#create_network_update(text) ⇒ Object



131
132
133
134
# File 'lib/linked_in/client.rb', line 131

def create_network_update(text)
  path = "/people/~/person-activities"
  post(path, network_update_to_xml(CGI.escapeHTML(text)), {"Content-Type" => "text/xml"})
end

#current_statusObject



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

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

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



76
77
78
79
80
81
# File 'lib/linked_in/client.rb', line 76

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

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



52
53
54
55
56
57
# File 'lib/linked_in/client.rb', line 52

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

#network_statuses(options = {}) ⇒ Object



216
217
218
219
# File 'lib/linked_in/client.rb', line 216

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

#network_updates(options = {}) ⇒ Object



221
222
223
224
# File 'lib/linked_in/client.rb', line 221

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

#post(path, body = '', options = {}) ⇒ Object



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

def post(path, body='', options={})
  path = "/v1#{path}"
  response = access_token.post(path, body, options)
  puts 'response:'
  puts response
  raise_errors(response)
  response
end

#profile(options = {}) ⇒ Object



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

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, body, options = {}) ⇒ Object



69
70
71
72
73
74
# File 'lib/linked_in/client.rb', line 69

def put(path, body, options={})
  path = "/v1#{path}"
  response = access_token.put(path, body, 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


32
33
34
# File 'lib/linked_in/client.rb', line 32

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

#search(options = {}) ⇒ Object



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

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

#send_invite(subject, body, recipient_paths) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/linked_in/client.rb', line 184

def send_invite(subject, body, recipient_paths)
  path = "/people/~/mailbox"

  message         = LinkedIn::InviteMessage.new
  message.subject = subject
  message.body    = body
  recipients      = LinkedIn::Recipients.new

  recipients.recipients = recipient_paths.map do |recipient_hash|
    email = recipient_hash[:email]
    first = recipient_hash[:first]
    last  = recipient_hash[:last]
    recipient             = LinkedIn::Recipient.new
    recipient.person      = LinkedIn::Person.new
    recipient.person.path = "\"/people/email=#{email}\""
    recipient.person.first_name = first
    recipient.person.last_name  = last
    recipient
  end

  message.recipients = recipients
  
  puts 'sending in send_message:'
  puts path
  puts message_to_xml(message)
  
  
  post(path, message_to_xml(message), { "Content-Type" => "text/xml" }).code
end

#send_message(subject, body, recipient_paths) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/linked_in/client.rb', line 158

def send_message(subject, body, recipient_paths)
  path = "/people/~/mailbox"

  message         = LinkedIn::Message.new
  message.subject = subject
  message.body    = body
  recipients      = LinkedIn::Recipients.new

  recipients.recipients = recipient_paths.map do |profile_path|
    recipient             = LinkedIn::Recipient.new
    recipient.person      = LinkedIn::Person.new
    recipient.person.path = "\"/people/#{profile_path}\""
    recipient
  end

  message.recipients = recipients
  
  puts 'sending in send_message:'
  puts path
  puts message_to_xml(message)
  
  
  post(path, message_to_xml(message), { "Content-Type" => "text/xml" }).code
end

#set_callback_url(url) ⇒ Object



24
25
26
27
# File 'lib/linked_in/client.rb', line 24

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

#update_comment(network_key, comment) ⇒ Object



148
149
150
151
# File 'lib/linked_in/client.rb', line 148

def update_comment(network_key, comment)
  path = "/people/~/network/updates/key=#{network_key}/update-comments"
  post(path,comment_to_xml(comment),{'Content-Type' => 'application/xml'})
end

#update_status(text) ⇒ Object

NOTE: this is a deprecated API. Please use the Share API,

which is not currently implemented


143
144
145
146
# File 'lib/linked_in/client.rb', line 143

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



228
229
230
231
232
# File 'lib/linked_in/client.rb', line 228

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