Class: Pechkin::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/pechkinrb/pechkin.rb

Constant Summary collapse

API_URL =
'https://api.pechkin-mail.ru'

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Connection

Initializes new connection instance

Parameters:

  • username (String)

    Service username

  • password (String)

    Password


13
14
15
# File 'lib/pechkinrb/pechkin.rb', line 13

def initialize(username, password)
  @username, @password = username, password
end

Instance Method Details

#call_method(method, params = {}) ⇒ Object

Invokes API method by name

Parameters:

  • method (String)

    Method name, corresponding API reference pechkin-mail.ru/api/

  • params (Hash) (defaults to: {})

    Params to be passed


32
33
34
35
36
37
38
39
# File 'lib/pechkinrb/pechkin.rb', line 32

def call_method(method, params = {})
  response = connection.post '/', {method: method}.merge(credentials).merge(params)
  err_code = response.body["response"]["msg"]["err_code"]
  unless err_code == 0
    raise Pechkin::ApiException.new(response.body["response"]["msg"]["text"])
  end
  response.body["response"]["data"]
end

#connectionObject

Memoized Faraday connection factory

Returns:

  • Faraday connection instance


20
21
22
23
24
25
26
# File 'lib/pechkinrb/pechkin.rb', line 20

def connection
  @conn ||= Faraday.new(:url => 'https://api.pechkin-mail.ru') do |faraday|
    faraday.request  :url_encoded
    faraday.response :json
    faraday.adapter  Faraday.default_adapter
  end
end

#get_list(id) ⇒ Pechkin::List

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

Parameters:

  • id (Fixnum)

    List id

Returns:


54
55
56
# File 'lib/pechkinrb/pechkin.rb', line 54

def get_list(id)
  lists(list_id: id)[0]
end

#get_member(email) ⇒ Array

Invokes 'lists.get_member' API method

Parameters:

  • email (String)

    Email for search

Returns:

  • (Array)

    Array of Pechkin::Member instances


66
67
68
# File 'lib/pechkinrb/pechkin.rb', line 66

def get_member(email)
  call_method('lists.get_member', {email: email}).map {|member| Pechkin::Member.new(connection, member)}
end

#lists(params = {}) ⇒ Array Also known as: get

Invokes 'lists.get' API method

Parameters:

  • params (Hash) (defaults to: {})

    Params to be passed

Returns:

  • (Array)

    Array of Pechkin::List instances


45
46
47
# File 'lib/pechkinrb/pechkin.rb', line 45

def lists(params = {})
  call_method('lists.get', params).map {|list_raw| Pechkin::List.new(self, list_raw)}
end

#unsubscribe_member(params) ⇒ Fixnum

Invokes 'lists.unsubscribe_member' API method

Parameters:

  • params (Hash)

    Params to be passed

Returns:

  • (Fixnum)

    Count of unsubscribed members


74
75
76
# File 'lib/pechkinrb/pechkin.rb', line 74

def unsubscribe_member(params)
  call_method('lists.unsubscribe_member', params)['unsubscribed']
end