sasswillio 1.0.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 846da3f8ba8c1607855ba02d78c8d0b17557816e8d94c54ae812a1acbeec52a4
4
- data.tar.gz: c3bfe454c65b4db675dc23c108c89bc1d5c90ea0de632799fcd8879cf8ff0d70
3
+ metadata.gz: ed8776bd99b624f0b72a7496031ec59444c2dbf69fb173b001a7fb87d9306985
4
+ data.tar.gz: c41657faff94c61927c77dbe268d48f66c54f412909ec4fd7e9c57a9b66d9893
5
5
  SHA512:
6
- metadata.gz: 87a51d8bd34529a422cb001233335d37969275803b107d550cbf4e4657730598ce5c7139779b2b77cf3708e9f69bd72543684a1e06c25fafb28086e36834ff14
7
- data.tar.gz: 17f7f0cda49481a995338a0d29641635d0ef71cfd4da6f017348b187ab3198b0421fc5c30e2b28ac30d8245d67062b46a046c8785fe486f878a3f93af49c0dc2
6
+ metadata.gz: aa2aa47605e86bf1f6b73ac9e2720e89c622876cb86e68ebfe5a764b14e6bc27e3ed32e0b7b8f6b5a14ef713ee9d4d3431708194e1339e962a7dea3913428f2c
7
+ data.tar.gz: fbe16687c5d1f91d626d39584889663f3ae788cb38ad3c0df06fcc45fbd45ddd1242203c368d86c036b0bb696790926e566333869e5fa55a363263c561b1cdfd
@@ -8,6 +8,7 @@ require 'twilio-ruby'
8
8
  # subaccount = Sasswillio.create_subaccount(@client, '423')
9
9
  # sms_pricing = Sasswillio.get_sms_pricing_for(@client, 'FR')
10
10
  # country_nums = Sasswillio.list_sms_enabled_phone_numbers_for_country(@client, {country_code: 'GB'})
11
+ # numbers_with_pricing = Sasswillio.list_sms_enabled_phone_numbers_for_country_with_pricing(@client, {country_code: 'CA'})
11
12
 
12
13
  module Sasswillio
13
14
 
@@ -87,7 +88,7 @@ module Sasswillio
87
88
  in_region: options[:region],
88
89
  contains: "#{options[:contains]}*******"
89
90
  )
90
- return numbers.map{|n| {number: n.phone_number, capabilities: {**n.capabilities.transform_keys(&:to_sym), friendly_name: n.friendly_name}}}
91
+ return numbers.map{|n| {number: n.phone_number, friendly_name: n.friendly_name, capabilities: {**n.capabilities.transform_keys(&:to_sym)}}}
91
92
  rescue Twilio::REST::TwilioError => e
