orkut_os_client 0.0.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.
@@ -0,0 +1,40 @@
1
+ module Orkut
2
+ class Captcha
3
+
4
+ def captcha_image(token, secret, captcha_token_id)
5
+
6
+ begin
7
+
8
+ consumer = initialize_consumer
9
+
10
+ token_hash = { :oauth_token => token, :oauth_token_secret => secret }
11
+ access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
12
+
13
+ o = Oauth.new
14
+ o.consumer_key = Network::CONFIG[Rails.env]['orkut']['consumer_key']
15
+ o.consumer_secret = Network::CONFIG[Rails.env]['orkut']['consumer_secret']
16
+
17
+ url = "http://sandbox.orkut.com/social/pages/captcha?xid=#{captcha_token_id}"
18
+
19
+ require 'net/http'
20
+
21
+ parsed_url = URI.parse( url )
22
+
23
+ result = ''
24
+ Net::HTTP.start( parsed_url.host ) {|http|
25
+ final_url = "#{url}?#{o.sign( url ).to_query_string() }"
26
+ result = access_token.get(final_url)
27
+ }
28
+
29
+ return result
30
+
31
+ rescue Exception => e
32
+ puts e.message
33
+ puts e.backtrace.inspect
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,11 @@
1
+ module Orkut
2
+ class Client
3
+ module Albums
4
+
5
+ def albums
6
+ Orkut::Connect.new.post_json_rpc self, Orkut::RPC::ALBUMS
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Orkut
2
+ class Client
3
+ module Friends
4
+
5
+ # get friends
6
+ def friends
7
+ Orkut::Connect.new.post_json_rpc self, Orkut::RPC::FRIENDS
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require "orkut/connect/connect"
2
+
3
+ module Orkut
4
+ class Client
5
+ module Profile
6
+
7
+ # get profile data
8
+ def profile(id='@me')
9
+ Orkut::Connect.new.post_json_rpc self, mount_profile(id)
10
+ end
11
+
12
+ private
13
+ def mount_profile(id)
14
+ "{ 'method' : 'people.get', 'id' : 'myself', 'params' : { 'userId' : '#{id}', 'groupId' : '@self' } }"
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,88 @@
1
+ require "orkut/captcha"
2
+
3
+ module Orkut
4
+ class Client
5
+ module Scraps
6
+
7
+ # get scraps
8
+ def scraps
9
+ Orkut::Connect.new.post_json_rpc self, Orkut::RPC::SCRAPS
10
+ end
11
+
12
+ def send_scrap_with_captcha(token, secret, friend_social_id, msg, captchaAnswer, captchaValue)
13
+
14
+ consumer = initialize_consumer
15
+
16
+ token_hash = { :oauth_token => token, :oauth_token_secret => secret }
17
+ access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
18
+
19
+ data = "
20
+ [
21
+ {
22
+ 'method' : 'captcha.answer',
23
+ 'id' : 'myself',
24
+ 'params' :
25
+ {
26
+ 'userId' : '@me',
27
+ 'groupId' : '@self',
28
+ 'captchaAnswer' : '#{captchaAnswer}',
29
+ 'captchaToken' : '#{captchaValue}'
30
+ }
31
+ }
32
+ ,
33
+ {
34
+ 'method' : 'messages.create',
35
+ 'id' : 'myself',
36
+ 'params' :
37
+ {
38
+ 'userId' : '#{friend_social_id}',
39
+ 'groupId' : '@self',
40
+ 'message' :
41
+ {
42
+ 'recipients' : '#{friend_social_id}',
43
+ 'body' : '#{msg}',
44
+ 'title' : 'teste',
45
+ },
46
+ 'messageType' : 'public_message'
47
+ }
48
+ }
49
+ ]
50
+ "
51
+
52
+ result = access_token.post(RPC_ENDPOINT, data, { 'Content-Type' => 'application/json' }).body
53
+
54
+ end
55
+
56
+
57
+ def send_scrap(token, secret, friend_social_id, msg)
58
+
59
+ consumer = initialize_consumer
60
+
61
+ token_hash = { :oauth_token => token, :oauth_token_secret => secret }
62
+ access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
63
+
64
+ data = "
65
+ {
66
+ 'method' : 'messages.create',
67
+ 'id' : 'myself',
68
+ 'params' :
69
+ {
70
+ 'userId' : '#{friend_social_id}',
71
+ 'groupId' : '@self',
72
+ 'message' :
73
+ {
74
+ 'recipients' : '#{friend_social_id}',
75
+ 'body' : '#{msg}',
76
+ 'title' : 'Chip',
77
+ },
78
+ 'messageType' : 'public_message'
79
+ }
80
+ }"
81
+
82
+ result = access_token.post(RPC_ENDPOINT, data, { 'Content-Type' => 'application/json' }).body
83
+ MultiJson.decode(result)
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,32 @@
1
+ require "orkut/config"
2
+ require "orkut/rpc"
3
+
4
+ module Orkut
5
+
6
+ class Client
7
+
8
+ require "orkut/client/profile"
9
+ require "orkut/client/friends"
10
+ require "orkut/client/scraps"
11
+ require "orkut/client/albums"
12
+
13
+ include Orkut::Client::Profile
14
+ include Orkut::Client::Friends
15
+ include Orkut::Client::Scraps
16
+ include Orkut::Client::Albums
17
+
18
+ attr_accessor *Config::VALID_OPTIONS_KEYS
19
+
20
+ # Initializes a new API object
21
+ #
22
+ # @return [Orkut::Client]
23
+ def initialize(attrs={})
24
+ attrs = Orkut.options.merge(attrs)
25
+ Config::VALID_OPTIONS_KEYS.each do |key|
26
+ instance_variable_set("@#{key}".to_sym, attrs[key])
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,27 @@
1
+ require "orkut/version"
2
+
3
+ module Orkut
4
+ module Config
5
+
6
+ # The endpoint that will be used to connect if none is set
7
+ DEFAULT_RPC_ENDPOINT = 'http://sandbox.orkut.com/social/rpc'
8
+
9
+ # An array of valid keys in the options hash when configuring a {Orkut::Client}
10
+ VALID_OPTIONS_KEYS = [
11
+ :consumer_key,
12
+ :consumer_secret,
13
+ :oauth_token,
14
+ :oauth_token_secret,
15
+ ]
16
+
17
+ attr_accessor *VALID_OPTIONS_KEYS
18
+
19
+ # Create a hash of options and their values
20
+ def options
21
+ options = {}
22
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
23
+ options
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require "oauth"
2
+ require 'multi_json'
3
+
4
+ module Orkut
5
+ class Connect
6
+
7
+ # generic consumer initializer
8
+ def initialize_consumer(client)
9
+ OAuth::Consumer.new( client.consumer_key, client.consumer_secret,
10
+ :site => "https://www.google.com",
11
+ :request_token_path => "/accounts/OAuthGetRequestToken",
12
+ :access_token_path => "/accounts/OAuthGetAccessToken",
13
+ :authorize_path => "/accounts/OAuthAuthorizeToken")
14
+ end
15
+
16
+ # generic post json rpc call
17
+ def post_json_rpc(client, subject)
18
+ consumer = initialize_consumer client
19
+ token_hash = { :oauth_token => client.oauth_token, :oauth_token_secret => client.oauth_token_secret }
20
+ access_token = OAuth::AccessToken.from_hash consumer, token_hash
21
+
22
+ data = access_token.post(Orkut::Config::DEFAULT_RPC_ENDPOINT, subject,
23
+ { "Content-Type" => "application/json" }).body
24
+
25
+ MultiJson.decode data
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,117 @@
1
+ # A utility for signing an url using OAuth in a way that's convenient for debugging
2
+ # Note: the standard Ruby OAuth lib is here http://github.com/mojodna/oauth
3
+ # License: http://gist.github.com/375593
4
+ # Usage: see example.rb below
5
+
6
+ require 'uri'
7
+ require 'openssl'
8
+ require 'base64'
9
+
10
+ class Oauth
11
+
12
+ attr_accessor :consumer_key, :consumer_secret, :token, :token_secret, :req_method, :sig_method, :oauth_version, :callback_url, :params
13
+
14
+ def initialize
15
+ @consumer_key = ''
16
+ @consumer_secret = ''
17
+ @token = ''
18
+ @token_secret = ''
19
+ @req_method = 'GET'
20
+ @sig_method = 'HMAC-SHA1'
21
+ @oauth_version = '1.0'
22
+ @callback_url = ''
23
+ @params = []
24
+ end
25
+
26
+ # openssl::random_bytes returns non-word chars, which need to be removed. using alt method to get consistent length
27
+ # ref http://snippets.dzone.com/posts/show/491
28
+ def generate_nonce
29
+ Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first
30
+ end
31
+
32
+ def percent_encode( string )
33
+
34
+ # ref http://snippets.dzone.com/posts/show/1260
35
+ URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
36
+ end
37
+
38
+ # sort (very important as it affects the signature), concat, and percent encode
39
+ # @ref http://oauth.net/core/1.0/#rfc.section.9.1.1
40
+ # @ref http://oauth.net/core/1.0/#9.2.1
41
+ # @ref http://oauth.net/core/1.0/#rfc.section.A.5.1
42
+ def normalize_req_params( params )
43
+ percent_encode( params.sort().join( '&' ) )
44
+ end
45
+
46
+ # @ref http://oauth.net/core/1.0/#rfc.section.9.1.2
47
+ def construct_req_url( url)
48
+ parsed_url = URI.parse( url )
49
+ parsed_url.scheme + '://' + parsed_url.host + parsed_url.path
50
+ end
51
+
52
+ # @ref http://oauth.net/core/1.0/#rfc.section.9.2
53
+ def generate_sig( args )
54
+ key = percent_encode( args[:consumer_secret] ) + '&' + percent_encode( args[:token_secret] )
55
+ text = args[:base_str]
56
+
57
+ # credit: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks
58
+ digest = OpenSSL::Digest::Digest.new( 'sha1' )
59
+ raw_sig = OpenSSL::HMAC.digest( digest, key, text )
60
+
61
+ # ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81
62
+ Base64.encode64( raw_sig ).chomp.gsub( /\n/, '' )
63
+ end
64
+
65
+ def to_query_string
66
+ @params.find_all{ | item | item =~ /oauth_.*/ }.join( '&' )
67
+ end
68
+
69
+ def to_header_string
70
+ pairs = []
71
+
72
+ # format string as key1="val1", key2="val2" ...
73
+ @params.find_all{ | item | item =~ /oauth_.*/ }.each{ | item |
74
+ pair = '%s="%s"' % item.split( '=' )
75
+ pairs.push( pair )
76
+ }
77
+ pairs.join( ', ' )
78
+ end
79
+
80
+ def sign( url )
81
+
82
+ parsed_url = URI.parse( url )
83
+
84
+ # basic oauth params formatted as strings in array so we can easily sort
85
+ @params.push( 'oauth_consumer_key=' + @consumer_key )
86
+ @params.push( 'oauth_nonce=' + generate_nonce() )
87
+ @params.push( 'oauth_signature_method=' + @sig_method )
88
+ @params.push( 'oauth_timestamp=' + Time.now.to_i.to_s )
89
+ @params.push( 'oauth_version=' + @oauth_version )
90
+
91
+ # if params passed in, add them & replace defaults
92
+ if parsed_url.query
93
+ @params = @params | parsed_url.query.split( '&' )
94
+ end
95
+
96
+ # elems for base str
97
+ normal_req_params = normalize_req_params( params )
98
+
99
+ # @ref http://oauth.net/core/1.0/#rfc.section.9.1.2
100
+ req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path
101
+
102
+ # create base str
103
+ base_str = [ @req_method, percent_encode( req_url ), normal_req_params ].join( '&' )
104
+
105
+ # sign
106
+ signature = generate_sig({
107
+ :base_str => base_str,
108
+ :consumer_secret => @consumer_secret,
109
+ :token_secret => @token_secret
110
+ })
111
+
112
+ @params.push( 'oauth_signature=' + percent_encode( signature ) )
113
+
114
+ return self
115
+ end
116
+ end
117
+
data/lib/orkut/rpc.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Orkut
2
+ module RPC
3
+
4
+ SCRAPS = '{
5
+ "method" : "messages.get",
6
+ "id" : "myself",
7
+ "params" :
8
+ {
9
+ "userId" : "@me",
10
+ "groupId" : "@friends",
11
+ "pageType" : "first",
12
+ "messageType" : "public_message"
13
+ }
14
+ }'
15
+
16
+ FRIENDS = '{
17
+ "method" : "people.get",
18
+ "id" : "myself",
19
+ "params" :
20
+ {
21
+ "userId" : "@me",
22
+ "groupId" : "@friends",
23
+ "count" : 999
24
+ }
25
+ }'
26
+
27
+ ALBUMS = '{
28
+ "method" : "albums.get",
29
+ "id" : "myself",
30
+ "params" :
31
+ {
32
+ "userId" : "@me",
33
+ "groupId" : "@self"
34
+ }
35
+ }'
36
+
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ module Orkut
2
+ class Version
3
+
4
+ def self.major
5
+ 0
6
+ end
7
+
8
+ def self.minor
9
+ 0
10
+ end
11
+
12
+ def self.patch
13
+ 0
14
+ end
15
+
16
+ def self.pre
17
+ nil
18
+ end
19
+
20
+ def self.to_s
21
+ [major, minor, patch, pre].compact.join('.')
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ require "orkut/client"
2
+ require "orkut/config"
3
+
4
+ module Orkut
5
+ extend Config
6
+
7
+ class << self
8
+
9
+ # Alias for Orkut::Client.new
10
+ # return [Orkut::Client]
11
+ def new(options={})
12
+ Orkut::Client.new(options)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ {"id":"myself","data":{"id":"082172724459123123","thumbnailUrl":"http://img8.orkut.com/images/small/1273006165/123456/ln.jpg","name":{"familyName":"Leite","givenName":"Anderson"}}}
data/spec/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+
3
+ require "orkut_os_client"
@@ -0,0 +1,45 @@
1
+ require "helper"
2
+
3
+ describe Orkut do
4
+
5
+ context "when using orkut client" do
6
+
7
+ before do
8
+ # stub_post("").
9
+ # to_return(:body => fixture("profile.json"), :headers => {:content_type => "application/json; charset=utf-8"})
10
+
11
+ @orkut = Orkut.new(:consumer_key => 'tictacfight.heroku.com',
12
+ :consumer_secret => 'F_d-RByVwEJe2hV2fE7jo2l5',
13
+ :oauth_token => '1/uSnaAxVdx6SOs0pAjkQO3iC3h-N9Lng4U3N1Q8tivOc',
14
+ :oauth_token_secret => '9I6LlarEUjGLBrt2fb3eslyn')
15
+
16
+ end
17
+
18
+ it "should get the user profile" do
19
+ @orkut.should_not be_nil
20
+
21
+ data = @orkut.profile
22
+ data['data']['name']['givenName'].should be == 'Anderson'
23
+ data['data']['name']['familyName'].should be == 'Leite'
24
+ end
25
+
26
+ it "should get user's friends" do
27
+ @orkut.should_not be_nil
28
+ data = @orkut.friends
29
+ data.should_not be_nil
30
+ end
31
+
32
+ it "should get user's scraps" do
33
+ @orkut.should_not be_nil
34
+ data = @orkut.scraps
35
+ data.should_not be_nil
36
+ end
37
+
38
+ it "should get user's albums" do
39
+ @orkut.should_not be_nil
40
+ data = @orkut.albums
41
+ data.should_not be_nil
42
+ end
43
+
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orkut_os_client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Anderson Leite
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-25 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A Ruby wrapper for the Orkut RPC-JSON API.
22
+ email:
23
+ - andersonlfl@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/orkut/captcha.rb
32
+ - lib/orkut/client/albums.rb
33
+ - lib/orkut/client/friends.rb
34
+ - lib/orkut/client/profile.rb
35
+ - lib/orkut/client/scraps.rb
36
+ - lib/orkut/client.rb
37
+ - lib/orkut/config.rb
38
+ - lib/orkut/connect/connect.rb
39
+ - lib/orkut/oauth.rb
40
+ - lib/orkut/rpc.rb
41
+ - lib/orkut/version.rb
42
+ - lib/orkut_os_client.rb
43
+ - spec/fixtures/profile.json
44
+ - spec/helper.rb
45
+ - spec/orkut_spec.rb
46
+ homepage: https://github.com/andersonleite/orkut_os_client
47
+ licenses: []
48
+
49
+ post_install_message: |
50
+ ********************************************************************************
51
+
52
+ orkut_os_client
53
+
54
+ A Ruby wrapper for the Orkut RPC-JSON API.
55
+
56
+ ********************************************************************************
57
+
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 23
77
+ segments:
78
+ - 1
79
+ - 3
80
+ - 6
81
+ version: 1.3.6
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.11
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Orkut RPC-JSON API wrapper
89
+ test_files:
90
+ - spec/fixtures/profile.json
91
+ - spec/helper.rb
92
+ - spec/orkut_spec.rb