smspromote 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module SmsPromote
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
4
4
 
5
5
  require 'smspromote/message'
@@ -1,10 +1,12 @@
1
1
  require "net/http"
2
+ require "net/https"
2
3
  require "uri"
3
4
 
4
5
  module SmsPromote
5
6
  class Gateway
6
- SECURE = "https://gateway.smspromote.de"
7
- INSECURE = "http://gateway.smspromote.de"
7
+ DOMAIN = "gateway.smspromote.de".freeze
8
+ SECURE = "https://#{DOMAIN}"
9
+ INSECURE = "http://#{DOMAIN}"
8
10
 
9
11
  # returns the api key as string. The api key will be read
10
12
  # from the ".smspromote.key" file from the home directroy of
@@ -45,7 +47,10 @@ module SmsPromote
45
47
  options[:from] = @options[:originator]
46
48
  end
47
49
 
48
- response = Net::HTTP.post_form(URI.parse(service_url + "/"), options)
50
+ url = service_url
51
+ url.path = "/"
52
+ response = Net::HTTP.post_form(url, options)
53
+
49
54
  data = parse_response(response.body)
50
55
  message.after_send(data[:code], data[:message_id], data[:cost], data[:count])
51
56
  message
@@ -53,8 +58,9 @@ module SmsPromote
53
58
 
54
59
  # returns the credits left for the gateway
55
60
  def credits
56
- url = URI.parse(service_url + "/credits/?key=#{@api_key}")
57
- Net::HTTP.get(url).to_f
61
+ session do |http|
62
+ http.get("/credits/?key=#{@api_key}").body.to_f
63
+ end
58
64
  end
59
65
 
60
66
  # returns the response message hash based on the body data
@@ -72,15 +78,36 @@ module SmsPromote
72
78
  def debug?
73
79
  !!@options[:debug]
74
80
  end
81
+
82
+ # returns true if the service should use https
83
+ def secure?
84
+ !!@options[:secure]
85
+ end
86
+
87
+ # returns true if the messages should contain an orginator
88
+ def originator?
89
+ !!@options[:originator]
90
+ end
75
91
 
76
92
  # returns either basic or gold
77
93
  def route
78
- @options[:originator] ? :gold : :basic
94
+ originator? ? :gold : :basic
79
95
  end
80
96
 
81
97
  # returns the service url based on the security options
82
98
  def service_url
83
- @options[:secure] ? SECURE : INSECURE
99
+ URI.parse(secure? ? SECURE : INSECURE)
100
+ end
101
+
102
+ private
103
+
104
+ def session # :yield: http
105
+ http = Net::HTTP.new(service_url.host, service_url.port)
106
+ if secure?
107
+ http.use_ssl = true
108
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
109
+ end
110
+ yield(http)
84
111
  end
85
112
  end
86
113
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smspromote
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vincent Landgraf