rapleaf_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ Manifest.txt
2
+ README.txt
3
+ Rakefile
4
+ lib/rapleaf_api.rb
5
+ lib/api/api.rb
6
+ lib/api/graph.rb
7
+ lib/api/person.rb
8
+ lib/api/errors.rb
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # rapleaf_api
2
+
3
+ Getting Started
4
+ ---------------
5
+
6
+ You need to first get an API key found here:
7
+
8
+ [http://www.rapleaf.com/developer/](http://www.rapleaf.com/developer/)
9
+
10
+
11
+ Installation
12
+ ------------
13
+
14
+ sudo gem install rapleaf_api
15
+
16
+ Usage
17
+ -----
18
+
19
+ require 'rubygems'
20
+ require 'rapleaf_api'
21
+
22
+ rapAPI = RapleafApi::Api.new("your api key")
23
+
24
+ ### Person API
25
+
26
+ #by email
27
+ person = rapAPI.query({:type => :person, {:email => "email"}})
28
+
29
+ #by md5 hash of email
30
+ person = rapAPI.query({:type => :person, {:hash => RapleafApi::MD5, :id => "md5 hash of email"}})
31
+
32
+ #by site id
33
+ person = rapAPI.query({:type => :person, {:site => RapleafApi::TWITTER, :id => "twitter username"}})
34
+
35
+ ### Graph API
36
+
37
+ #by email (note this query will only work if you have an email set stored with Rapleaf)
38
+ friendList = rapAPI.query({:type => :graph, {:id => "email address"}})
39
+
40
+ #by email, return Rapleaf IDs instead of scoped emails
41
+ friendList = rapAPI.query({:type => :graph, {:id => "email address", :return_rapid => true}})
42
+
43
+ #by Rapleaf ID, this will return a list of Rapleaf IDs
44
+ friendList = rapAPI.query({:type => :graph, {:id => "rapleaf id", :by_rapid => true}})
45
+
46
+ Authors
47
+ -------
48
+
49
+ * Adam Coffman :: coffman.adam@gmail.com
50
+ * Brent Beer :: brentbeer@gmail.com
51
+ * Mark Sands :: marksands07@gmail.com
data/lib/api/api.rb ADDED
@@ -0,0 +1,85 @@
1
+ module RapleafApi
2
+ class Api
3
+
4
+ def initialize(api_key)
5
+ @API_KEY = "?api_key=#{api_key}"
6
+ @PERSON_URL = "http://api.rapleaf.com/v3/person/"
7
+ @GRAPH_URL = "http://api.rapleaf.com/v2/graph/"
8
+ end
9
+
10
+ def query(params)
11
+ raise InvalidParams, "Invalid query, please use an :opts hash to specify your options" unless !params[:opts].nil?
12
+ case params[:type]
13
+ when :person
14
+ return Person.new(person_request(params[:opts]))
15
+ when :graph
16
+ return Graph.new(graph_request(params[:opts]))
17
+ else
18
+ raise InvalidParams, "Invalid query type, use either :graph or :person"
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def request(url)
25
+ resp = Net::HTTP.get_response(URI.parse(url))
26
+ if resp.code == "200"
27
+ return resp.body
28
+ else
29
+ raise_response_errors(resp.code)
30
+ end
31
+ end
32
+
33
+ def person_request(params)
34
+ raise InvalidParams, "Your params hash was formatted incorrectly!" unless url = verify_and_build_person_url(params)
35
+ request(url)
36
+ end
37
+
38
+ def graph_request(params)
39
+ raise InvalidParams, "Your params hash was formatted incorrectly!" unless url = verify_and_build_graph_url(params)
40
+ request(url)
41
+ end
42
+
43
+ def verify_and_build_person_url(params)
44
+ if params.size == 1
45
+ return false unless params[:email]
46
+ return @PERSON_URL + "email/#{params[:email]}" + @API_KEY
47
+ else
48
+ return false unless params[:id] && (params[:hash] || params[:site])
49
+ return @PERSON_URL + (!params[:hash].nil? ? "hash/#{params[:hash]}/#{params[:id]}" : "web/#{params[:site]}/#{params[:id]}") + @API_KEY
50
+ end
51
+ end
52
+
53
+ def verify_and_build_graph_url(params)
54
+ if params.size == 1
55
+ return false unless params[:id]
56
+ return @GRAPH_URL + params[:id] + @API_KEY
57
+ elsif params.size == 2
58
+ return false unless params[:id] && (params[:by_rapid] || params[:return_rapid])
59
+ return @GRAPH_URL + params[:id] + @API_KEY + "&" + ( !params[:by_rapid].nil? ? "n=2" : "n=1")
60
+ else
61
+ return false
62
+ end
63
+ end
64
+
65
+ def raise_response_errors(code)
66
+ case code
67
+ when "202"
68
+ raise AcceptedException, "This person is currently being searched. Please check back shortly."
69
+ when "400"
70
+ raise MalformedRequestException, "Malformed request."
71
+ when "401"
72
+ raise ApiKeyInvalidException, "API Key invalid"
73
+ when "403"
74
+ raise RateLimitExceededException, "API Key query limit exceded, please contact developer@rapleaf.com to have your limit increased."
75
+ when "404"
76
+ raise PersonNotFoundException, "We do not have this person in our system. If you would like better results, consider supplying the email address."
77
+ when "500"
78
+ raise InternalServerError, "There was an unexpected error on our server. This should be very rare and if you see it please contact developer@rapleaf.com."
79
+ else
80
+ raise "Unkown Error"
81
+ end
82
+ end
83
+
84
+ end
85
+ end
data/lib/api/errors.rb ADDED
@@ -0,0 +1,26 @@
1
+ module RapleafApi
2
+
3
+ class InvalidParams < Exception
4
+ end
5
+
6
+ class AcceptedException < Exception
7
+ end
8
+
9
+ class MalformedRequestException < Exception
10
+ end
11
+
12
+ class ApiKeyInvalidException < Exception
13
+ end
14
+
15
+ class RateLimitExceededException < Exception
16
+ end
17
+
18
+ class PersonNotFoundException < Exception
19
+ end
20
+
21
+ class InternalServerError < Exception
22
+ end
23
+
24
+ end
25
+
26
+
data/lib/api/graph.rb ADDED
@@ -0,0 +1,17 @@
1
+ module RapleafApi
2
+ class Graph
3
+
4
+ def initialize( body )
5
+ @body = body
6
+ end
7
+
8
+ def friends
9
+ if @body.include?(",")
10
+ @body.split(",")
11
+ else
12
+ @body.split("\n")
13
+ end
14
+ end
15
+
16
+ end
17
+ end
data/lib/api/person.rb ADDED
@@ -0,0 +1,19 @@
1
+ module RapleafApi
2
+
3
+ class Person
4
+
5
+ def initialize(xml)
6
+ @xml = XmlSimple.xml_in(xml)
7
+ end
8
+
9
+ def xml
10
+ @xml
11
+ end
12
+
13
+ def rapleaf_id
14
+ @xml["id"]
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'xmlsimple'
4
+
5
+ %w{api.rb person.rb graph.rb errors.rb}.each do |file|
6
+ require File.dirname(__FILE__) +"/api/" + file
7
+ end
8
+
9
+ module RapleafApi
10
+
11
+ VERSION = '0.1.0'
12
+
13
+ #Constants for web queries
14
+ BEBO = "bebo"
15
+ FACEBOOK = "facebook"
16
+ FLICKR = "flickr"
17
+ FRIENDSTER = "friendster"
18
+ HI5 = "hi5"
19
+ LINKEDIN = "linkedin"
20
+ MYSPACE = "myspace"
21
+ PLAXO = "plaxo"
22
+ RAPLEAF = "rapleaf"
23
+ TWITTER = "twitter"
24
+
25
+ #constants for hash types
26
+ MD5 = "md5"
27
+ SHA1 = "sha1"
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rapleaf_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Coffman
8
+ - Brent Beer
9
+ - Mark Sands
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2010-02-12 00:00:00 -06:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: xml-simple
19
+ type: :runtime
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "0"
26
+ version:
27
+ description: An interface to Rapleaf's person and graph APIs. Note that this library is not maintained by or affiliated with Rapleaf in any way and you will still need a (free) api key from Rapleaf to use this.
28
+ email:
29
+ - coffman.adam@gmail.com
30
+ - brent.beer@gmail.com
31
+ - marksands07@gmail.com
32
+ executables: []
33
+
34
+ extensions: []
35
+
36
+ extra_rdoc_files: []
37
+
38
+ files:
39
+ - README.md
40
+ - Manifest.txt
41
+ - lib/rapleaf_api.rb
42
+ - lib/api/api.rb
43
+ - lib/api/graph.rb
44
+ - lib/api/person.rb
45
+ - lib/api/errors.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/thecoffman/rapleaf_api
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: An interface to Rapleaf's person and graph APIs.
74
+ test_files: []
75
+