sasswillio 1.0.1 → 1.1.2

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: 36748b53a2c4bca267b718a64ad1cf0d05635e3a12c959ab47cd9a90bea78098
4
- data.tar.gz: 591169d80dc3b62517484e79d9758ce49f203565016e7de7eb592f9ea0c1ca87
3
+ metadata.gz: 62ecfe02406c1d26cca5b1bd1da9c3cfc865d17083482f05dc73b5525d828d00
4
+ data.tar.gz: 3fab714e4e5ac29fa5e0cf142d36b54a85e9a8b0b4d818f91b1df4d66c4fa0c1
5
5
  SHA512:
6
- metadata.gz: e3b9955c6348bfeef9c1cedae294a31ac8df35d9e4371ba78ed85b7a3593efa847e4698d476a9390aae35d79c004a4c3c16f08efe15e145e88efac1e995638a3
7
- data.tar.gz: 47a2a75c91085088b60a7fd89430d3903e93508f0aaedcf59a24b52116019c2854a60612ce2da26448c6926b2dd29d1a7c93f5fb49b88bf3a868a0cc645a23b6
6
+ metadata.gz: cad481e00b9b159757a7c09fa4ab2891b9258f184d700728699f1a79f4775bf809b368e3bd5bda8ff4dc6838f73bc5f383fc86774a38ef1103953bbe8d87c819
7
+ data.tar.gz: d826f52fadb90598f8d36fc400eee0a78c95185d78e7090255cc69ea131331749bbbe07d5f418642202814aa5725db846b41309625cd86a796313db616507fad
@@ -6,6 +6,9 @@ require 'twilio-ruby'
6
6
  # sms_pricing = Sasswillio.get_sms_pricing_for(@client)
7
7
  # phone_number_pricing = Sasswillio.get_phone_number_pricing_for(@client)
8
8
  # subaccount = Sasswillio.create_subaccount(@client, '423')
9
+ # sms_pricing = Sasswillio.get_sms_pricing_for(@client, 'FR')
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'})
9
12
 
10
13
  module Sasswillio
11
14
 
@@ -38,7 +41,7 @@ module Sasswillio
38
41
 
39
42
  def self.get_sms_pricing_for(twilio_client, country = 'CA')
40
43
  begin
41
- return twilio_client.pricing.v1.messaging.countries(country).fetch
44
+ return Formatter.aggregate_sms_prices_obj(twilio_client.pricing.v1.messaging.countries(country).fetch)
42
45
  rescue Twilio::REST::TwilioError => e
43
46
  return {
44
47
  error: true,
@@ -85,7 +88,7 @@ module Sasswillio
85
88
  in_region: options[:region],
86
89
  contains: "#{options[:contains]}*******"
87
90
  )
88
- 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)}}}
89
92
  rescue Twilio::REST::TwilioError => e
90
93
  return {
91
94
  error: true,
@@ -95,6 +98,68 @@ module Sasswillio
95
98
  end
96
99
  end
97
100
 
101
+ def self.list_sms_enabled_phone_numbers_for_country(twilio_client, options = {country_code: 'CA'})
102
+ numbers = Hash.new
103
+ begin
104
+ numbers[:local] = twilio_client.available_phone_numbers(options[:country_code]).local.list(
105
+ sms_enabled: true,
106
+ )
107
+ rescue Twilio::REST::TwilioError => e
108
+ numbers[:local] = []
109
+ end
110
+
111
+ begin
112
+ numbers[:mobile] = twilio_client.available_phone_numbers(options[:country_code]).mobile.list(
113
+ sms_enabled: true,
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
123
+
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
154
+ rescue Twilio::REST::TwilioError => e
155
+ return {
156
+ error: true,
157
+ errors: [e.message],
158
+ context: 'list sms enabled phone numbers for country with pricing'
159
+ }
160
+ end
161
+ end
162
+
98
163
  def self.create_subaccount(twilio_client, options)
99
164
  begin
100
165
  # save the sid + token and associate it with your Saas user. This will be used to buy phone numbers and send messages on their behalf
@@ -193,4 +258,6 @@ module Sasswillio
193
258
  }
194
259
  end
195
260
  end
196
- end
261
+ end
262
+
263
+ require 'sasswillio/formatter'
@@ -0,0 +1,22 @@
1
+ module Formatter
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
+ pricing_hash[transformed_key_for_outbound.to_sym] = 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} / twilio_sms_pricing_res.outbound_sms_prices.size
10
+ end
11
+ local_price_inbound = twilio_sms_pricing_res.inbound_sms_prices.find{|n| n["number_type"] == 'local'}
12
+ local_price_outbound = twilio_sms_pricing_res.outbound_sms_prices.map{|n| n["prices"].find{|i| i["number_type"] == 'local'}}.compact
13
+ return {
14
+ inbound_sms_price_for_local_number: local_price_inbound ? local_price_inbound["current_price"] : 'no local numbers available, so no pricing',
15
+ 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',
16
+ currency: twilio_sms_pricing_res.price_unit,
17
+ complete_pricing_for_country: {
18
+ **pricing_hash
19
+ }
20
+ }
21
+ end
22
+ 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.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shashike J
@@ -130,6 +130,7 @@ extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
132
  - lib/sasswillio.rb
133
+ - lib/sasswillio/formatter.rb
133
134
  homepage: https://github.com/donrestarone/sasswillio
134
135
  licenses:
135
136
  - MIT