Class: Sms50X::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sms50X/client.rb', line 11

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  @api_key = get_api_key(args[0])
  @country_code = get_country_code(args[1])

  if [@api_key, @country_code].any? { |k| k.nil? }
    raise ArgumentError, 'API key and country code are required'
  end

  @host = get_host

end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#country_codeObject

Returns the value of attribute country_code.



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

def country_code
  @country_code
end

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

Instance Method Details

#balanceObject



30
31
32
33
# File 'lib/sms50X/client.rb', line 30

def balance
  response = Faraday.get("#{host}/balance/#{api_key}")
  response.body.to_i
end

#bulk_send(message, phone_numbers) ⇒ Object



45
46
47
48
# File 'lib/sms50X/client.rb', line 45

def bulk_send(message, phone_numbers)
  response = Faraday.post("#{host}/xml", build_bulk_xml(message, phone_numbers))
  response.body
end

#get_replies(date = Date.today, output_format = :json) ⇒ Object



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

def get_replies(date = Date.today, output_format = :json)
  response = Faraday.get("#{host}/smsin/#{api_key}/#{output_format.to_s}/#{date.strftime("%d-%m-%Y")}")
  response.body
end

#get_stats(month = Date.today.month) ⇒ Object



35
36
37
38
# File 'lib/sms50X/client.rb', line 35

def get_stats(month = Date.today.month)
  response = Faraday.get("#{host}/stat/#{api_key}/#{month}")
  response.body.to_i
end

#send_message(phone, message) ⇒ Object



25
26
27
28
# File 'lib/sms50X/client.rb', line 25

def send_message(phone, message)
  response = Faraday.get("#{host}/sms/#{api_key}/t=#{phone}&m=#{escape(message)}")
  response.body.to_i
end