holetse-linkedin 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
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,73 @@
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
+
55
+
56
+ ## TODO
57
+
58
+ * Implement Messaging APIs
59
+
60
+ ## Note on Patches/Pull Requests
61
+
62
+ * Fork the project.
63
+ * Make your feature addition or bug fix.
64
+ * Add tests for it. This is important so I don't break it in a
65
+ future version unintentionally.
66
+ * Commit, do not mess with rakefile, version, or history.
67
+ (if you want to have your own version, that is fine but
68
+ bump version in a commit by itself I can ignore when I pull)
69
+ * Send me a pull request. Bonus points for topic branches.
70
+
71
+ ## Copyright
72
+
73
+ Copyright (c) 2009 [Wynn Netherland](http://wynnnetherland.com). See LICENSE for details.
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "holetse-linkedin"
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/pengwynn/linkedin"
12
+ gem.authors = ["Wynn Netherland"]
13
+ gem.files = FileList["[A-Z]*", "{lib,test}/**/*"]
14
+
15
+
16
+ gem.add_dependency('oauth', '~> 0.3.5')
17
+ gem.add_dependency('roxml', '~> 3.1.3')
18
+ gem.add_dependency('crack', '~> 0.1.4')
19
+
20
+ gem.add_development_dependency('thoughtbot-shoulda', '>= 2.10.1')
21
+ gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
22
+ gem.add_development_dependency('mocha', '0.9.4')
23
+ gem.add_development_dependency('fakeweb', '>= 1.2.5')
24
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
25
+ end
26
+ Jeweler::GemcutterTasks.new
27
+ rescue LoadError
28
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
29
+ end
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'test'
34
+ test.ruby_opts << '-rubygems'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ begin
40
+ require 'rcov/rcovtask'
41
+ Rcov::RcovTask.new do |test|
42
+ test.libs << 'test'
43
+ test.pattern = 'test/**/test_*.rb'
44
+ test.verbose = true
45
+ end
46
+ rescue LoadError
47
+ task :rcov do
48
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
49
+ end
50
+ end
51
+
52
+ task :test => :check_dependencies
53
+
54
+ task :default => :test
55
+
56
+ require 'rake/rdoctask'
57
+ Rake::RDocTask.new do |rdoc|
58
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
59
+
60
+ rdoc.rdoc_dir = 'rdoc'
61
+ rdoc.title = "linkedin #{version}"
62
+ rdoc.rdoc_files.include('README*')
63
+ rdoc.rdoc_files.include('lib/**/*.rb')
64
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.8
@@ -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,9 @@
1
+ module LinkedIn
2
+ class ApiStandardProfileRequest
3
+ include ROXML
4
+ xml_convention {|val| val.gsub("_","-") }
5
+ xml_reader :url
6
+ xml_reader :headers, :as => {:key => 'name', :value => 'value'}, :in => 'headers/http-header'
7
+
8
+ end
9
+ end
@@ -0,0 +1,215 @@
1
+ module LinkedIn
2
+ class Client
3
+
4
+ attr_reader :ctoken, :csecret, :consumer_options
5
+
6
+ def initialize(ctoken=LinkedIn.token, csecret=LinkedIn.secret, options={})
7
+ opts = {
8
+ :request_token_path => "/uas/oauth/requestToken",
9
+ :access_token_path => "/uas/oauth/accessToken",
10
+ :authorize_path => "/uas/oauth/authorize"
11
+ }
12
+ @ctoken, @csecret, @consumer_options = ctoken, csecret, opts.merge(options)
13
+ end
14
+
15
+ def consumer
16
+ @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, {:site => 'https://api.linkedin.com'}.merge(consumer_options))
17
+ end
18
+
19
+ def set_callback_url(url)
20
+ clear_request_token
21
+ request_token(:oauth_callback => url)
22
+ end
23
+
24
+ # Note: If using oauth with a web app, be sure to provide :oauth_callback.
25
+ # Options:
26
+ # :oauth_callback => String, url that LinkedIn should redirect to
27
+ def request_token(options={})
28
+ @request_token ||= consumer.get_request_token(options)
29
+ end
30
+
31
+ # For web apps use params[:oauth_verifier], for desktop apps,
32
+ # use the verifier is the pin that LinkedIn gives users.
33
+ def authorize_from_request(rtoken, rsecret, verifier_or_pin)
34
+ request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret)
35
+ access_token = request_token.get_access_token(:oauth_verifier => verifier_or_pin)
36
+ @atoken, @asecret = access_token.token, access_token.secret
37
+ end
38
+
39
+ def access_token
40
+ @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret)
41
+ end
42
+
43
+ def authorize_from_access(atoken, asecret)
44
+ @atoken, @asecret = atoken, asecret
45
+ end
46
+
47
+ def get(path, options={})
48
+ path = "/v1#{path}"
49
+ response = access_token.get(path, options)
50
+ raise_errors(response)
51
+ response.body
52
+ end
53
+
54
+ def put(path, options={})
55
+ path = "/v1#{path}"
56
+ response = access_token.put(path, options)
57
+ raise_errors(response)
58
+ response
59
+ end
60
+
61
+ def delete(path, options={})
62
+ path = "/v1#{path}"
63
+ response = access_token.delete(path, options)
64
+ raise_errors(response)
65
+ response
66
+ end
67
+
68
+
69
+ def profile(options={})
70
+
71
+ path = person_path(options)
72
+
73
+ unless options[:fields].nil?
74
+ if options[:public]
75
+ path +=":public"
76
+ else
77
+ path +=":(#{options[:fields].map{|f| f.to_s.gsub("_","-")}.join(',')})"
78
+ end
79
+ end
80
+
81
+ Profile.from_xml(get(path))
82
+ end
83
+
84
+ def connections(options={})
85
+ path = "#{person_path(options)}/connections"
86
+
87
+ unless options[:fields].nil?
88
+ if options[:public]
89
+ path +=":public"
90
+ else
91
+ path +=":(#{options[:fields].map{|f| f.to_s.gsub("_","-")}.join(',')})"
92
+ end
93
+ end
94
+
95
+ Connections.from_xml(get(path)).profiles
96
+ end
97
+
98
+ def search(options={})
99
+ path = "/people"
100
+ options = {:keywords => options} if options.is_a?(String)
101
+ options = format_options_for_query(options)
102
+
103
+ People.from_xml(get(to_uri(path, options)))
104
+ end
105
+
106
+ def current_status
107
+ path = "/people/~/current-status"
108
+ Crack::XML.parse(get(path))['current_status']
109
+ end
110
+
111
+ def update_status(text)
112
+ path = "/people/~/current-status"
113
+ put(path, status_to_xml(text))
114
+ end
115
+
116
+ def clear_status
117
+ path = "/people/~/current-status"
118
+ delete(path).code
119
+ end
120
+
121
+ def network_statuses(options={})
122
+ options[:type] = 'STAT'
123
+ network_updates(options)
124
+ end
125
+
126
+ def network_updates(options={})
127
+ path = "/people/~/network"
128
+ Network.from_xml(get(to_uri(path, options)))
129
+ end
130
+
131
+
132
+
133
+
134
+ # helpful in making authenticated calls and writing the
135
+ # raw xml to a fixture file
136
+ def write_fixture(path, filename)
137
+ file = File.new("test/fixtures/#{filename}", "w")
138
+ file.puts(access_token.get(path).body)
139
+ file.close
140
+ end
141
+
142
+ private
143
+ def clear_request_token
144
+ @request_token = nil
145
+ end
146
+
147
+ def raise_errors(response)
148
+ # Even if the XML answer contains the HTTP status code, LinkedIn also sets this code
149
+ # in the HTTP answer (thankfully).
150
+ case response.code.to_i
151
+ when 400
152
+ data = LinkedIn::Error.from_xml(response.body)
153
+ raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data.code if data}"
154
+ when 401
155
+ data = LinkedIn::Error.from_xml(response.body)
156
+ raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data.code if data}"
157
+ when 403
158
+ data = LinkedIn::Error.from_xml(response.body)
159
+ raise General.new(data), "(#{response.code}): #{response.message} - #{data.code if data}"
160
+ when 404
161
+ raise NotFound, "(#{response.code}): #{response.message}"
162
+ when 500
163
+ raise InformLinkedIn, "LinkedIn had an internal error. Please let them know in the forum. (#{response.code}): #{response.message}"
164
+ when 502..503
165
+ raise Unavailable, "(#{response.code}): #{response.message}"
166
+ end
167
+ end
168
+
169
+ def format_options_for_query(opts)
170
+ opts.keys.each do |key|
171
+ value = opts.delete(key)
172
+ value = value.join("+") if value.is_a?(Array)
173
+ value = value.gsub(" ", "+") if value.is_a?(String)
174
+ opts[key.to_s.gsub("_","-")] = value
175
+ end
176
+ opts
177
+ end
178
+
179
+ def to_query(options)
180
+ options.inject([]) do |collection, opt|
181
+ collection << "#{opt[0]}=#{opt[1]}"
182
+ collection
183
+ end * '&'
184
+ end
185
+
186
+ def to_uri(path, options)
187
+ uri = URI.parse(path)
188
+
189
+ if options && options != {}
190
+ uri.query = to_query(options)
191
+ end
192
+ uri.to_s
193
+ end
194
+
195
+ def person_path(options)
196
+ path = "/people/"
197
+ if options[:id]
198
+ path += "id=#{options[:id]}"
199
+ elsif options[:email]
200
+ path += "email=#{options[:email]}"
201
+ elsif options[:url]
202
+ path += "url=#{CGI.escape(options[:url])}"
203
+ else
204
+ path += "~"
205
+ end
206
+ end
207
+
208
+ def status_to_xml(status)
209
+ %Q{<?xml version="1.0" encoding="UTF-8"?>
210
+ <current-status>#{status}</current-status>}
211
+ end
212
+
213
+
214
+ end
215
+ end