Sha256: 8f8ac40848aca0a40efbc81dc96b23aba0cf16bd5caf013194485a43777f3521

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Authy
  #
  #  Authy.api_key = 'foo'
  #  Authy.api_uri = 'http://test-authy-api.heroku.com/'
  #
  class API
    include Typhoeus

    def self.register_user(attributes)
      user_data = {
        :user => attributes,
        :api_key => Authy.api_key
      }

      response = Typhoeus::Request.post("#{Authy.api_uri}/protected/json/users/new", :params => user_data)

      Authy::User.new(response)
    end

    # options:
    # :id user id
    # :token authy token entered by the user
    # :force (true|false) force to check even if the cellphone is not confirmed
    #
    def self.verify(params)
      token = params.delete(:token) || params.delete('token')
      user_id = params.delete(:id) || params.delete('id')

      response = Typhoeus::Request.get("#{Authy.api_uri}/protected/json/verify/#{escape_for_url(token)}/#{escape_for_url(user_id)}", :params => params.merge({:api_key => Authy.api_key}))

      Authy::Response.new(response)
    end

    # options:
    # :id user id
    def self.request_sms(params)
      user_id = params.delete(:id) || params.delete('id')

      response = Typhoeus::Request.get("#{Authy.api_uri}/protected/json/sms/#{escape_for_url(user_id)}", :params => params.merge({:api_key => Authy.api_key}))

      Authy::Response.new(response)
    end

    private
    def self.escape_for_url(field)
      URI.escape(field.to_s.strip, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
authy-0.0.6 lib/authy/api.rb
authy-0.0.5 lib/authy/api.rb