Class: Laposta::Client

Inherits:
Object
  • Object
show all
Includes:
Accounts, Campaigns, Fields, Lists, Login, Reports, Subscribers, Webhooks
Defined in:
lib/laposta/client.rb,
lib/laposta/client/lists.rb,
lib/laposta/client/login.rb,
lib/laposta/client/fields.rb,
lib/laposta/client/reports.rb,
lib/laposta/client/accounts.rb,
lib/laposta/client/webhooks.rb,
lib/laposta/client/campaigns.rb,
lib/laposta/client/subscribers.rb

Defined Under Namespace

Modules: Accounts, Campaigns, Fields, Lists, Login, Reports, Subscribers, Webhooks

Instance Method Summary collapse

Methods included from Webhooks

#create_webhook!, #delete_webhook!, #get_webhook, #get_webhooks_for_list, #update_webhook!

Methods included from Subscribers

#create_subscriber!, #delete_subscriber!, #get_subscriber, #get_subscribers_for_list, #update_subscriber!

Methods included from Reports

#get_report, #reports

Methods included from Login

#login

Methods included from Lists

#create_list!, #delete_list!, #get_list, #lists, #purge_list!, #update_list!

Methods included from Fields

#create_field!, #delete_field!, #get_field, #get_fields_for_list, #update_field!

Methods included from Campaigns

#campaigns, #create_campaign!, #delete_campaign!, #fill_campaign_content!, #get_campaign, #get_campaign_content, #schedule_campaign!, #send_campaign!, #test_campaign!, #update_campaign!

Methods included from Accounts

#accounts, #create_account!, #get_account

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
# File 'lib/laposta/client.rb', line 27

def initialize(options={})
  auth_token = options[:auth_token]
  user_agent = options[:user_agent] || "Laposta Ruby Gem #{VERSION}"
  @headers = HTTP.basic_auth(user: auth_token, pass: '')
                 .headers({
                   Accept: "application/json",
                   "User-Agent": user_agent,
                   "Content-Type": "application/x-www-form-urlencoded"
                 })
end

Instance Method Details

#create(path, body) ⇒ Object



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

def create(path, body)
  res = @headers.post("#{base_url}#{path}", body: format_query(flatten_hash(body)))
  response = JSON.parse(res.to_s)
  if !res.status.success?
    raise Error.from_response(res)
  end
  response
end

#create_without_response(path, body) ⇒ Object



83
84
85
86
87
88
# File 'lib/laposta/client.rb', line 83

def create_without_response(path, body)
  res = @headers.post("#{base_url}#{path}", body: format_query(flatten_hash(body)))
  if !res.status.success?
    raise Error.from_response(res)
  end
end

#delete(path, body) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/laposta/client.rb', line 99

def delete(path, body)
  res = @headers.delete("#{base_url}#{path}", body: format_query(flatten_hash(body)))
  response = JSON.parse(res.to_s)
  if !res.status.success?
    raise Error.from_response(res)
  end
  response
end

#get(path) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/laposta/client.rb', line 48

def get(path)
  res = @headers.get("#{base_url}#{path}")
  if !res.status.success?
    raise Error.from_response(res)
  end
  JSON.parse(res.to_s)
end

#get_plain(path) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/laposta/client.rb', line 56

def get_plain(path)
  headers_copy = @headers.dup
  res = @headers.accept("text/plain").get("#{base_url}#{path}")
  if !res.status.success?
    raise Error.from_response(res)
  end
  res.to_s
end

#get_raw(path) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/laposta/client.rb', line 65

def get_raw(path)
  headers_copy = @headers.dup
  res = @headers.get("#{base_url}#{path}")
  if !res.status.success?
    raise Error.from_response(res)
  end
  res
end

#list(path, params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/laposta/client.rb', line 38

def list(path, params = {})
  query = format_query(params)
  url = "#{base_url}#{path}?#{query}"
  res = @headers.get(url)
  if !res.status.success?
    raise Error.from_response(res)
  end
  JSON.parse(res.to_s)
end

#update(path, body) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/laposta/client.rb', line 90

def update(path, body)
  res = @headers.post("#{base_url}#{path}", body: format_query(flatten_hash(body)))
  response = JSON.parse(res.to_s)
  if !res.status.success?
    raise Error.from_response(res)
  end
  response
end