mailru_api 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mailru_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Artyom Keydnov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # MailruApi
2
+
3
+ ## Установка
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'mailru_api'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install mailru_api
16
+
17
+ ## Использование
18
+
19
+ client = ::MailruApi::Client.new app_id, api_secret, access_token
20
+ client.stream.get_by_author
21
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/mailru_api.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "mailru_api/version"
2
+
3
+ module MailruApi
4
+ autoload :Client, 'mailru_api/client'
5
+ end
@@ -0,0 +1,87 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'digest/md5'
4
+ require 'json'
5
+ require 'active_support/inflector'
6
+
7
+ # Author:: Keydunov Artem
8
+ # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
9
+
10
+ # Mail.ru REST API Документация - http://api.mail.ru/docs/reference/rest/
11
+
12
+ # Пример использования:
13
+ # client = ::MailruApi::Client.new app_id, api_secret, access_token
14
+ # client.stream.get_by_author
15
+
16
+ module MailruApi
17
+ class Client
18
+ MAILRU_API_URL = "http://www.appsmail.ru/platform/api?"
19
+ MAILRUAPI_METHODS = %w(audio stream users events friends payments photos messages guestbook notifications
20
+ widget mail)
21
+
22
+ attr_accessor :client_id, :client_secret, :access_token
23
+
24
+ # ------ Конструктор, аргументы:
25
+ # * client_id — идентификатор вашего сайта
26
+ # * client_secret — секретный ключ вашего сайта, выданный при регистрации
27
+ # * access_token — это идентификатор сессии, необходимый для работы с REST API.
28
+ # ------ Подробнее см. http://api.mail.ru/docs/guides/oauth/sites/
29
+ def initialize client_id, client_secret, access_token, prefix = nil
30
+ @client_id, @client_secret, @access_token, @prefix = client_id, client_secret, access_token, prefix
31
+ end
32
+
33
+ def request method, params = {}
34
+ params[:method] = @prefix ? "#{@prefix}.#{method}" : method
35
+ params[:app_id] = @client_id
36
+ params[:secure] = "1"
37
+ params[:session_key] ||= @access_token
38
+ params[:sig] = sig(params.tap do |s|
39
+ # stringify keys
40
+ s.keys.each {|k| s[k.to_s] = s.delete k }
41
+ end )
42
+ puts params
43
+ response = JSON.parse(Net::HTTP.post_form(URI.parse(MAILRU_API_URL), params).body)
44
+ if !response.is_a?(Array) and response['error']
45
+ raise ServerError.new self, method, params, response['error']['error_msg']
46
+ end
47
+ response
48
+ end
49
+
50
+ # ------ Создаем подпись запроса (http://api.mail.ru/docs/guides/restapi/#sig)
51
+ def sig(params)
52
+ Digest::MD5::hexdigest(
53
+ params.keys.sort.map{|key| "#{key}=#{params[key]}"}.join +
54
+ client_secret)
55
+ end
56
+
57
+ # ------ Перехват неизвестных методов
58
+ def method_missing(name, *arg)
59
+ #Позволяет использовать названия методов в стиле Ruby (get_by_author вместо getByAuthor)
60
+ method = name.to_s.camelize(:lower)
61
+ request method, *arg
62
+ end
63
+
64
+ # ------ Создаем методы (audio, stream и т.д.)
65
+ MAILRUAPI_METHODS.each do |name|
66
+ self.send :define_method, name do
67
+ instance_variable_set("@#{name}", ::MailruApi::Client.new(@client_id, @client_secret, @access_token, name))
68
+ end
69
+ end
70
+
71
+
72
+ # ------ Errors
73
+ # Base error class
74
+ class Error < ::StandardError; end
75
+
76
+ # Server side error
77
+ class ServerError < Error
78
+ attr_accessor :session, :method, :params, :error
79
+ def initialize(client, method, params, error)
80
+ super "Server side error calling Mail.ru API method: #{error}"
81
+ @client, @method, @params, @error = client, method, params, error
82
+ end
83
+ end
84
+
85
+
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module MailruApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mailru_api/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "mailru_api"
8
+ gem.version = MailruApi::VERSION
9
+ gem.authors = ["Artyom Keydunov"]
10
+ gem.email = ["artyom.keydunov@gmail.com"]
11
+ gem.description = %q{Gem для общения с Mail.ru API}
12
+ gem.summary = %q{Gem для общения с Mail.ru API}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('activesupport')
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailru_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Artyom Keydunov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Gem для общения с Mail.ru API
31
+ email:
32
+ - artyom.keydunov@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/mailru_api.rb
43
+ - lib/mailru_api/client.rb
44
+ - lib/mailru_api/version.rb
45
+ - mailru_api.gemspec
46
+ homepage: ''
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Gem для общения с Mail.ru API
70
+ test_files: []
71
+ has_rdoc: