GithubHelp home page GithubHelp logo

lukaspijak / bulkgate_sms_gateway_contract Goto Github PK

View Code? Open in Web Editor NEW

This project forked from equivalent/bulkgate_sms_gateway_contract

0.0 1.0 0.0 26 KB

Ruby on Rails Contract classes for SMS Gateway via bulkgate.com

License: MIT License

Ruby 98.70% Shell 1.30%

bulkgate_sms_gateway_contract's Introduction

BulkgateSmsGatewayContract

The following gem is a Gateway Test contract & Production gateway for sending SMS texts via service API: https://www.bulkgate.com/

Gem is designed for Rails only.

Installation

Add this line to your application's Gemfile:

gem 'bulkgate_sms_gateway_contract'

And then execute:

$ bundle

Usage

step 1: Configure Rails environments (individual sections down bellow)

step 2: Then just call:

BulkgateSmsGatewayContract.contract.send_sms(country: 'sk', number: 908123123, body: 'whatever')
# or
BulkgateSmsGatewayContract.contract.send_sms(country: 'sk', number: 908123123, body: 'whatever', sender_name: "my-website.com")

Production

This is the real thing that will send SMS via Bulkgate.com API

#config/environments/production.rb

Rails.application.configure do
  # ...
  config.x.bulkgate_sms_gateway_contract = 'BulkgateSmsGatewayContract::Real'
  # ...
end

you also need to configure Rails credentials

# ...
bulkgate_sms:
  application_id: yyyy
  application_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# ...

in order to open Rails credentials you can do EDITOR=vim bundle exec rails credentials:edit

In case of API error exception BulkgateError with Bulkgate error type as message.

You can capture exception as

begin
  BulkgateSmsGatewayContract.contract.send_sms(country: 'sk', number: 'this-is-wrong', body: '666666 is your Authentication code')
rescue BulkgateSmsGatewayContract::BulkgateError => e
  puts "rescue any API specific error form Bulkgate"
end

# or

begin
  BulkgateSmsGatewayContract.contract.send_sms(country: 'sk', number: 'this-is-wrong', body: '666666 is your Authentication code')
rescue BulkgateSmsGatewayContract::BulkgateError, 'invalid_phone_number'
  puts "phone number invalid"
end

# or gem also provides custom error classes for most common errors like

rescue BulkgateSmsGatewayContract::EmptyMessageBody
rescue BulkgateSmsGatewayContract::InvalidPhoneNumber
rescue BulkgateSmsGatewayContract::OutOfCredit

see https://help.bulkgate.com/docs/en/api-error-types.html#error-types for full list off error types

Test

This is actuall Test contract

If you don't know what Test contracts are please read: https://blog.eq8.eu/article/explicit-contracts-for-rails-http-api-usecase.html

#config/environments/test.rb

Rails.application.configure do
  # ...
  config.x.bulkgate_sms_gateway_contract = 'BulkgateSmsGatewayContract::Mock'
  # ...
end

Example RSpec usage:

require 'rails_helper'
RSpec.describe 'sending of SMS' do
  def trigger
    BulkgateSmsGatewayContract.contract.send_sms(
      country: "sk",
      number: 908111222,
      body: "Your 2FA code is 1234")
   end

  it do
    expect(trigger).to eq "Bulkgate: Your 2FA code is 1234 TO: sk:908111222"
  end
end

Development

Will log SMS codes to log/sms.log

#config/environments/development.rb

Rails.application.configure do
  # ...
  config.x.bulkgate_sms_gateway_contract = 'BulkgateSmsGatewayContract::FileLog'
  # ...
end

Bonus - Production with Airbrake Debugging

If you are using Airbrake you can use for production debugging AirbrakeNotify contract

# config/environments/production.rb

# This contract is not loaded by default. You need to explicitly require it
require 'sms_gateway_contract/airbrake_notify'

# ...
  config.x.bulkgate_sms_gateway_contract = 'BulkgateSmsGatewayContract::AirbrakeNotify'
# ...

BulkgateSmsGatewayContract::AirbrakeNotify is only for debugging production. It's more a lazy tool to save cost during develompment. Don't use it once live.

Code of Conduct

Everyone interacting in the SmsGatewayContract project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

bulkgate_sms_gateway_contract's People

Contributors

equivalent avatar

Watchers

 avatar

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.