92
93
  return {
93
94
  error: true,
@@ -103,22 +104,61 @@ module Sasswillio
103
104
  numbers[:local] = twilio_client.available_phone_numbers(options[:country_code]).local.list(
104
105
  sms_enabled: true,
105
106
  )
107
+ rescue Twilio::REST::TwilioError => e
108
+ numbers[:local] = []
109
+ end
110
+
111
+ begin
106
112
  numbers[:mobile] = twilio_client.available_phone_numbers(options[:country_code]).mobile.list(
107
113
  sms_enabled: true,
108
114
  )
115
+ rescue Twilio::REST::TwilioError => e
116
+ numbers[:mobile] = []
117
+ end
118
+ transformed = Hash.new
119
+ transformed[:local_numbers] = numbers[:local].map{|n| {number: n.phone_number, friendly_name: n.friendly_name, capabilities: {**n.capabilities.transform_keys(&:to_sym)}}}
120
+ transformed[:mobile_numbers] = numbers[:mobile].map{|n| {number: n.phone_number, friendly_name: n.friendly_name, capabilities: {**n.capabilities.transform_keys(&:to_sym)}}}
121
+ return transformed
122
+ end
109
123
 
110
- transformed = Hash.new
111
- transformed[:local_numbers] = numbers[:local].map{|n| {number: n.phone_number, capabilities: {**n.capabilities.transform_keys(&:to_sym), friendly_name: n.friendly_name}}}
112
- transformed[:mobile_numbers] = numbers[:mobile].map{|n| {number: n.phone_number, capabilities: {**n.capabilities.transform_keys(&:to_sym), friendly_name: n.friendly_name}}}
113
- return transformed
124
+ def self.list_sms_enabled_phone_numbers_for_country_with_pricing(twilio_client, options = {country_code: 'CA'})
125
+ numbers_with_pricing = Hash.new
126
+ begin
127
+ available_nums = Sasswillio.list_sms_enabled_phone_numbers_for_country(twilio_client, options)
128
+ sms_pricing = Sasswillio.get_sms_pricing_for(twilio_client, options[:country_code])
129
+ phone_number_monthly_costs = Sasswillio.get_phone_number_pricing_for(twilio_client, options[:country_code])
130
+ available_nums.keys.each do |k|
131
+ numbers_with_pricing[k] = available_nums[k].map do |n|
132
+ if k == :local_numbers
133
+ {
134
+ **n,
135
+ sms_pricing: {
136
+ inbound_cost: sms_pricing[:complete_pricing_for_country][:local_inbound],
137
+ average_outbound_cost: sms_pricing[:complete_pricing_for_country][:local_outbound_average]
138
+ },
139
+ monthly_cost: phone_number_monthly_costs.phone_number_prices.find{|c| c["number_type"] == 'local'} ? phone_number_monthly_costs.phone_number_prices.find{|c| c["number_type"] == 'local'}["current_price"] : nil
140
+ }
141
+ elsif k == :mobile_numbers
142
+ {
143
+ **n,
144
+ sms_pricing: {
145
+ inbound_cost: sms_pricing[:complete_pricing_for_country][:mobile_inbound],
146
+ average_outbound_cost: sms_pricing[:complete_pricing_for_country][:mobile_outbound_average]
147
+ },
148
+ monthly_cost: phone_number_monthly_costs.phone_number_prices.find{|c| c["number_type"] == 'mobile'} ? phone_number_monthly_costs.phone_number_prices.find{|c| c["number_type"] == 'mobile'}["current_price"] : nil
149
+ }
150
+ end
151
+ end
152
+ end
153
+ return numbers_with_pricing
114
154
  rescue Twilio::REST::TwilioError => e
115
155
  return {
116
156
  error: true,
117
157
  errors: [e.message],
118
- context: 'list sms enable phone numbers for country'
158
+ context: 'list sms enabled phone numbers for country with pricing'
119
159
  }
120
160
  end
121
- end
161
+ end
122
162
 
123
163
  def self.create_subaccount(twilio_client, options)
124
164
  begin
@@ -1,15 +1,28 @@
1
-
2
- # remove
3
- require 'pry'
4
-
5
1
  module Formatter
6
2
  def self.aggregate_sms_prices_obj(twilio_sms_pricing_res)
3
+ pricing_hash = Hash.new
4
+ phone_number_types = twilio_sms_pricing_res.inbound_sms_prices.map{|h| h["number_type"]}
5
+ phone_number_types.each do |type|
6
+ transformed_key_for_inbound = type + '_inbound'
7
+ transformed_key_for_outbound = type + '_outbound_average'
8
+ pricing_hash[transformed_key_for_inbound.to_sym] = twilio_sms_pricing_res.inbound_sms_prices.find{|n| n["number_type"] == type}
9
+ numerator = twilio_sms_pricing_res.outbound_sms_prices.map{|n| n["prices"].find{|i| i["number_type"] == type}}.compact.map{|n| n["current_price"].to_f}.inject{|sum, el| sum+el}
10
+ if numerator && numerator > 0
11
+ pricing_hash[transformed_key_for_outbound.to_sym] = numerator / twilio_sms_pricing_res.outbound_sms_prices.size
12
+ else
13
+ pricing_hash[transformed_key_for_outbound.to_sym] = nil
14
+ end
15
+ end
16
+
7
17
  local_price_inbound = twilio_sms_pricing_res.inbound_sms_prices.find{|n| n["number_type"] == 'local'}
8
- local_price_outbound = twilio_sms_pricing_res.outbound_sms_prices.map{|n| n["prices"].find{|i| i["number_type"] == 'local'}}
18
+ local_price_outbound = twilio_sms_pricing_res.outbound_sms_prices.map{|n| n["prices"].find{|i| i["number_type"] == 'local'}}.compact
9
19
  return {
10
20
  inbound_sms_price_for_local_number: local_price_inbound ? local_price_inbound["current_price"] : 'no local numbers available, so no pricing',
11
21
  average_outbound_sms_price_for_local_number: local_price_outbound ? local_price_outbound.map{|n| n["current_price"].to_f}.inject{|sum, el| sum+el} / twilio_sms_pricing_res.outbound_sms_prices.size : 'no local numbers available, so no pricing',
12
22
  currency: twilio_sms_pricing_res.price_unit,
23
+ complete_pricing_for_country: {
24
+ **pricing_hash
25
+ }
13
26
  }
14
27
  end
15
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sasswillio
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shashike J