class Dashamail::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/dashamailrb/dashamail.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 dashamail.ru/api/ @param params [Hash] Params to be passed

# File lib/dashamailrb/dashamail.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 Dashamail::NoDataException.new(response.body["response"]["msg"]["text"])
  else
    raise Dashamail::ApiException.new(response.body["response"]["msg"]["text"])
  end
end
connection() click to toggle source

Memoized Faraday connection factory

@return Faraday connection instance

# File lib/dashamailrb/dashamail.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(params = {})

'list.get' method alias

Alias for: lists
get_list(id) click to toggle source

Invokes 'lists.get' API method to retrieve single List object

@param id [Fixnum] List id @return [Dashamail::List] List instance

# File lib/dashamailrb/dashamail.rb, line 62
def get_list(id)
  lists(list_id: id)[0]
end
get_member(email) click to toggle source

Invokes 'lists.get_member' API method

@param email [String] Email for search @return [Array] Array of Dashamail::Member instances

# File lib/dashamailrb/dashamail.rb, line 74
def get_member(email)
  call_method('lists.get_member', {email: email}).map {|member| Dashamail::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 Dashamail::List instances

# File lib/dashamailrb/dashamail.rb, line 53
def lists(params = {})
  call_method('lists.get', params).map {|list_raw| Dashamail::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/dashamailrb/dashamail.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/dashamailrb/dashamail.rb, line 88
def credentials
  {username: @username, password: @password}
end