Class: Dashamail::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/dashamailrb/dashamail.rb

Constant Summary collapse

API_URL =
'https://api.dashamail.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, endpoint = API_URL) ⇒ Connection

Initializes new connection instance


13
14
15
16
# File 'lib/dashamailrb/dashamail.rb', line 13

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint


18
19
20
# File 'lib/dashamailrb/dashamail.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/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

#connectionObject

Memoized Faraday connection factory


23
24
25
26
27
28
29
# 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_list(id) ⇒ Dashamail::List

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


62
63
64
# File 'lib/dashamailrb/dashamail.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/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 = {}) ⇒ Array Also known as: get

Invokes 'lists.get' API method


53
54
55
# 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

#unsubscribe_member(params) ⇒ Fixnum

Invokes 'lists.unsubscribe_member' API method


82
83
84
# File 'lib/dashamailrb/dashamail.rb', line 82

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