lib/globalsms/sms.rb in globalsms-0.3.1 vs lib/globalsms/sms.rb in globalsms-0.4.0

- old
+ new

@@ -1,55 +1,32 @@ -require "globalsms/version" -require 'httpclient' -require 'json' - module GlobalSMS class SMS def initialize(api_key, api_secret) - @api_base_url = "http://api.globalhaberlesme.com" #TODO: DEFAULTS a çıkart @api_key = api_key @api_secret = api_secret + @default_args = { + time: 'now', + turkish_character: '0' + } end - - def single_send(argv) - argv = { - time: "now", - turkish_character: "0" - }.merge(argv) - body = "data=#{argv.to_json.to_s}" - uri = "#{@api_base_url}/sms/send/single?key=#{@api_key}&secret=#{@api_secret}" - - c = HTTPClient.new - return JSON.parse(c.post(uri, body).body) + def single_send(message) + message = @default_args.merge(message) + post_to_api('single', message) end - def bulk_send(argv) - argv = { - time: "now", - turkish_character: "0" - }.merge(argv) - - body = "data=#{argv.to_json.to_s}" - uri = "#{@api_base_url}/sms/send/single?key=#{@api_key}&secret=#{@api_secret}" - - c = HTTPClient.new - return JSON.parse(c.post(uri, body).body) + def multi_send(messages) + messages = messages.map { |message| @default_args.merge(message) } + post_to_api('multi', messages) end - def multi_send(argv) + private - argv_def = { - time: "now", - turkish_character: "0" - } - - argv_array = argv.map { |arg| argv_def.merge(arg) } - - body = "data=#{argv_array.to_json.to_s}" - uri = "#{@api_base_url}/sms/send/multi?key=#{@api_key}&secret=#{@api_secret}" - - c = HTTPClient.new - return JSON.parse(c.post(uri, body).body) + def post_to_api(to, data) + client = HTTPClient.new + data = "data=#{data.to_json.to_s}" + uri = "#{API_BASE_URL}/sms/send/#{to}?key=#{@api_key}&secret=#{@api_secret}" + response = client.post(uri, data) + JSON.parse(response.body) end end -end \ No newline at end of file +end