mailru-api 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mailru-api.rb ADDED
@@ -0,0 +1,136 @@
1
+ #:encoding: utf-8
2
+
3
+ require 'mailru-api/error'
4
+ require 'mailru-api/request'
5
+ require 'mailru-api/audio'
6
+ require 'mailru-api/events'
7
+ require 'mailru-api/friends'
8
+ require 'mailru-api/guestbook'
9
+ require 'mailru-api/mail'
10
+ require 'mailru-api/mobile'
11
+ require 'mailru-api/notifications'
12
+ require 'mailru-api/photos'
13
+ require 'mailru-api/stream'
14
+ require 'mailru-api/users'
15
+
16
+ module MailRU
17
+ class APIConfiguration
18
+ attr_accessor :app_id, :secret_key, :private_key, :session_key, :uid, :format
19
+ end
20
+
21
+ class APIConfigurationBuilder
22
+ attr_reader :configuration
23
+
24
+ def initialize(&block)
25
+ if block_given?
26
+ @configuration = APIConfiguration.new
27
+ instance_eval(&block)
28
+ end
29
+ end
30
+
31
+ def app_id value
32
+ @configuration.app_id = value
33
+ end
34
+
35
+ def secret_key value
36
+ @configuration.secret_key = value
37
+ end
38
+
39
+ def private_key value
40
+ @configuration.private_key = value
41
+ end
42
+
43
+ def session_key value
44
+ @configuration.session_key = value
45
+ end
46
+
47
+ def uid value
48
+ @configuration.uid = value
49
+ end
50
+
51
+ def format value
52
+ @configuration.format = value
53
+ end
54
+ end
55
+
56
+ class API
57
+ module Format
58
+ XML = 'xml'
59
+ JSON = 'json'
60
+ end
61
+
62
+ PATH = 'http://www.appsmail.ru/platform/api'
63
+
64
+ attr_accessor :app_id, :secret_key, :private_key, :session_key, :uid, :format
65
+
66
+ def initialize options = {}, &block
67
+ @app_id = options[:app_id]
68
+ @secret_key = options[:secret_key]
69
+ @private_key = options[:private_key]
70
+ @session_key = options[:session_key]
71
+ @uid = options[:uid]
72
+ @format = options[:format]
73
+
74
+ if block_given?
75
+ if block.arity == 1
76
+ yield self
77
+ else
78
+ configuration = APIConfigurationBuilder.new(&block).configuration
79
+
80
+ unless configuration.nil?
81
+ @app_id = configuration.app_id || @app_id
82
+ @secret_key = configuration.secret_key || @secret_key
83
+ @private_key = configuration.private_key || @private_key
84
+ @session_key = configuration.session_key || @session_key
85
+ @uid = configuration.uid || @uid
86
+ @format = configuration.format || @format
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ def audio
93
+ return Audio.new(self)
94
+ end
95
+
96
+ def events
97
+ return Events.new(self)
98
+ end
99
+
100
+ def friends
101
+ return Friends.new(self)
102
+ end
103
+
104
+ def guestbook
105
+ return Guestbook.new(self)
106
+ end
107
+
108
+ def mail
109
+ return Mail.new(self)
110
+ end
111
+
112
+ def messages
113
+ return Messages.new(self)
114
+ end
115
+
116
+ def mobile
117
+ return Mobile.new(self)
118
+ end
119
+
120
+ def notifications
121
+ return Notifications.new(self)
122
+ end
123
+
124
+ def photos
125
+ return Photos.new(self)
126
+ end
127
+
128
+ def stream
129
+ return Stream.new(self)
130
+ end
131
+
132
+ def users
133
+ return Users.new(self)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,23 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Audio
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def get params = {}
11
+ GetRequest.new(@api, 'audio.get', params).get
12
+ end
13
+
14
+ def link params = {}
15
+ GetRequest.new(@api, 'audio.link', params).get
16
+ end
17
+
18
+ def search params = {}
19
+ GetRequest.new(@api, 'audio.search', params).get
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Error < RuntimeError
6
+ attr_accessor :code, :description
7
+
8
+ def initialize code, description
9
+ @code = Integer(code)
10
+ @description = description
11
+ end
12
+
13
+ def self.create(code, description)
14
+ case code
15
+ when 1 then UnknwonError.new(code, description)
16
+ when 2 then UnknownMethodCalledError.new(code, description)
17
+ when 3 then MethodIsDeprecatedError.new(code, description)
18
+ when 100 then InvalidParameterError.new(code, description)
19
+ when 102 then AuthorizationFailedError.new(code, description)
20
+ when 103 then ApplicationLookupFailedError.new(code, description)
21
+ when 104 then IncorrectSignatureError.new(code, description)
22
+ when 105 then ApplicationIsNotInstalledError.new(code, description)
23
+ when 200 then PermissionDeniedError.new(code, description)
24
+ when 202 then AccessToObjectDeniedError.new(code, description)
25
+ when 501 then IncorrectImageError.new(code, description)
26
+ else Error.new(code, description)
27
+ end
28
+ end
29
+ end
30
+
31
+ class UnknownError < Error; end
32
+ class UnknownMethodCalledError < Error; end
33
+ class MethodIsDeprecatedError < Error; end
34
+ class InvalidParameterError < Error; end
35
+ class AuthorizationFailedError < Error; end
36
+ class ApplicationLookupFailedError < Error; end
37
+ class IncorrectSignatureError < Error; end
38
+ class ApplicationIsNotInstalledError < Error; end
39
+ class PermissionDeniedError < Error; end
40
+ class AccessToObjectDeniedError < Error; end
41
+ class IncorrectImageError < Error; end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Events
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def get_new_count
11
+ GetRequest.new(@api, 'events.getNewCount', {}).get
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Friends
6
+ RETURN_OBJECTS = 1
7
+ RETURN_UIDS = 0
8
+
9
+ def initialize api
10
+ @api = api
11
+ end
12
+
13
+ def get params = {}
14
+ GetRequest.new(@api, 'friends.get', params).get
15
+ end
16
+
17
+ def get_app_users params = {}
18
+ GetRequest.new(@api, 'friends.getAppUsers', params).get
19
+ end
20
+
21
+ def get_invitations_count params = {}
22
+ GetRequest.new(@api, 'friends.getInvitationsCount', params).get
23
+ end
24
+
25
+ def get_online params = {}
26
+ GetRequest.new(@api, 'friends.getOnline', params).get
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,19 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Guestbook
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def get params = {}
11
+ GetRequest.new(@api, 'guestbook.get', params).get
12
+ end
13
+
14
+ def post params = {}
15
+ PostRequest.new(@api, 'guestbook.post', params).post
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Mail
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def get_unread_count params = {}
11
+ GetRequest.new(@api, 'mail.getUnreadCount', params).get
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ #:encoding: utf-8
2
+
3
+ module MyMailRU
4
+ class API
5
+ class Messages
6
+ def get_thread params = {}
7
+ GetRequest.new(@api, 'messages.getThread', params).get
8
+ end
9
+
10
+ def get_threads_list params = {}
11
+ GetRequest.new(@api, 'messages.getThreadsList', params).get
12
+ end
13
+
14
+ def get_unread_count params = {}
15
+ GetRequest.new(@api, 'messages.getUnreadCount', params).get
16
+ end
17
+
18
+ def post params = {}
19
+ PostRequest.new(@api, 'messages.post', params).post
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Mobile
6
+ BASIC = 'basic'
7
+ SMARTPHONE = 'smartphone'
8
+
9
+ def initialize api
10
+ @api = api
11
+ end
12
+
13
+ def get_canvas params = {}
14
+ GetRequest.new(@api, 'mobile.getCanvas', params, Request::Secure::No).get
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Notifications
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def send params = {}
11
+ GetRequest.new(@api, 'notifications.send', params, Request::Secure::Yes).get
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Photos
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def create_album params = {}
11
+ GetRequest.new(@api, 'photos.createAlbum', params).get
12
+ end
13
+
14
+ def get params = {}
15
+ GetRequest.new(@api, 'photos.get', params).get
16
+ end
17
+
18
+ def get_albums params = {}
19
+ GetRequest.new(@api, 'photos.getAlbums', params).get
20
+ end
21
+
22
+ def upload params = {}
23
+ PostRequest.new(@api, 'photos.upload', params).post
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,103 @@
1
+ #:encoding: utf-8
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'digest'
7
+ require 'rexml/document'
8
+
9
+ module MailRU
10
+ class API
11
+ class Request
12
+
13
+ module Secure
14
+ No = 0
15
+ Yes = 1
16
+ Any = 2
17
+ end
18
+
19
+ def initialize api, method, method_params, secure = Secure::Any
20
+ @api = api
21
+ @method = method
22
+ @method_params = method_params
23
+ @secure = secure
24
+ end
25
+
26
+ protected
27
+
28
+ def use_s2s?
29
+ @api.secret_key and (@secure == Secure::Yes or @secure == Secure::Any)
30
+ end
31
+
32
+ def use_c2s?
33
+ @api.private_key and (@secure == Secure::No or @secure == Secure::Any)
34
+ end
35
+
36
+ def signature
37
+ return s2s_signature if use_s2s?
38
+ return c2s_signature if use_c2s?
39
+ ''
40
+ end
41
+
42
+ def c2s_signature
43
+ Digest::MD5.hexdigest(@api.uid + parameters.sort.join + @api.private_key)
44
+ end
45
+
46
+ def s2s_signature
47
+ Digest::MD5.hexdigest(parameters.sort.join + @api.secret_key)
48
+ end
49
+
50
+ def parameters
51
+ params = {app_id: @api.app_id, method: @method}
52
+ params.merge!({session_key: @api.session_key}) if @api.session_key
53
+ params.merge!({format: @api.format}) if @api.format
54
+ params.merge!(@method_params) if @method_params
55
+ params.merge!({secure: 1}) if use_s2s?
56
+
57
+ params.to_a.map{|p| p.join('=')}
58
+ end
59
+
60
+ def handle_response response
61
+ response = response.body unless response.is_a?(String)
62
+
63
+ if @api.format == Format::XML
64
+ response = REXML::Document.new(response).root
65
+
66
+ error = response if response.name == 'error'
67
+ if error
68
+ raise MailRU::API::Error.create(
69
+ error.elements['error_code'].text,
70
+ error.elements['error_msg'].text
71
+ )
72
+ end
73
+ else
74
+ response = JSON.parse(response)
75
+ if response.is_a?(Hash)
76
+ error = response['error']
77
+ if error
78
+ raise MailRU::API::Error.create(error['error_code'], error['error_msg'])
79
+ end
80
+ end
81
+ end
82
+
83
+ response
84
+ end
85
+ end
86
+
87
+ class GetRequest < Request
88
+ def get
89
+ request = URI.escape(PATH + '?' + parameters.push("sig=#{signature}").join('&'))
90
+ handle_response(Net::HTTP.get(URI(request)))
91
+ end
92
+ end
93
+
94
+ class PostRequest < Request
95
+ def post
96
+ uri = URI(PATH)
97
+ request = Net::HTTP::Post.new(uri.path)
98
+ request.body = parameters.push("sig=#{signature}").join('&')
99
+ handle_response(Net::HTTP.new(uri.host, uri.port).request(request))
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,39 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Stream
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def comment params = {}
11
+ GetRequest.new(@api, 'stream.comment', params).get
12
+ end
13
+
14
+ def get params = {}
15
+ GetRequest.new(@api, 'stream.get', params).get
16
+ end
17
+
18
+ def get_by_author params = {}
19
+ GetRequest.new(@api, 'stream.getByAuthor', params).get
20
+ end
21
+
22
+ def like params = {}
23
+ GetRequest.new(@api, 'stream.like', params).get
24
+ end
25
+
26
+ def post params = {}
27
+ PostRequest.new(@api, 'stream.post', params).post
28
+ end
29
+
30
+ def share params ={}
31
+ PostRequest.new(@api, 'stream.share', params).post
32
+ end
33
+
34
+ def unlike params = {}
35
+ GetRequest.new(@api, 'stream.unlike', params).get
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ #:encoding: utf-8
2
+
3
+ module MailRU
4
+ class API
5
+ class Users
6
+ def initialize api
7
+ @api = api
8
+ end
9
+
10
+ def get_balance
11
+ GetRequest.new(@api, 'users.getBalance', params).get
12
+ end
13
+
14
+ def get_info params = {}
15
+ GetRequest.new(@api, 'users.getInfo', params).get
16
+ end
17
+
18
+ def has_app_permission params = {}
19
+ GetRequest.new(@api, 'users.hasAppPermission', params).get
20
+ end
21
+
22
+ def is_app_user
23
+ GetRequest.new(@api, 'users.isAppUser', params).get
24
+ end
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailru-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Demin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2010-04-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: MailRU API for Ruby
15
+ email: demin.alexey@inbox.ru
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/mailru-api.rb
21
+ - lib/mailru-api/audio.rb
22
+ - lib/mailru-api/error.rb
23
+ - lib/mailru-api/events.rb
24
+ - lib/mailru-api/friends.rb
25
+ - lib/mailru-api/guestbook.rb
26
+ - lib/mailru-api/mail.rb
27
+ - lib/mailru-api/messages.rb
28
+ - lib/mailru-api/mobile.rb
29
+ - lib/mailru-api/notifications.rb
30
+ - lib/mailru-api/photos.rb
31
+ - lib/mailru-api/request.rb
32
+ - lib/mailru-api/stream.rb
33
+ - lib/mailru-api/users.rb
34
+ homepage: ''
35
+ licenses:
36
+ - MIT
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: MailRU API for Ruby
59
+ test_files: []
60
+ has_rdoc: