Class: DotMailer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_mailer/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_user, api_pass) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/dot_mailer/client.rb', line 9

def initialize(api_user, api_pass)
  self.api_user = api_user
  self.api_pass = api_pass
end

Instance Method Details

#delete(path) ⇒ Object



76
77
78
79
80
81
# File 'lib/dot_mailer/client.rb', line 76

def delete(path)
  rescue_api_errors do
    endpoint = endpoint_for(path)
    RestClient.delete endpoint, :accept => :json
  end
end

#get(path) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/dot_mailer/client.rb', line 14

def get(path)
  rescue_api_errors do
    endpoint = endpoint_for(path)
    response = RestClient.get endpoint, :accept => :json

    JSON.parse response
  end
end

#get_csv(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dot_mailer/client.rb', line 23

def get_csv(path)
  rescue_api_errors do
    endpoint = endpoint_for(path)
    response = RestClient.get endpoint, :accept => :csv

    # Force the encoding to UTF-8 as that is what the
    # API returns (otherwise it will be ASCII-8BIT, see
    # http://bugs.ruby-lang.org/issues/2567)
    response.force_encoding('UTF-8')

    # Remove the UTF-8 BOM if present
    response.sub!(/\A\xEF\xBB\xBF/, '')

    CSV.parse response, :headers => true
  end
end

#inspectObject



87
88
89
# File 'lib/dot_mailer/client.rb', line 87

def inspect
  to_s
end

#post(path, data, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dot_mailer/client.rb', line 54

def post(path, data, options = {})
  rescue_api_errors do
    endpoint = endpoint_for(path)
    response = RestClient.post endpoint, data, options.merge(:accept => :json)

    JSON.parse response
  end
end

#post_csv(path, csv) ⇒ Object

Need to use a Tempfile as the API will not accept CSVs without filenames



46
47
48
49
50
51
52
# File 'lib/dot_mailer/client.rb', line 46

def post_csv(path, csv)
  file = Tempfile.new(['dotmailer-contacts', '.csv'])
  file.write csv
  file.rewind

  post path, :csv => file
end

#post_json(path, params) ⇒ Object



40
41
42
# File 'lib/dot_mailer/client.rb', line 40

def post_json(path, params)
  post path, params.to_json, :content_type => :json
end

#put(path, data, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/dot_mailer/client.rb', line 67

def put(path, data, options = {})
  rescue_api_errors do
    endpoint = endpoint_for(path)
    response = RestClient.put endpoint, data, options.merge(:accept => :json)

    JSON.parse response
  end
end

#put_json(path, params) ⇒ Object



63
64
65
# File 'lib/dot_mailer/client.rb', line 63

def put_json(path, params)
  put path, params.to_json, :content_type => :json
end

#to_sObject



83
84
85
# File 'lib/dot_mailer/client.rb', line 83

def to_s
  "#{self.class.name} api_user: #{api_user}"
end