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.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sms50X/client.rb', line 8

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.



6
7
8
# File 'lib/sms50X/client.rb', line 6

def api_key
  @api_key
end

#country_codeObject

Returns the value of attribute country_code.



6
7
8
# File 'lib/sms50X/client.rb', line 6

def country_code
  @country_code
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/sms50X/client.rb', line 6

def host
  @host
end

Instance Method Details

#balanceObject



27
28
29
30
# File 'lib/sms50X/client.rb', line 27

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

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



37
38
39
40
# File 'lib/sms50X/client.rb', line 37

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



32
33
34
35
# File 'lib/sms50X/client.rb', line 32

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



22
23
24
25
# File 'lib/sms50X/client.rb', line 22

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