class Pechkin::Connection
Constants
- API_URL
Attributes
endpoint[R]
Public Class Methods
new(username, password, endpoint = API_URL)
click to toggle source
Initializes new connection instance
@param username [String] Service username @param password [String] Password
# File lib/pechkinrb/pechkin.rb, line 13 def initialize(username, password, endpoint = API_URL) @username, @password = username, password @endpoint = endpoint end
Public Instance Methods
call_method(method, params = {})
click to toggle source
Invokes API method by name
@param method [String] Method name, corresponding API reference pechkin-mail.ru/api/ @param params [Hash] Params to be passed
# File lib/pechkinrb/pechkin.rb, line 35 def call_method(method, params = {}) response = connection.post '/', {method: method}.merge(credentials).merge(params) err_code = response.body["response"]["msg"]["err_code"] case err_code when 0 response.body["response"]["data"] when 4 raise Pechkin::NoDataException.new(response.body["response"]["msg"]["text"]) else raise Pechkin::ApiException.new(response.body["response"]["msg"]["text"]) end end
connection()
click to toggle source
Memoized Faraday connection factory
@return Faraday connection instance
# File lib/pechkinrb/pechkin.rb, line 23 def connection @conn ||= Faraday.new(:url => endpoint) do |faraday| faraday.request :url_encoded faraday.response :json faraday.adapter Faraday.default_adapter end end
get_list(id)
click to toggle source
get_member(email)
click to toggle source
Invokes ‘lists.get_member’ API method
@param email [String] Email for search @return [Array] Array of Pechkin::Member
instances
# File lib/pechkinrb/pechkin.rb, line 74 def get_member(email) call_method('lists.get_member', {email: email}).map {|member| Pechkin::Member.new(connection, member)} end
lists(params = {})
click to toggle source
Invokes ‘lists.get’ API method
@param params [Hash] Params to be passed @return [Array] Array of Pechkin::List
instances
# File lib/pechkinrb/pechkin.rb, line 53 def lists(params = {}) call_method('lists.get', params).map {|list_raw| Pechkin::List.new(self, list_raw)} end
Also aliased as: get
unsubscribe_member(params)
click to toggle source
Invokes ‘lists.unsubscribe_member’ API method
@param params [Hash] Params to be passed @return [Fixnum] Count of unsubscribed members
# File lib/pechkinrb/pechkin.rb, line 82 def unsubscribe_member(params) call_method('lists.unsubscribe_member', params)['unsubscribed'] end
Private Instance Methods
credentials()
click to toggle source
# File lib/pechkinrb/pechkin.rb, line 88 def credentials {username: @username, password: @password} end