Class: MailruApi::Client
- Inherits:
-
Object
- Object
- MailruApi::Client
- Defined in:
- lib/mailru_api/client.rb
Defined Under Namespace
Classes: Error, ServerError
Constant Summary collapse
- MAILRU_API_URL =
"http://www.appsmail.ru/platform/api?"- MAILRUAPI_METHODS =
%w(audio stream users events friends payments photos messages guestbook notifications widget mail)
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
Instance Method Summary collapse
-
#initialize(client_id, client_secret, access_token, prefix = nil) ⇒ Client
constructor
—— Конструктор, аргументы: * client_id — идентификатор вашего сайта * client_secret — секретный ключ вашего сайта, выданный при регистрации * access_token — это идентификатор сессии, необходимый для работы с REST API.
-
#method_missing(name, *arg) ⇒ Object
—— Перехват неизвестных методов.
- #request(method, params = {}) ⇒ Object
-
#sig(params) ⇒ Object
—— Создаем подпись запроса (api.mail.ru/docs/guides/restapi/#sig).
Constructor Details
#initialize(client_id, client_secret, access_token, prefix = nil) ⇒ Client
—— Конструктор, аргументы:
-
client_id — идентификатор вашего сайта
-
client_secret — секретный ключ вашего сайта, выданный при регистрации
-
access_token — это идентификатор сессии, необходимый для работы с REST API.
—— Подробнее см. api.mail.ru/docs/guides/oauth/sites/
29 30 31 |
# File 'lib/mailru_api/client.rb', line 29 def initialize client_id, client_secret, access_token, prefix = nil @client_id, @client_secret, @access_token, @prefix = client_id, client_secret, access_token, prefix end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arg) ⇒ Object
—— Перехват неизвестных методов
58 59 60 61 62 |
# File 'lib/mailru_api/client.rb', line 58 def method_missing(name, *arg) #Позволяет использовать названия методов в стиле Ruby (get_by_author вместо getByAuthor) method = name.to_s.camelize(:lower) request method, *arg end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
22 23 24 |
# File 'lib/mailru_api/client.rb', line 22 def access_token @access_token end |
#client_id ⇒ Object
Returns the value of attribute client_id.
22 23 24 |
# File 'lib/mailru_api/client.rb', line 22 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
22 23 24 |
# File 'lib/mailru_api/client.rb', line 22 def client_secret @client_secret end |
Instance Method Details
#request(method, params = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mailru_api/client.rb', line 33 def request method, params = {} params[:method] = @prefix ? "#{@prefix}.#{method}" : method params[:app_id] = @client_id params[:secure] = "1" params[:session_key] ||= @access_token params[:sig] = sig(params.tap do |s| # stringify keys s.keys.each {|k| s[k.to_s] = s.delete k } end ) puts params response = JSON.parse(Net::HTTP.post_form(URI.parse(MAILRU_API_URL), params).body) if !response.is_a?(Array) and response['error'] raise ServerError.new self, method, params, response['error']['error_msg'] end response end |
#sig(params) ⇒ Object
—— Создаем подпись запроса (api.mail.ru/docs/guides/restapi/#sig)
51 52 53 54 55 |
# File 'lib/mailru_api/client.rb', line 51 def sig(params) Digest::MD5::hexdigest( params.keys.sort.map{|key| "#{key}=#{params[key]}"}.join + client_secret) end |