Code Monkey home page Code Monkey logo

phaxio-ruby's Introduction

๐Ÿ“  Phaxio

A Ruby gem for interacting with the Phaxio API.

Ruby Documentation

Installation

Add to your application's Gemfile:

gem 'phaxio', '~> 2.0.0'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install phaxio

Usage

Set up your API Key, API Secret, and, optionally, Webhook Token.

require 'phaxio'

Phaxio.api_key = '11111'
Phaxio.api_secret = '22222'
Phaxio.webhook_token = '33333'

Try sending a fax:

fax_file = File.open 'test.pdf', 'rb'
Phaxio::Fax.create to: '+15558675309', file: fax_file

You can include Phaxio::Resources to pull in the resource classes for convenience:

include Phaxio::Resources

fax_file = File.open 'test.pdf', 'rb'
Fax.create to: '+15558675309', file: fax_file

Currently Supported API Calls

Faxes

Fax.create

Create and send a fax.

fax_file = File.open 'test.pdf', 'rb'
ref = Fax.create to: '+15558675309', file: fax_file
# => Fax::Reference(id: 1234)
fax = ref.get
# => Fax(id: 1234, num_pages: 1, ...)
Fax.list

List faxes in date range.

start = 2.weeks.ago
stop = 1.week.ago
faxes = Fax.list created_after: start, created_before: stop
# => Phaxio::Resource::Collection([Fax(id: 1234, ...), ...])
faxes.length
# => 5
faxes.map(&:cost).inject(&:+)
# => 35
Fax.get

Get information about a specific fax.

fax = Fax.get 1234
# => Fax(id: 1234, ...)
Fax.cancel

Cancel a fax.

Fax.cancel 1234
# => Fax::Reference(id: 1234)
Fax.resend

Resend a fax.

Fax.resend 1234
# => Fax::Reference(id: 5678)
Fax.delete

Delete a fax. Only test faxes are allowed to be deleted.

Fax.delete 1234
# => true
Fax.delete_file

Delete fax file.

Fax.delete_file 1234
# => true
Fax.file
Fax.file 1234
# => File
Fax.test_receive

Test receiving a fax.

fax_file = File.open 'test.pdf', 'rb'
Fax.test_receive file: fax_file
# => true

Countries

Public::Country.list

Get a list of supported countries.

Public::Country.list
# => Phaxio::Resource::Collection([Public::Country(alpha2: 'US', ...), ...])

Phone Numbers

PhoneNumber.create

Provision a new phone number.

PhoneNumber.create country_code: 1, area_code: 555
# => PhoneNumber(phone_number: '+15558675309', ...)
PhoneNumber.list

List phone numbers that you own on Phaxio.

PhoneNumber.list
# => Phaxio::Resource::Collection([PhoneNumber(phone_number: '+15558675309', ...), ...])
PhoneNumber.get

Get information about a specific phone number.

PhoneNumber.get '+15558675309'
# => PhoneNumber(phone_number: '+15558675309', ...)
PhoneNumber.delete

Release a phone number.

PhoneNumber.delete '+15558675309'
# => true

Area Codes

Public::AreaCode.list

Lists available area codes for purchasing Phaxio numbers.

area_codes = Public::AreaCode.list toll_free: true
# => Phaxio::Resource::Collection([Public::AreaCode(city: 'Toll Free Service', ...), ...], page: 1)

PhaxCodes

PhaxCode.create

Creates a PhaxCode. Returns data about the PhaxCode by default, or a .png file if type: 'png' is passed.

PhaxCode.create metadata: 'test_phax_code'
# => PhaxCode(identifier: 'phax-code-identifier')
PhaxCode.create type: 'png'
# => File
PhaxCode.get

Gets a PhaxCode. Returns data about the PhaxCode by default, or a .png file if type: 'png' is passed.

PhaxCode.get 'phax-code-identifier'
# => PhaxCode(identifier: 'phax-code-identifier', metadata: 'phax-code-metadata')
PhaxCode.get 'phax-code-identifier', type: 'png'
# => File

Account

Account.get

Get information about your Phaxio account.

Account.get
# => Account(balance: 1000, faxes_today: 0, faxes_this_month: 100)

Webhook

Webhook.valid_signature?

Validate the webhook signature sent with a Phaxio webhook. Requires that Phaxio.webhook_token be set.

Webhook.valid_signature? received_signature, webhook_url, received_params, received_files
# => true

Webhook Validation Example with Sinatra

require 'sinatra/base'
require 'phaxio'

class PhaxioWebhookExample < Sinatra::Base
  Phaxio.webhook_token = 'YOUR WEBHOOK TOKEN HERE'

  post '/webhook' do
    signature = request.env['HTTP_X_PHAXIO_SIGNATURE']
    url = request.url
    file_params = params[:file]
    if Phaxio::Webhook.valid_signature? signature, url, webhook_params, file_params
      'Success'
    else
      'Invalid webhook signature'
    end
  end

  def webhook_params
    params.select do |key, _value|
      %w(success is_test direction fax metadata message event_type).include?(key)
    end
  end
