Class: Pechkin::Connection
- Inherits:
-
Object
- Object
- Pechkin::Connection
- Defined in:
- lib/pechkinrb/pechkin.rb
Constant Summary collapse
- API_URL =
'https://api.pechkin-mail.ru'
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
-
#call_method(method, params = {}) ⇒ Object
Invokes API method by name.
-
#connection ⇒ Object
Memoized Faraday connection factory.
-
#get_list(id) ⇒ Pechkin::List
Invokes ‘lists.get’ API method to retrieve single List object.
-
#get_member(email) ⇒ Array
Invokes ‘lists.get_member’ API method.
-
#initialize(username, password, endpoint = API_URL) ⇒ Connection
constructor
Initializes new connection instance.
-
#lists(params = {}) ⇒ Array
(also: #get)
Invokes ‘lists.get’ API method.
-
#unsubscribe_member(params) ⇒ Fixnum
Invokes ‘lists.unsubscribe_member’ API method.
Constructor Details
#initialize(username, password, endpoint = API_URL) ⇒ Connection
Initializes new connection instance
13 14 15 16 |
# File 'lib/pechkinrb/pechkin.rb', line 13 def initialize(username, password, endpoint = API_URL) @username, @password = username, password @endpoint = endpoint end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
18 19 20 |
# File 'lib/pechkinrb/pechkin.rb', line 18 def endpoint @endpoint end |
Instance Method Details
#call_method(method, params = {}) ⇒ Object
Invokes API method by name
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
Memoized Faraday connection factory
23 24 25 26 27 28 29 |
# 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) ⇒ Pechkin::List
Invokes ‘lists.get’ API method to retrieve single List object
62 63 64 |
# File 'lib/pechkinrb/pechkin.rb', line 62 def get_list(id) lists(list_id: id)[0] end |
#get_member(email) ⇒ Array
Invokes ‘lists.get_member’ API method
74 75 76 |
# 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 = {}) ⇒ Array Also known as: get
Invokes ‘lists.get’ API method
53 54 55 |
# 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 |
#unsubscribe_member(params) ⇒ Fixnum
Invokes ‘lists.unsubscribe_member’ API method
82 83 84 |
# File 'lib/pechkinrb/pechkin.rb', line 82 def unsubscribe_member(params) call_method('lists.unsubscribe_member', params)['unsubscribed'] end |