viadeo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/Gemfile +4 -0
  2. data/Gemfile.lock +43 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +55 -0
  5. data/Rakefile +28 -0
  6. data/fixtures/vcr_cassettes/newsfeed/retrieve.yml +114 -0
  7. data/fixtures/vcr_cassettes/recommend/create.yml +43 -0
  8. data/fixtures/vcr_cassettes/recommend/retrieve.yml +56 -0
  9. data/fixtures/vcr_cassettes/reference_data/all.yml +40 -0
  10. data/fixtures/vcr_cassettes/search/find.yml +68 -0
  11. data/fixtures/vcr_cassettes/status/create.yml +44 -0
  12. data/fixtures/vcr_cassettes/user/retrieve_fail_response.yml +41 -0
  13. data/fixtures/vcr_cassettes/user/retrieve_success_response.yml +41 -0
  14. data/fixtures/vcr_cassettes/user/retrieve_with_contacts_response.yml +40 -0
  15. data/fixtures/vcr_cassettes/user/save_response.yml +40 -0
  16. data/fixtures/vcr_cassettes/user/update_response.yml +40 -0
  17. data/lib/viadeo/configuration.rb +21 -0
  18. data/lib/viadeo/error.rb +5 -0
  19. data/lib/viadeo/graph_connection.rb +10 -0
  20. data/lib/viadeo/graph_object.rb +124 -0
  21. data/lib/viadeo/newsfeed.rb +15 -0
  22. data/lib/viadeo/operations/create.rb +12 -0
  23. data/lib/viadeo/operations/get.rb +13 -0
  24. data/lib/viadeo/operations/list.rb +12 -0
  25. data/lib/viadeo/operations/update.rb +25 -0
  26. data/lib/viadeo/recommend.rb +20 -0
  27. data/lib/viadeo/reference_data.rb +43 -0
  28. data/lib/viadeo/request.rb +24 -0
  29. data/lib/viadeo/search.rb +27 -0
  30. data/lib/viadeo/status.rb +15 -0
  31. data/lib/viadeo/user.rb +41 -0
  32. data/lib/viadeo/util.rb +21 -0
  33. data/lib/viadeo/version.rb +3 -0
  34. data/lib/viadeo.rb +56 -0
  35. data/spec/graph_object_spec.rb +141 -0
  36. data/spec/newsfeed_spec.rb +13 -0
  37. data/spec/recommend_spec.rb +26 -0
  38. data/spec/reference_data_spec.rb +13 -0
  39. data/spec/search_spec.rb +13 -0
  40. data/spec/shared_create_graph_object.rb +15 -0
  41. data/spec/shared_graph_object.rb +12 -0
  42. data/spec/spec_helper.rb +12 -0
  43. data/spec/status_spec.rb +15 -0
  44. data/spec/support/vcr_setup.rb +5 -0
  45. data/spec/user_spec.rb +92 -0
  46. data/spec/util_spec.rb +32 -0
  47. data/spec/viadeo_spec.rb +13 -0
  48. metadata +207 -0
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: https://api.viadeo.com/me?interests=---
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Viadeo gem/0.0.1
12
+ Authorization:
13
+ - Bearer access_token_test_3e8c55877482b2
14
+ Content-Type:
15
+ - application/x-www-form-urlencoded;charset=UTF-8
16
+ Content-Length:
17
+ - '0'
18
+ Accept:
19
+ - ! '*/*'
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Server:
26
+ - Apache-Coyote/1.1
27
+ Uuid:
28
+ - d395b761-7e80-4589-aae0-4e21405b11b3
29
+ Content-Type:
30
+ - application/json;charset=UTF-8
31
+ Content-Length:
32
+ - '16'
33
+ Date:
34
+ - Fri, 03 May 2013 11:05:19 GMT
35
+ body:
36
+ encoding: US-ASCII
37
+ string: ! '{"updated":true}'
38
+ http_version:
39
+ recorded_at: Fri, 03 May 2013 11:05:19 GMT
40
+ recorded_with: VCR 2.4.0
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: https://api.viadeo.com/me?interests=---
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Viadeo gem/0.0.1
12
+ Authorization:
13
+ - Bearer access_token_test_3e8c55877482b2
14
+ Content-Type:
15
+ - application/x-www-form-urlencoded;charset=UTF-8
16
+ Content-Length:
17
+ - '0'
18
+ Accept:
19
+ - ! '*/*'
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Server:
26
+ - Apache-Coyote/1.1
27
+ Uuid:
28
+ - 67d17999-da99-4b73-a6a9-7953f2e1df49
29
+ Content-Type:
30
+ - application/json;charset=UTF-8
31
+ Content-Length:
32
+ - '16'
33
+ Date:
34
+ - Fri, 03 May 2013 11:05:20 GMT
35
+ body:
36
+ encoding: US-ASCII
37
+ string: ! '{"updated":true}'
38
+ http_version:
39
+ recorded_at: Fri, 03 May 2013 11:05:21 GMT
40
+ recorded_with: VCR 2.4.0
@@ -0,0 +1,21 @@
1
+ require "singleton"
2
+ module Viadeo
3
+ class Configuration
4
+ include Singleton
5
+
6
+ DEFAULT_CONFIG = {timeout: 30, open_timeout: 30, url: 'https://api.viadeo.com'}
7
+ attr_accessor :logger, :timeout, :open_timeout, :url
8
+
9
+ def timeout
10
+ @timeout ||= DEFAULT_CONFIG[:timeout]
11
+ end
12
+
13
+ def open_timeout
14
+ @open_timeout ||= DEFAULT_CONFIG[:open_timeout]
15
+ end
16
+
17
+ def url
18
+ @url ||= DEFAULT_CONFIG[:url]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Viadeo
2
+ class Error < StandardError; end
3
+ class MissingResourceError < Error; end
4
+ class MissingAccessTokenError < Error; end
5
+ end
@@ -0,0 +1,10 @@
1
+ module Viadeo
2
+ class GraphConnection < GraphObject
3
+
4
+ def initialize(access_token, options={})
5
+ @connection = options[:connection] if options[:connection]
6
+ super(access_token, options)
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,124 @@
1
+ module Viadeo
2
+ class GraphObject
3
+ include Enumerable
4
+
5
+ attr_reader :id, :access_token
6
+ @@initial_attributes = Set.new([:id, :access_token])
7
+
8
+ def initialize(access_token, options={})
9
+ raise Viadeo::MissingAccessTokenError.new("access_token parameter is mandatory") if (access_token.nil? || access_token.empty?)
10
+ @access_token = access_token
11
+ @id = options[:id] if options[:id]
12
+ @connection = options[:connection] if options[:connection]
13
+ @values = {}
14
+ @updated_attributes = Set.new
15
+ end
16
+
17
+ def self.new_from(values, access_token=nil)
18
+ obj = self.new(access_token, values)
19
+ obj.refresh_from(values, access_token)
20
+ end
21
+
22
+ def url
23
+ self.class.name.downcase.split('::')[-1]
24
+ end
25
+
26
+ def reset
27
+ @values = {}
28
+ @updated_attributes = Set.new
29
+ end
30
+
31
+ def updated?(name=nil)
32
+ return !@updated_attributes.empty? unless name
33
+ return @updated_attributes.include?(name)
34
+ end
35
+
36
+ def to_s(*args)
37
+ MultiJson.dump(@values, :pretty => true)
38
+ end
39
+
40
+ def inspect()
41
+ id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
42
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + MultiJson.dump(@values, :pretty => true)
43
+ end
44
+
45
+ def refresh_from(values, access_token, partial=false)
46
+ @access_token = access_token
47
+ add_accessors(values.keys)
48
+ values.each do |k, v|
49
+ @values[k] = Util.convert_to_graph_object(v, access_token)
50
+ @updated_attributes.delete(k)
51
+ end
52
+ log("response : graph_object : ", self)
53
+ self
54
+ end
55
+
56
+ def [](k)
57
+ k = k.to_sym if k.kind_of?(String)
58
+ @values[k]
59
+ end
60
+
61
+ def []=(k, v)
62
+ send(:"#{k}=", v)
63
+ end
64
+
65
+ def keys
66
+ @values.keys
67
+ end
68
+
69
+ def values
70
+ @values.values
71
+ end
72
+
73
+ def to_json(*a)
74
+ MultiJson.dump(@values)
75
+ end
76
+
77
+ def to_hash
78
+ @values
79
+ end
80
+
81
+ def each(&blk)
82
+ @values.each(&blk)
83
+ end
84
+
85
+ protected
86
+
87
+ def log(message, arg=nil)
88
+ message += " : " + arg.inspect if arg
89
+ puts message
90
+ end
91
+
92
+ def metaclass
93
+ class << self; self; end
94
+ end
95
+
96
+ def add_accessors(keys)
97
+ metaclass.instance_eval do
98
+ keys.each do |k|
99
+ next if @@initial_attributes.include?(k)
100
+ k_eq = :"#{k}="
101
+ define_method(k) { @values[k] }
102
+ define_method(k_eq) do |v|
103
+ @values[k] = v
104
+ @updated_attributes.add(k)
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ def method_missing(name, *args)
111
+ if name.to_s.end_with?('=')
112
+ attr = name.to_s[0...-1].to_sym
113
+ @values[attr] = args[0]
114
+ @updated_attributes.add(attr)
115
+ add_accessors([attr])
116
+ return
117
+ else
118
+ return @values[name] if @values.has_key?(name)
119
+ end
120
+ super
121
+ end
122
+ end
123
+ end
124
+
@@ -0,0 +1,15 @@
1
+ module Viadeo
2
+ # == List of actions
3
+ # * find : Also aliased as : retrieve, get (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/newsfeed]
4
+ #
5
+ # == Example :
6
+ # require "viadeo"
7
+ # # Retrieve a list of newsfeeds and limit the result to 5
8
+ # newsfeed = Viadeo::Newsfeed.new(access_token)
9
+ # newsfeed.find(limit: 5)
10
+
11
+ class Newsfeed < GraphObject
12
+ include Viadeo::Operations::Get
13
+ end
14
+ end
15
+
@@ -0,0 +1,12 @@
1
+ module Viadeo
2
+ module Operations
3
+ module Create
4
+ def create(options={})
5
+ data = Viadeo.request(:post, url: url , access_token: @access_token, params: options)
6
+ refresh_from(data, @access_token)
7
+ self
8
+ end
9
+ alias_method :post, :create
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Viadeo
2
+ module Operations
3
+ module Get
4
+ def find(options={})
5
+ data = Viadeo.request(:get, url: url , access_token: @access_token, params: options)
6
+ refresh_from(data, @access_token)
7
+ self
8
+ end
9
+ alias_method :retrieve, :find
10
+ alias_method :get, :find
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Viadeo
2
+ module Operations
3
+ module List
4
+ def all(options={})
5
+ data = Viadeo.request(:get, url: "#{url}/list" , access_token: @access_token, params: options)
6
+ refresh_from(data, @access_token)
7
+ self
8
+ end
9
+ alias_method :list, :all
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ module Viadeo
2
+ module Operations
3
+ module Update
4
+ def update(options={})
5
+ data = Viadeo.request(:put, url: url , access_token: @access_token, params: options)
6
+ refresh_from(data, @access_token)
7
+ self
8
+ end
9
+
10
+ def save
11
+ if updated?
12
+ update(params)
13
+ end
14
+ self
15
+ end
16
+
17
+ private
18
+ def params
19
+ values = {}
20
+ @updated_attributes.each { |k| values[k] = @values[k] }
21
+ values
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module Viadeo
2
+ # == List of actions
3
+ # * find : Also aliased as : retrieve, get (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/recommend#GET_recommend]
4
+ # * create : Also aliased as post : (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/recommend#POST_recommend]
5
+ # == Examples :
6
+ # require "viadeo"
7
+ #
8
+ # # Retrieve recommended URL infos and limit the result to 5
9
+ # recommend = Viadeo::Recommend.new(access_token)
10
+ # recommend.retrieve(url: "http://dev.viadeo.com", limit: 5)
11
+ #
12
+ # #recommend a new URL
13
+ # recommend.create(url: "http://dev.viadeo.com", title: "Get started with the Viadeo API")
14
+
15
+ class Recommend < GraphObject
16
+ include Viadeo::Operations::Get
17
+ include Viadeo::Operations::Create
18
+ end
19
+ end
20
+
@@ -0,0 +1,43 @@
1
+ module Viadeo
2
+ # == List of actions
3
+ # * all : Also aliased as : list
4
+ # == List of resources
5
+ # * companydepartment (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/companydepartment/list/&domains=,Reference%20data]
6
+ # * companyindustry (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/companyindustry/list/&domains=,Reference%20data]
7
+ # * companynewscategory (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/companynewscategory/list/&domains=,Reference%20data]
8
+ # * contracttype (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/contracttype/list/&domains=,Reference%20data]
9
+ # * country (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/country/list/&domains=,Reference%20data]
10
+ # * experiencelevel (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/experiencelevel/list/&domains=,Reference%20data]
11
+ # * imtype (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/imtype/list/&domains=,Reference%20data]
12
+ # * jobcategory (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/jobcategory/list/&domains=,Reference%20data]
13
+ # * language (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/language/list/&domains=,Reference%20data]
14
+ # * newscategory (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/newscategory/list/&domains=,Reference%20data]
15
+ # * professionalstatus (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/professionalstatus/list/&domains=,Reference%20data]
16
+ # * studylevel (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/studylevel/list/&domains=,Reference%20data]
17
+ # * videoprovider (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/videoprovider/list/&domains=,Reference%20data]
18
+ #
19
+ # == Example :
20
+ # require "viadeo"
21
+ #
22
+ # # Retrieve reference list of languages and limit the result to 5
23
+ # reference_data = Viadeo::ReferenceData.new(access_token, resource: "language")
24
+ # reference_data.all(limit: 5)
25
+ #
26
+ # *WARNING*: if it called without resource parameter it raise MissingResourceError
27
+
28
+ class ReferenceData < GraphObject
29
+ include Viadeo::Operations::List
30
+
31
+ def initialize(access_token, options={})
32
+ raise Viadeo::MissingResourceError.new("resource parameter is mandatory") unless options[:resource]
33
+ @resource = options[:resource]
34
+ super(access_token, options)
35
+ end
36
+
37
+ protected
38
+ def url
39
+ "#{@resource}"
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,24 @@
1
+ module Viadeo
2
+ def self.request(method, options={})
3
+ headers = {
4
+ :user_agent => "Viadeo gem/#{Viadeo::VERSION}",
5
+ :authorization => "Bearer #{options[:access_token]}",
6
+ :content_type => "application/x-www-form-urlencoded;charset=UTF-8"
7
+ }
8
+ timeout_options = {timeout: Viadeo.config.timeout, open_timeout: Viadeo.config.open_timeout}
9
+
10
+ conn = Faraday.new(:url => Viadeo.config.url) do |faraday|
11
+ faraday.request :url_encoded # form-encode POST params
12
+ faraday.response :logger # log requests to STDOUT
13
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
14
+ end
15
+ response = conn.send(method) do |req|
16
+ req.url(options[:id].nil? ? options[:url] : options[:id])
17
+ req.headers = headers
18
+ req.options = timeout_options
19
+ req.params = options[:params] if options[:params]
20
+ end
21
+ Viadeo.log "response : body : " + response.body
22
+ Viadeo::Util.filter_response(response)
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ module Viadeo
2
+ # == List of actions
3
+ # * retrieve (find, get)
4
+ # == List of connections
5
+ # * groups (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/search/groups]
6
+ # * joboffers (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/search/joboffers]
7
+ # * jobs (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/search/jobs]
8
+ # * schoolorganization (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/search/schoolorganization]
9
+ # * users (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/search/users]
10
+ # == Example :
11
+ # require "viadeo"
12
+ # # Search users at paris school and limit the result to 5
13
+ # search = Viadeo::Search.new(access_token, connection: "users")
14
+ # search.find(school: "paris", limit: 5)
15
+ #
16
+ # *WARNING*: if it called without connection it raise an error
17
+
18
+ class Search < GraphConnection
19
+ include Viadeo::Operations::Get
20
+
21
+ protected
22
+ def url
23
+ "#{super}/#{@connection}"
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,15 @@
1
+ module Viadeo
2
+ # == List of actions
3
+ # * create : Also aliased as post : (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/status]
4
+ # == Example :
5
+ # require "viadeo"
6
+ #
7
+ # # Post a status
8
+ # status = Viadeo::Status.new(access_token)
9
+ # status.create(message: "Your message")
10
+
11
+ class Status < GraphObject
12
+ include Viadeo::Operations::Create
13
+ end
14
+ end
15
+
@@ -0,0 +1,41 @@
1
+ module Viadeo
2
+ # == List of actions
3
+ # * find : Also aliased as : retrieve, get (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/user#GET_user_id]
4
+ # * update : Also aliased as put : (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/user#POST_user_id]
5
+ # == Examples :
6
+ # require "viadeo"
7
+ #
8
+ # # Retrieve information from a Viadeo member’s profile. An access to another user profile will count a new visit
9
+ # user = Viadeo::User.new(access_token)
10
+ # user.retrieve
11
+ #
12
+ # # Update user information
13
+ # user.update(country: "France")
14
+ # # Alternative
15
+ # user.country = "France"
16
+ # user.save
17
+ #
18
+ # == List of connections
19
+ # * contacts (details)[http://dev.viadeo.com/documentation/graph-api-resource/?resource=/user/ID/contacts/&domains=,Entry%20points]
20
+ # * ... (more)[http://dev.viadeo.com/graph-api-resource/?resource=%2Fuser%2F&domains=%2CEntry%20points#connections]
21
+ # == Examples :
22
+ # require "viadeo"
23
+ #
24
+ # # Retrieve a list of all a member’s premium contacts.
25
+ # user = Viadeo::User.new(access_token, connection: "contacts")
26
+ # user.retrieve(is_premium: true)
27
+
28
+
29
+
30
+ class User < GraphConnection
31
+ include Viadeo::Operations::Get
32
+ include Viadeo::Operations::Create
33
+ include Viadeo::Operations::Update
34
+
35
+ protected
36
+ def url
37
+ id_or_me = @id.nil? ? "me" : @id
38
+ @connection.nil? ? id_or_me : "#{id_or_me}/#{@connection}"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ module Viadeo
2
+ class Util
3
+ class << self
4
+ def convert_to_graph_object(data, access_token)
5
+ case data
6
+ when Array
7
+ data.map {|v| convert_to_graph_object(v, access_token) }
8
+ when Hash
9
+ data[:id].nil? ? data : Viadeo::GraphObject.new_from(data, access_token)
10
+ else
11
+ data
12
+ end
13
+ end
14
+
15
+ def filter_response(response)
16
+ data = response.body.split("\r\n\r\n")[-1]
17
+ data = MultiJson.load(data, :symbolize_keys => true)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Viadeo
2
+ VERSION = "0.0.1"
3
+ end
data/lib/viadeo.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "multi_json"
2
+ require "faraday"
3
+
4
+ #Utils
5
+ require "viadeo/version"
6
+ require "viadeo/util"
7
+ require "viadeo/configuration"
8
+ require "viadeo/request"
9
+
10
+ #Operations
11
+ require "viadeo/operations/create"
12
+ require "viadeo/operations/update"
13
+ require "viadeo/operations/get"
14
+ require "viadeo/operations/list"
15
+
16
+ #Graph Objects
17
+ require "viadeo/graph_object"
18
+ require "viadeo/graph_connection"
19
+ require "viadeo/user"
20
+ require "viadeo/newsfeed"
21
+ require "viadeo/recommend"
22
+ require "viadeo/reference_data"
23
+ require "viadeo/search"
24
+ require "viadeo/status"
25
+
26
+ #error
27
+ require "viadeo/error"
28
+
29
+ module Viadeo
30
+ class << self
31
+ def version_string
32
+ "Viadeo version #{Viadeo::VERSION}"
33
+ end
34
+
35
+ # Configure Viadeo parameters by passing block
36
+ # === Example
37
+ # Viadeo.configure do |config|
38
+ # config.logger = logger
39
+ # config.timeout = 30
40
+ # config.open_timeout = 30
41
+ # config.url = "https://api.viadeo.com"
42
+ # end
43
+ def configure(&block)
44
+ block.call(config) if block_given?
45
+ end
46
+
47
+ def config
48
+ Viadeo::Configuration.instance
49
+ end
50
+
51
+ def log message
52
+ return if config.logger.nil? || message.nil?
53
+ config.logger.debug message
54
+ end
55
+ end
56
+ end