end

Webhook Validation Example with Rails Controller

class WebhookController < ApplicationController
  skip_before_action :verify_authenticity_token

  def index
    signature = request.headers['X-Phaxio-Signature']
    Phaxio.webhook_token = 'YOUR WEBHOOK TOKEN HERE'
    url = request.original_url

    Rails.logger.debug "URL: " + url
    Rails.logger.debug "Signature: " + signature
    Rails.logger.debug "params: " + params.inspect
    Rails.logger.debug "webhook_params: " + webhook_params.to_h.inspect

    if Phaxio::Webhook.valid_signature? signature, url, webhook_params.to_h, file_params
      Rails.logger.debug "Success"
      render plain: 'Success'
    else
      Rails.logger.debug "Invalid webhook signature"
      render plain: 'Invalid webhook signature'
    end
  end

  def webhook_params
    params.permit(:success, :is_test, :direction, :fax, :metadata, :event_type, :message)
  end

  def file_params
    if params[:file]
      [{ :name => 'file', :tempfile => params[:file].tempfile }]
    end
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

phaxio-ruby's People

Contributors

billsimon avatar brettchalupa avatar jnankin avatar jtnegrotto avatar lucaong avatar mwmayerle avatar nerab avatar prestoncopeland avatar russ avatar seanbehan avatar sjamog avatar travistheorange avatar yujideveloper avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

phaxio-ruby's Issues

`s/callback/webhook/g`

In Phaxio, callbacks have been renamed to webhooks. This library needs to be updated to match, but for compability's sake, we should retain the original names as aliases for the new ones.

No error handling?

Is there any kind of error handling? i.e. when sending faxes with invalid phone numbers or other data, the API call would return the error message in previous versions of this gem. However, now it seems like it can only return a Reference with id or throw an unclear error, if anything goes wrong:

undefined method `start_with?' for nil:NilClass

undefined method `starts_with` when api service unavailable

This error was propagated in our background jobs during this incident

Error with Backtrace:

NoMethodError

undefined method `start_with?' for nil:NilClass
/app/vendor/bundle/ruby/2.6.0/gems/phaxio-2.0.1/lib/phaxio/client.rb:54:in `handle_response'
/app/vendor/bundle/ruby/2.6.0/gems/phaxio-2.0.1/lib/phaxio/client.rb:34:in `request'
/app/vendor/bundle/ruby/2.6.0/gems/phaxio-2.0.1/lib/phaxio/resources/fax.rb:165:in `create'

Ruby 2.x compatability?

I saw this at the top of the README:

"Note: This gem only runs on Ruby version 1.9.+"

Is this still true? What is the nature of the incompatibility, and do you know if it is going to get fixed soon? I'd really like to use this gem, but we run Ruby 2.1.1.

Verifying callback requests in Rails?

Hey team,

I'm trying to verify a Phaxio request in a Rails controller, and I can't quite figure out what I need to be feeding into the Phaxio.valid_callback_signature? method's 3rd argument. I've tried inserting the params method directly, building my own hash, and pretty much every combination I can think of.

For a regular 'received' fax, what format of data should I be providing for the validation method?

FWIW, here's roughly the code I've been using:

file = params[:filename]

if Phaxio.valid_callback_signature?(request.env['HTTP_X_PHAXIO_SIGNATURE'], request.url, params, { name: file.headers['name'], tempfile: file.tempfile } )
# Always returns false, no matter what I put into the 3rd (and 4th) argument

'make_tmpname' was removed in v2.5.0+ of Ruby

This error occurs when trying to download a file using Phaxio::Fax.file(id):

"NoMethodError (undefined method `make_tmpname' for Dir::Tmpname:Module)"

'make_tmpname' was removed from Rails v2.5.0+. Here's a link to the Rails issue: rails/rails#31458

In lib/phaxio/client.rb starting on line 51:

def handle_response response
        content_type = response.headers[:content_type]
        if content_type.start_with? 'application/json'
          body = JSON.parse(response.body).with_indifferent_access
        else
          extension = MimeTypeHelper.extension_for_mimetype content_type
          filename = File.join(
            Dir.tmpdir,
            Dir::Tmpname.make_tmpname('phaxio-', "download.#{extension}")
          )
          File.open(filename, 'wb') { |file| file.write response.body }
          body = {'success' => response.success?, 'data' => File.open(filename, 'rb')}
       end

Latest release

The latest published gem is 2.0.1 from 2018. Is this the recommended version to use still or should we be using 2.1.pre straight from the github repo?

Publish 2.0.1 to Ruby gems

๐Ÿ‘‹ We are pointing directly at Github because we want support to sending multiple files b71abcd the following commit is a version bump ea61db4 but that version has not been published to Ruby gems, do you mind doing that please?

Push 0.5.1 to Rubygems, add a tag for 2.0.1

The tags and Rubygems releases seem to be out of sync: there is a tag called 0.5.1 without a matching Rubygems release, and there is 2.0.1on Rubygems without a matching tag on Github.

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.