Module: Sasswillio
- Defined in:
- lib/sasswillio.rb
Overview
numbers_with_pricing = Sasswillio.list_sms_enabled_phone_numbers_for_country_with_pricing(@client, ‘CA’)
Class Method Summary collapse
- .activate_subaccount(twilio_client, sid) ⇒ Object
- .close_subaccount(twilio_client, sid) ⇒ Object
- .create_subaccount(twilio_client, options) ⇒ Object
- .fetch_subaccount(twilio_client, sid) ⇒ Object
- .get_credentials ⇒ Object
- .get_phone_number_pricing_for(twilio_client, country = 'CA') ⇒ Object
- .get_sms_pricing_for(twilio_client, country = 'CA') ⇒ Object
- .get_subaccount_sms_usage(subaccount_sid, subaccount_token, options) ⇒ Object
- .get_subaccount_usage(subaccount_sid, subaccount_token, options) ⇒ Object
- .init ⇒ Object
- .list_sms_enabled_phone_numbers_for(twilio_client, options = {country_code: 'CA', region: 'ON', contains: '647'}) ⇒ Object
- .list_sms_enabled_phone_numbers_for_country(twilio_client, options = {country_code: 'CA'}) ⇒ Object
- .list_sms_enabled_phone_numbers_for_country_with_pricing(twilio_client, options = {country_code: 'CA'}) ⇒ Object
- .provision_sms_number_for_subaccount(twilio_client, sid, options) ⇒ Object
- .send_text_message(twilio_client, body, to_number, from_number = "+15005550006") ⇒ Object
- .suspend_subaccount(twilio_client, sid) ⇒ Object
Class Method Details
.activate_subaccount(twilio_client, sid) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/sasswillio.rb', line 216 def self.activate_subaccount(twilio_client, sid) # sending and recieving messages is possible when activated begin return Sasswillio.fetch_subaccount(twilio_client, sid).update(status: 'active') rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'activate subaccount' } end end |
.close_subaccount(twilio_client, sid) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/sasswillio.rb', line 203 def self.close_subaccount(twilio_client, sid) # when closed owned numbers are released back to twilio begin return Sasswillio.fetch_subaccount(twilio_client, sid).update(status: 'closed') rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'close subaccount' } end end |
.create_subaccount(twilio_client, options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/sasswillio.rb', line 163 def self.create_subaccount(twilio_client, ) begin # 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 return twilio_client.api.accounts.create( friendly_name: [:reference], ) rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'create subaccount' } end end |
.fetch_subaccount(twilio_client, sid) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/sasswillio.rb', line 178 def self.fetch_subaccount(twilio_client, sid) begin return twilio_client.api.accounts(sid).fetch rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'fetch subaccount' } end end |
.get_credentials ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sasswillio.rb', line 15 def self.get_credentials if ENV['TWILIO_ACCOUNT_SID'] && ENV['TWILIO_ACCOUNT_AUTH_TOKEN'] return { sid: ENV['TWILIO_ACCOUNT_SID'], token: ENV['TWILIO_ACCOUNT_AUTH_TOKEN'] } else return { sid: 'AC7fbd312e3dbfc2a44499e7ef59d5b303', token: '1df1dbcdac772278cc7091a0584d3858' } end end |
.get_phone_number_pricing_for(twilio_client, country = 'CA') ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sasswillio.rb', line 54 def self.get_phone_number_pricing_for(twilio_client, country = 'CA') begin return twilio_client.pricing.v1.phone_numbers.countries(country).fetch rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'monthly cost for local & toll free numbers' } end end |
.get_sms_pricing_for(twilio_client, country = 'CA') ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sasswillio.rb', line 42 def self.get_sms_pricing_for(twilio_client, country = 'CA') begin return Formatter.aggregate_sms_prices_obj(twilio_client.pricing.v1.messaging.countries(country).fetch) rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'sms pricing for country' } end end |
.get_subaccount_sms_usage(subaccount_sid, subaccount_token, options) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/sasswillio.rb', line 246 def self.get_subaccount_sms_usage(subaccount_sid, subaccount_token, ) begin sub_account_client = Twilio::REST::Client.new subaccount_sid, subaccount_token return sub_account_client.usage.records.list( category: 'sms', start_date: [:start_date], end_date: [:end_date] ) rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'subaccount usage query' } end end |
.get_subaccount_usage(subaccount_sid, subaccount_token, options) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/sasswillio.rb', line 229 def self.get_subaccount_usage(subaccount_sid, subaccount_token, ) begin sub_account_client = Twilio::REST::Client.new subaccount_sid, subaccount_token return sub_account_client.usage.records.list( category: 'totalprice', start_date: [:start_date], end_date: [:end_date] ) rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'subaccount usage query' } end end |
.init ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sasswillio.rb', line 29 def self.init credentials = Sasswillio.get_credentials begin return Twilio::REST::Client.new credentials[:sid], credentials[:token] rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'client initialization' } end end |
.list_sms_enabled_phone_numbers_for(twilio_client, options = {country_code: 'CA', region: 'ON', contains: '647'}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sasswillio.rb', line 84 def self.list_sms_enabled_phone_numbers_for(twilio_client, = {country_code: 'CA', region: 'ON', contains: '647'}) begin numbers = twilio_client.available_phone_numbers([:country_code]).local.list( sms_enabled: true, in_region: [:region], contains: "#{[:contains]}*******" ) return numbers.map{|n| {number: n.phone_number, friendly_name: n.friendly_name, capabilities: {**n.capabilities.transform_keys(&:to_sym)}}} rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'list sms enable phone numbers' } end end |
.list_sms_enabled_phone_numbers_for_country(twilio_client, options = {country_code: 'CA'}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sasswillio.rb', line 101 def self.list_sms_enabled_phone_numbers_for_country(twilio_client, = {country_code: 'CA'}) numbers = Hash.new begin numbers[:local] = twilio_client.available_phone_numbers([:country_code]).local.list( sms_enabled: true, ) rescue Twilio::REST::TwilioError => e numbers[:local] = [] end begin numbers[:mobile] = twilio_client.available_phone_numbers([:country_code]).mobile.list( sms_enabled: true, ) rescue Twilio::REST::TwilioError => e numbers[:mobile] = [] end transformed = Hash.new transformed[:local_numbers] = numbers[:local].map{|n| {number: n.phone_number, friendly_name: n.friendly_name, capabilities: {**n.capabilities.transform_keys(&:to_sym)}}} transformed[:mobile_numbers] = numbers[:mobile].map{|n| {number: n.phone_number, friendly_name: n.friendly_name, capabilities: {**n.capabilities.transform_keys(&:to_sym)}}} return transformed end |
.list_sms_enabled_phone_numbers_for_country_with_pricing(twilio_client, options = {country_code: 'CA'}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/sasswillio.rb', line 124 def self.list_sms_enabled_phone_numbers_for_country_with_pricing(twilio_client, = {country_code: 'CA'}) numbers_with_pricing = Hash.new begin available_nums = Sasswillio.list_sms_enabled_phone_numbers_for_country(twilio_client, ) sms_pricing = Sasswillio.get_sms_pricing_for(twilio_client, [:country_code]) phone_number_monthly_costs = Sasswillio.get_phone_number_pricing_for(twilio_client, [:country_code]) available_nums.keys.each do |k| numbers_with_pricing[k] = available_nums[k].map do |n| if k == :local_numbers { **n, sms_pricing: { inbound_cost: sms_pricing[:complete_pricing_for_country][:local_inbound], average_outbound_cost: sms_pricing[:complete_pricing_for_country][:local_outbound_average] }, 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 } elsif k == :mobile_numbers { **n, sms_pricing: { inbound_cost: sms_pricing[:complete_pricing_for_country][:mobile_inbound], average_outbound_cost: sms_pricing[:complete_pricing_for_country][:mobile_outbound_average] }, 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 } end end end return numbers_with_pricing rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'list sms enabled phone numbers for country with pricing' } end end |
.provision_sms_number_for_subaccount(twilio_client, sid, options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sasswillio.rb', line 66 def self.provision_sms_number_for_subaccount(twilio_client, sid, ) begin return Sasswillio.fetch_subaccount(twilio_client, sid).incoming_phone_numbers.create( phone_number: [:phone_number], sms_method: 'POST', sms_url: [:sms_path], status_callback: [:sms_status_path], status_callback_method: 'POST', ) rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'provision sms number for subaccount' } end end |
.send_text_message(twilio_client, body, to_number, from_number = "+15005550006") ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/sasswillio.rb', line 263 def self.(twilio_client, body, to_number, from_number = "+15005550006") begin return twilio_client..create( body: body, to: to_number, from: from_number ) rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'send message' } end end |
.suspend_subaccount(twilio_client, sid) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/sasswillio.rb', line 190 def self.suspend_subaccount(twilio_client, sid) # sending and recieving messages is disabled when suspended begin return Sasswillio.fetch_subaccount(twilio_client, sid).update(status: 'suspended') rescue Twilio::REST::TwilioError => e return { error: true, errors: [e.], context: 'suspend subaccount' } end end |