linkedin-idkmybffjill 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/Gemfile +16 -0
  2. data/LICENSE +20 -0
  3. data/README.markdown +74 -0
  4. data/Rakefile +70 -0
  5. data/VERSION +1 -0
  6. data/examples/authenticate.rb +21 -0
  7. data/examples/network.rb +12 -0
  8. data/examples/profile.rb +14 -0
  9. data/examples/status.rb +9 -0
  10. data/lib/linked_in/api_standard_profile_request.rb +17 -0
  11. data/lib/linked_in/base.rb +13 -0
  12. data/lib/linked_in/birthdate.rb +21 -0
  13. data/lib/linked_in/client.rb +267 -0
  14. data/lib/linked_in/company.rb +11 -0
  15. data/lib/linked_in/connections.rb +19 -0
  16. data/lib/linked_in/country.rb +9 -0
  17. data/lib/linked_in/education.rb +44 -0
  18. data/lib/linked_in/error.rb +21 -0
  19. data/lib/linked_in/group.rb +35 -0
  20. data/lib/linked_in/location.rb +13 -0
  21. data/lib/linked_in/message.rb +31 -0
  22. data/lib/linked_in/network.rb +15 -0
  23. data/lib/linked_in/people.rb +21 -0
  24. data/lib/linked_in/person.rb +7 -0
  25. data/lib/linked_in/position.rb +49 -0
  26. data/lib/linked_in/profile.rb +54 -0
  27. data/lib/linked_in/recipient.rb +7 -0
  28. data/lib/linked_in/recipients.rb +14 -0
  29. data/lib/linked_in/update.rb +19 -0
  30. data/lib/linked_in/url_resource.rb +29 -0
  31. data/lib/linkedin.rb +97 -0
  32. data/test/client_test.rb +179 -0
  33. data/test/fixtures/blank.xml +0 -0
  34. data/test/fixtures/connections.xml +3733 -0
  35. data/test/fixtures/error.xml +7 -0
  36. data/test/fixtures/mailbox_items.xml +13 -0
  37. data/test/fixtures/network_status_with_group.xml +44 -0
  38. data/test/fixtures/network_statuses.xml +299 -0
  39. data/test/fixtures/picture_updates.xml +117 -0
  40. data/test/fixtures/profile.xml +9 -0
  41. data/test/fixtures/profile_full.xml +3849 -0
  42. data/test/fixtures/profile_with_positions.xml +79 -0
  43. data/test/fixtures/search.xml +538 -0
  44. data/test/fixtures/status.xml +2 -0
  45. data/test/oauth_test.rb +117 -0
  46. data/test/test_helper.rb +54 -0
  47. metadata +214 -0
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source :gemcutter
2
+
3
+ gem 'oauth', '~> 0.3.5'
4
+ gem 'nokogiri'
5
+ gem 'crack', '>= 0.1.4'
6
+
7
+ group :development do
8
+ gem 'jeweler'
9
+ gem 'rake'
10
+ gem 'test-unit'
11
+ # gem 'redgreen'
12
+ gem 'shoulda'
13
+ gem 'jnunemaker-matchy', "= 0.4.0", :require => 'matchy'
14
+ gem 'mocha'
15
+ gem 'fakeweb', ">= 1.2.5"
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Wynn Netherland
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ # LinkedIn
2
+
3
+ Ruby wrapper for the [LinkedIn API](http://developer.linkedin.com). Heavily inspired by [John Nunemaker's](http://github.com/jnunemaker) [Twitter gem](http://github.com/jnunemaker/twitter), the LinkedIn gem provides an easy-to-use wrapper for LinkedIn's Oauth/XML APIs.
4
+
5
+ ## Installation
6
+
7
+ sudo gem install gemcutter
8
+ gem tumble
9
+ sudo gem install linkedin
10
+
11
+ ## Usage
12
+
13
+ ### Authenticate
14
+
15
+ LinkedIn's API uses Oauth for authentication. Luckily, the LinkedIn gem hides most of the gory details from you.
16
+
17
+ require 'rubygems'
18
+ require 'linkedin'
19
+
20
+ # get your api keys at https://www.linkedin.com/secure/developer
21
+ client = LinkedIn::Client.new('your_consumer_key', 'your_consumer_secret')
22
+ rtoken = client.request_token.token
23
+ rsecret = client.request_token.secret
24
+
25
+ # to test from your desktop, open the following url in your browser
26
+ # and record the pin it gives you
27
+ client.request_token.authorize_url
28
+ => "https://api.linkedin.com/uas/oauth/authorize?oauth_token=<generated_token>"
29
+
30
+ # then fetch your access keys
31
+ client.authorize_from_request(rtoken, rsecret, pin)
32
+ => ["OU812", "8675309"] # <= save these for future requests
33
+
34
+ # or authorize from previously fetched access keys
35
+ c.authorize_from_access("OU812", "8675309")
36
+
37
+ # you're now free to move about the cabin, call any API method
38
+
39
+ ### Profile examples
40
+
41
+ # get the profile for the authenticated user
42
+ client.profile
43
+
44
+ # get a profile for someone found in network via ID
45
+ client.profile(:id => 'gNma67_AdI')
46
+
47
+ # get a profile for someone via their public profile url
48
+ client.profile(:url => 'http://www.linkedin.com/in/netherland')
49
+
50
+
51
+
52
+ More examples in the [examples folder](http://github.com/pengwynn/linkedin/blob/master/examples).
53
+
54
+ For a nice example on using this in a [Rails App](http://pivotallabs.com/users/will/blog/articles/1096-linkedin-gem-for-a-web-app).
55
+
56
+
57
+ ## TODO
58
+
59
+ * Implement Messaging APIs
60
+
61
+ ## Note on Patches/Pull Requests
62
+
63
+ * Fork the project.
64
+ * Make your feature addition or bug fix.
65
+ * Add tests for it. This is important so I don't break it in a
66
+ future version unintentionally.
67
+ * Commit, do not mess with rakefile, version, or history.
68
+ (if you want to have your own version, that is fine but
69
+ bump version in a commit by itself I can ignore when I pull)
70
+ * Send me a pull request. Bonus points for topic branches.
71
+
72
+ ## Copyright
73
+
74
+ Copyright (c) 2009 [Wynn Netherland](http://wynnnetherland.com). See LICENSE for details.
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "linkedin-idkmybffjill"
8
+ gem.summary = %Q{Ruby wrapper for the LinkedIn API}
9
+ gem.description = %Q{Ruby wrapper for the LinkedIn API}
10
+ gem.email = "wynn.netherland@gmail.com"
11
+ gem.homepage = "http://github.com/idkmybffjill/linkedin"
12
+ gem.authors = ["Wynn Netherland, idkmybffjill"]
13
+ gem.files = FileList["[A-Z]*", "{lib,test}/**/*"]
14
+
15
+
16
+ gem.add_dependency('oauth', '~> 0.3.5')
17
+
18
+ # roxml dependency removed
19
+ # gem.add_dependency('roxml', '~> 3.1.3')
20
+
21
+ gem.add_dependency('crack', '~> 0.1.4')
22
+
23
+ # updated gem dependency to shoulda
24
+ # gem.add_development_dependency('thoughtbot-shoulda', '>= 2.10.1')
25
+ gem.add_development_dependency('shoulda', '>= 2.10.1')
26
+
27
+ gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
28
+ gem.add_development_dependency('mocha', '>= 0.9.4')
29
+ gem.add_development_dependency('fakeweb', '>= 1.2.5')
30
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
31
+ end
32
+ Jeweler::GemcutterTasks.new
33
+ rescue LoadError
34
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
35
+ end
36
+
37
+ require 'rake/testtask'
38
+ Rake::TestTask.new(:test) do |test|
39
+ test.libs << 'test'
40
+ test.ruby_opts << '-rubygems'
41
+ test.pattern = 'test/**/*_test.rb'
42
+ test.verbose = true
43
+ end
44
+
45
+ begin
46
+ require 'rcov/rcovtask'
47
+ Rcov::RcovTask.new do |test|
48
+ test.libs << 'test'
49
+ test.pattern = 'test/**/test_*.rb'
50
+ test.verbose = true
51
+ end
52
+ rescue LoadError
53
+ task :rcov do
54
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
55
+ end
56
+ end
57
+
58
+ task :test => :check_dependencies
59
+
60
+ task :default => :test
61
+
62
+ require 'rake/rdoctask'
63
+ Rake::RDocTask.new do |rdoc|
64
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
65
+
66
+ rdoc.rdoc_dir = 'rdoc'
67
+ rdoc.title = "linkedin #{version}"
68
+ rdoc.rdoc_files.include('README*')
69
+ rdoc.rdoc_files.include('lib/**/*.rb')
70
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.9
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'linkedin'
3
+
4
+ # get your api keys at https://www.linkedin.com/secure/developer
5
+ client = LinkedIn::Client.new('your_consumer_key', 'your_consumer_secret')
6
+ rtoken = client.request_token.token
7
+ rsecret = client.request_token.secret
8
+
9
+ # to test from your desktop, open the following url in your browser
10
+ # and record the pin it gives you
11
+ client.request_token.authorize_url
12
+ => "https://api.linkedin.com/uas/oauth/authorize?oauth_token=<generated_token>"
13
+
14
+ # then fetch your access keys
15
+ client.authorize_from_request(rtoken, rsecret, pin)
16
+ => ["OU812", "8675309"] # <= save these for future requests
17
+
18
+ # or authorize from previously fetched access keys
19
+ c.authorize_from_access("OU812", "8675309")
20
+
21
+ # you're now free to move about the cabin, call any API method
@@ -0,0 +1,12 @@
1
+ # AUTHENTICATE FIRST found in examples/authenticate.rb
2
+
3
+ # client is a LinkedIn::Client
4
+
5
+ # get network updates for the authenticated user
6
+ client.network_updates
7
+
8
+ # get profile picture changes
9
+ client.network_updates(:type => 'PICT')
10
+
11
+ # view connections for the currently authenticated user
12
+ client.connections
@@ -0,0 +1,14 @@
1
+ # AUTHENTICATE FIRST found in examples/authenticate.rb
2
+
3
+ # client is a LinkedIn::Client
4
+
5
+ # get the profile for the authenticated user
6
+ client.profile
7
+
8
+ # get a profile for someone found in network via ID
9
+ client.profile(:id => 'gNma67_AdI')
10
+
11
+ # get a profile for someone via their public profile url
12
+ client.profile(:url => 'http://www.linkedin.com/in/netherland')
13
+
14
+
@@ -0,0 +1,9 @@
1
+ # AUTHENTICATE FIRST found in examples/authenticate.rb
2
+
3
+ # client is a LinkedIn::Client
4
+
5
+ # update status for the authenticated user
6
+ client.update_status('is playing with the LinkedIn Ruby gem')
7
+
8
+ # clear status for the currently logged in user
9
+ client.clear_status
@@ -0,0 +1,17 @@
1
+ module LinkedIn
2
+ class ApiStandardProfileRequest < LinkedIn::Base
3
+
4
+ def url
5
+ @doc.xpath("//api-standard-profile-request/url").text
6
+ end
7
+
8
+ # returning a hash should be ok, but suggestions are welcome
9
+ def headers
10
+ hash = {}
11
+ hash[:name] = @doc.xpath("//api-standard-profile-request/headers/http-header/name").text
12
+ hash[:value] = @doc.xpath("//api-standard-profile-request/headers/http-header/value").text
13
+ hash
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module LinkedIn
2
+ class Base
3
+
4
+ def self.from_xml(doc)
5
+ new(Nokogiri::XML(doc))
6
+ end
7
+
8
+ def initialize(doc)
9
+ @doc = doc
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module LinkedIn
2
+ class Birthdate < LinkedIn::Base
3
+
4
+ def year
5
+ @year ||= @doc.xpath("/person/date-of-birth/year").text.to_i
6
+ end
7
+
8
+ def day
9
+ @day ||= @doc.xpath("/person/date-of-birth/day").text.to_i
10
+ end
11
+
12
+ def month
13
+ @month ||= @doc.xpath("/person/date-of-birth/month").text.to_i
14
+ end
15
+
16
+ def to_date
17
+ Date.civil(y=year,m=month,d=day)
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,267 @@
1
+ module LinkedIn
2
+ class Client
3
+
4
+ # TODO: @ http://developer.linkedin.com/docs/DOC-1061 && / DOC-1014
5
+ # add in client.get("/people/~:(im-accounts)")
6
+ # client.get("/people/~:(twitter-accounts)")
7
+ # client.get("/people/~:(date-of-birth)")
8
+ # client.get("/people/~:(main-address)")
9
+ attr_reader :ctoken, :csecret, :consumer_options
10
+
11
+ def initialize(ctoken=LinkedIn.token, csecret=LinkedIn.secret, options={})
12
+ opts = {
13
+ :request_token_path => "/uas/oauth/requestToken",
14
+ :access_token_path => "/uas/oauth/accessToken",
15
+ :authorize_path => "/uas/oauth/authorize"
16
+ }
17
+ @ctoken, @csecret, @consumer_options = ctoken, csecret, opts.merge(options)
18
+ end
19
+
20
+ def consumer
21
+ @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'https://api.linkedin.com'}.merge(@consumer_options))
22
+ end
23
+
24
+ def set_callback_url(url)
25
+ clear_request_token
26
+ request_token(:oauth_callback => url)
27
+ end
28
+
29
+ # Note: If using oauth with a web app, be sure to provide :oauth_callback.
30
+ # Options:
31
+ # :oauth_callback => String, url that LinkedIn should redirect to
32
+ def request_token(options={})
33
+ @request_token ||= consumer.get_request_token(options)
34
+ end
35
+
36
+ # For web apps use params[:oauth_verifier], for desktop apps,
37
+ # use the verifier is the pin that LinkedIn gives users.
38
+ def authorize_from_request(rtoken, rsecret, verifier_or_pin)
39
+ request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
40
+ access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
41
+ @atoken, @asecret = access_token.token, access_token.secret
42
+ end
43
+
44
+ def access_token
45
+ @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
46
+ end
47
+
48
+ def authorize_from_access(atoken, asecret)
49
+ @atoken, @asecret = atoken, asecret
50
+ end
51
+
52
+ def get(path, options={})
53
+ path = "/v1#{path}"
54
+ response = access_token.get(path, options)
55
+ raise_errors(response)
56
+ response.body
57
+ end
58
+
59
+
60
+ def post(path, body='', options={})
61
+ path = "/v1#{path}"
62
+ response = access_token.post(path, body, options)
63
+ raise_errors(response)
64
+ response
65
+ end
66
+
67
+ def put(path, body, options={})
68
+ path = "/v1#{path}"
69
+ response = access_token.put(path, body, options)
70
+ raise_errors(response)
71
+ response
72
+ end
73
+
74
+ def delete(path, options={})
75
+ path = "/v1#{path}"
76
+ response = access_token.delete(path, options)
77
+ raise_errors(response)
78
+ response
79
+ end
80
+
81
+
82
+ def profile(options={})
83
+
84
+ path = person_path(options)
85
+
86
+ unless options[:fields].nil?
87
+ if options[:public]
88
+ path +=":public"
89
+ else
90
+ path +=":(#{options[:fields].map{|f| f.to_s.gsub("_","-")}.join(',')})"
91
+ end
92
+ end
93
+
94
+ Profile.from_xml(get(path))
95
+ end
96
+
97
+ def connections(options={})
98
+ path = "#{person_path(options)}/connections"
99
+
100
+ unless options[:fields].nil?
101
+ if options[:public]
102
+ path +=":public"
103
+ else
104
+ path +=":(#{options[:fields].map{|f| f.to_s.gsub("_","-")}.join(',')})"
105
+ end
106
+ end
107
+
108
+ Connections.from_xml(get(path)).profiles
109
+ end
110
+
111
+ ##
112
+ # usage:
113
+ # dob = client.birthdate
114
+ # returns a birthdate object allowing access to day, month, year
115
+ # dob.to_date will return a DATE object
116
+ def birthdate
117
+ path = "/people/~:(date-of-birth)"
118
+ Birthdate.from_xml(get(path))
119
+ end
120
+
121
+ def search(options={})
122
+ path = "/people"
123
+ options = {:keywords => options} if options.is_a?(String)
124
+ options = format_options_for_query(options)
125
+
126
+ People.from_xml(get(to_uri(path, options)))
127
+ end
128
+
129
+ def current_status
130
+ path = "/people/~/current-status"
131
+ Crack::XML.parse(get(path))['current_status']
132
+ end
133
+
134
+ def update_status(text)
135
+ path = "/people/~/current-status"
136
+ put(path, status_to_xml(text))
137
+ end
138
+
139
+ def update_comment(network_key, comment)
140
+ path = "/people/~/network/updates/key=#{network_key}/update-comments"
141
+ post(path,comment_to_xml(comment),{'Content-Type' => 'application/xml'})
142
+ end
143
+
144
+ def clear_status
145
+ path = "/people/~/current-status"
146
+ delete(path).code
147
+ end
148
+
149
+ def send_message(subject, body, recipient_paths)
150
+ path = "/people/~/mailbox"
151
+
152
+ message = LinkedIn::Message.new
153
+ message.subject = subject
154
+ message.body = body
155
+ recipients = LinkedIn::Recipients.new
156
+
157
+ recipients.recipients = recipient_paths.map do |profile_path|
158
+ recipient = LinkedIn::Recipient.new
159
+ recipient.person = LinkedIn::Person.new
160
+ recipient.person.path = "/people/#{profile_path}"
161
+ recipient
162
+ end
163
+
164
+ message.recipients = recipients
165
+ post(path, message_to_xml(message), { "Content-Type" => "text/xml" }).code
166
+ end
167
+
168
+ def network_statuses(options={})
169
+ options[:type] = 'STAT'
170
+ network_updates(options)
171
+ end
172
+
173
+ def network_updates(options={})
174
+ path = "/people/~/network"
175
+ Network.from_xml(get(to_uri(path, options)))
176
+ end
177
+
178
+ # helpful in making authenticated calls and writing the
179
+ # raw xml to a fixture file
180
+ def write_fixture(path, filename)
181
+ file = File.new("test/fixtures/#{filename}", "w")
182
+ file.puts(access_token.get(path).body)
183
+ file.close
184
+ end
185
+
186
+ private
187
+ def clear_request_token
188
+ @request_token = nil
189
+ end
190
+
191
+ def raise_errors(response)
192
+ # Even if the XML answer contains the HTTP status code, LinkedIn also sets this code
193
+ # in the HTTP answer (thankfully).
194
+ case response.code.to_i
195
+ when 400
196
+ data = LinkedIn::Error.from_xml(response.body)
197
+ raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data.code if data}"
198
+ when 401
199
+ data = LinkedIn::Error.from_xml(response.body)
200
+ raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data.code if data}"
201
+ when 403
202
+ data = LinkedIn::Error.from_xml(response.body)
203
+ raise General.new(data), "(#{response.code}): #{response.message} - #{data.code if data}"
204
+ when 404
205
+ raise NotFound, "(#{response.code}): #{response.message}"
206
+ when 500
207
+ raise InformLinkedIn, "LinkedIn had an internal error. Please let them know in the forum. (#{response.code}): #{response.message}"
208
+ when 502..503
209
+ raise Unavailable, "(#{response.code}): #{response.message}"
210
+ end
211
+ end
212
+
213
+ def format_options_for_query(opts)
214
+ opts.keys.each do |key|
215
+ value = opts.delete(key)
216
+ value = value.join("+") if value.is_a?(Array)
217
+ value = value.gsub(" ", "+") if value.is_a?(String)
218
+ opts[key.to_s.gsub("_","-")] = value
219
+ end
220
+ opts
221
+ end
222
+
223
+ def to_query(options)
224
+ options.inject([]) do |collection, opt|
225
+ collection << "#{opt[0]}=#{opt[1]}"
226
+ collection
227
+ end * '&'
228
+ end
229
+
230
+ def to_uri(path, options)
231
+ uri = URI.parse(path)
232
+
233
+ if options && options != {}
234
+ uri.query = to_query(options)
235
+ end
236
+ uri.to_s
237
+ end
238
+
239
+ def person_path(options)
240
+ path = "/people/"
241
+ if options[:id]
242
+ path += "id=#{options[:id]}"
243
+ elsif options[:email]
244
+ path += "email=#{options[:email]}"
245
+ elsif options[:url]
246
+ path += "url=#{CGI.escape(options[:url])}"
247
+ else
248
+ path += "~"
249
+ end
250
+ end
251
+
252
+ def status_to_xml(status)
253
+ %Q{<?xml version="1.0" encoding="UTF-8"?>
254
+ <current-status>#{status}</current-status>}
255
+ end
256
+
257
+ def comment_to_xml(comment)
258
+ %Q{<?xml version="1.0" encoding="UTF-8"?><update-comment><comment>#{comment}</comment></update-comment>}
259
+ end
260
+
261
+ def message_to_xml(message)
262
+ %Q{<?xml version="1.0" encoding="UTF-8"?>
263
+ #{message.to_xml}}
264
+ end
265
+
266
+ end
267
+ end