Git Product home page Git Product logo

anonymous-communications-rails's People

Contributors

dependabot-preview[bot] avatar hortega avatar jarodreyes avatar jefflinwood avatar joliveros avatar kwhinnery avatar mmena1 avatar mosampaio avatar philnash avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

anonymous-communications-rails's Issues

Misc Style Suggestions

Enumberables

Use numbers.first over numbers[0]
Use if @numbers.empty? over if not @numbers.any?

Ternary

@app_number = from_number.nil? ? ENV['TWILIO_NUMBER'] : from_number

over inline if:

@app_number = if from_number.nil? then ENV['BARISTA_NUMBER'] else from_number end

Saving

Use

player.update(name: 'Tom Brady') 

over

player.name = 'Tom Brady'
player.save

simplify @app_number assignment in send_message_via_sms

send_message_via_sms

  • can use the assignment in the params to set the default value for from_number
  • extract twilio client creation to own method
  • no need to assign sms message to a variable
def send_message_via_sms(message, from_number = nil)
  @app_number = if from_number.nil? then ENV['BARISTA_NUMBER'] else from_number end
  @client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
  sms_message = @client.account.messages.create(
    from: @app_number,
    to: self.phone_number,
    body: message,
  )
end

becomes

def send_message_via_sms(message, from_number = ENV['TWILIO_NUMBER'])
  twilio_client.account.messages.create(
    from: from_number,
    to: self.phone_number,
    body: message,
  )
end

def twilio_client
  Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
end

Can send response message with <Message>

In the reservations controller you can send a message to the host by sending back a <Message> TwiML where the to attribute is the intended receipient (e.g. it doesn't have to be a reply to the sender of the message).

This would eliminate an extra HTTP request, and looks like an extra database read too. Would also reduce code in the model.

refactor provision_phone_number

Here.

For the sake of single responsibility, and because it'll make the narrative easier, I'd suggest creating a lib/twilio_module.rb where you store your Twilio logic. Then you can either include it in an object, or call it verbosely using something like TwilioModule::twilio_client. (I do the first method because it's prettier, but it's not always obvious where these methods are magically coming from).

Your twilio module might look like:

module TwilioModule

  def twilio_client
    Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
  end

  def avilable_numbers_in_area_code(area_code)
    twilio_client.account.available_phone_numbers.get('US').local.list(:area_code => area_code)
  end

  def available_numbers_anywhere
    twilio_client.account.available_phone_numbers.get('US').local.list
  end

  def available_number(area_code)
    numbers = available_numbers_in_area_code(area_code)
    numbers.present? ? numbers.first : available_numbers_anywhere.first
  end

  def buy_number(number)
    client.account.incoming_phone_numbers.create(:phone_number => number)
  end

  def buy_an_available_number(area_code)
    number = available_number(area_code)
    buy_number(number) #returns a twilio_number object
  end

  def update_application_sids(phone_number)
    phone_number.update(
      :voice_application_sid => ENV['ANONYMOUS_APPLICATION_SID'],
      :sms_application_sid => ENV['ANONYMOUS_APPLICATION_SID']
    )
  end

end

You will need to create a file at config/initializers/modules.rb that simply says:

require File.join(Rails.root, "lib/twilio_module.rb")

This tells Rails to load the module when it starts up, which it won't do otherwise.

With all this reusable logic in place, your provision_phone_number becomes:

include TwilioModule
def provision_phone_number
  begin
    anon_number = buy_an_available_number(self.host.area_code)
    update_application_sids(anon_number)
    self.update(phone_number: anon_number.phone_number)
  rescue Exception => e
    puts "ERROR: #{e.message}"
  end
end

I haven't tested any of this code, so I'm sure it has errors. I'm also not sure that including a module is the best practice way of doing this so you might want to run it past Phil. But I suspect that extracting the Twilio logic into macro-like methods will make it easier to explain what's going on.

What are the steps to setup the 2 sms webhooks?

Hi! I went through parts 1 and 2 of your tutorial and I am missing a point.

I installed the app, configured and deployed to heroku no problem. However, when I try to "accept/reject" the reservation, the code sends me in the 'reservations#connect_guest_to_host_sms' action.

This seems logical, since I created the Twilio app with this webhook as instructed in the readme: SMS & MMS: http://[your server].com/reservations/connect_sms
capture d ecran 2015-12-10 a 11 15 41

So... I wonder what needs to be configured so that different incoming sms go to different webhooks? The first sms needing to go to the 'reservations#accept_or_reject' action...

# exerpt from routes.rb
post "reservations/incoming", to: 'reservations#accept_or_reject', as: 'incoming'
post "reservations/connect_sms", to: 'reservations#connect_guest_to_host_sms'

Thanks for helping me out understanding how this part works! :)

use ternary instead of inline if/else

user.rb line 16

@app_number = if from_number.nil? then ENV['BARISTA_NUMBER'] else from_number end

If you want to do this inline, use the ternary operator:

@app_number = from_number.nil? ? ENV['TWILIO_NUMBER'] : from_number

But there's a simpler way for your use case which I mention in the next issue.

refactor provision_phone_number

Here. I would break this up into several pieces. May go so far as to create my own Twilio module with helper functions that I could reuse throughout the app. For instance, creating a twilio client.

It feels weird to have the code for provisioning a number inside of the reservation model.

So maybe could do something like:

WIP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.