nrsgateway 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/nrs_gateway.rb +93 -0
  2. metadata +128 -0
@@ -0,0 +1,93 @@
1
+ require 'global_phone'
2
+ require 'net/https'
3
+
4
+ module NrsGateway
5
+
6
+ APIUri = URI.parse("https://gateway.nrsgateway.com/send.php")
7
+
8
+ class << self
9
+
10
+ @@login = ""
11
+ @@password = ""
12
+ @@from = ""
13
+
14
+ def login=(username)
15
+ @@login = username
16
+ end
17
+
18
+ def password=(secret)
19
+ @@password = secret
20
+ end
21
+
22
+ def from=(sender)
23
+ @@from = sender
24
+ end
25
+
26
+ # Send a petition to NrsGateway
27
+ #
28
+ # Example:
29
+ # >> NrsGateway.send_sms(:login => "login",
30
+ # :password => "password",
31
+ # :from => "Sender",
32
+ # :destination => "34600000001" || ["34600000001", "34600000002"],
33
+ # :message => "Message with 160 chars maximum")
34
+ def send_sms(options = {})
35
+ # Check for login
36
+ login = options[:login] || @@login
37
+ raise ArgumentError, "Login must be present" unless login and not (login.strip.size == 0)
38
+
39
+ # Check for password
40
+ password = options[:password] || @@password
41
+ raise ArgumentError, "Password must be present" unless password and not (password.strip.size == 0)
42
+
43
+ from = options[:from] || @@from
44
+ raise ArgumentError, "Sender length 11 characters maximum" if (from.size > 11)
45
+
46
+ # Multiple destinations support
47
+ options[:destination] = [options[:destination]] unless options[:destination].kind_of?(Array)
48
+
49
+ destinations = []
50
+ options[:destination].each do |phone|
51
+ raise ArgumentError, "Recipient must be a telephone number with international format: #{phone.to_s}" unless parsed = GlobalPhone.parse(phone.to_s)
52
+ destinations << parsed.international_string.gsub("+", "") # Remove + from international string
53
+ end
54
+
55
+ message = options[:message].to_s
56
+ raise ArgumentError, "Message must be present" if message.nil? or (message.strip.size == 0)
57
+ raise ArgumentError, "Message is 160 chars maximum" if message.size > 160
58
+
59
+ uri = APIUri.dup
60
+ params = { 'username' => login,
61
+ 'password' => password,
62
+ 'from' => from,
63
+ 'to' => destinations.join(" "),
64
+ 'text' => message,
65
+ 'coding' => "0",
66
+ 'trsec' => "1"}
67
+ uri.query = URI.escape(params.map{ |k,v| "#{k}=#{v}" }.compact.join('&'))
68
+ https = Net::HTTP.new(APIUri.host, APIUri.port)
69
+ https.use_ssl = true
70
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
71
+ req = Net::HTTP::Get.new(uri.request_uri)
72
+
73
+ response = https.start { |cx| cx.request(req) }
74
+ if response.code == "200"
75
+ begin
76
+ match = response.body.match(/(\d+):\s((\s|\w)+)(\.\sID\s(\d+))?/i)
77
+ if match
78
+ result = {:code => match[1].to_i, :description => match[2], :id => match[5]}
79
+ else
80
+ result = {:code => response.body, :description => "Unknown error"}
81
+ end
82
+ rescue
83
+ # Try to get petition result
84
+ result = {:code => response.body}
85
+ end
86
+ return result
87
+ else
88
+ raise RuntimeError, "Server responded with code #{response.code}: #{response.body}"
89
+ end
90
+ end
91
+
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nrsgateway
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Angel Garc\xC3\xADa P\xC3\xA9rez"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2015-07-22 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: global_phone
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 21
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 1
34
+ version: 1.0.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 49
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 7
50
+ version: 0.8.7
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ hash: 25
62
+ segments:
63
+ - 1
64
+ - 3
65
+ - 1
66
+ version: 1.3.1
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ description: A gateway to send SMS using HTTP, through NRSGateway, written in ruby
84
+ email: wage83@gmail.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files: []
90
+
91
+ files:
92
+ - lib/nrs_gateway.rb
93
+ has_rdoc: true
94
+ homepage: http://nrsgateway.com/http_api_peticion.php
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.4.2
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: NRSGateway SMS send through HTTP
127
+ test_files: []
128
+