0

I'm trying to integrate an API into a class but can't work out how I put the modules in place.

class PlivoNumber < ActiveRecord::Base
  require 'plivo'
  include Plivo 

def initialize_plivo

 @p = RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
end

def delete_number
  self.initialize_plivo
  params = {
    'number' => self.number
  }
  response = @p.unrent_number(params)

end

I've tried both include and Extend

if I use in initialize

self.RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])

NoMethodError: undefined method `RestAPI' for #<PlivoNumber:0x007f8eca9523f0>

if I use

RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])

NameError: uninitialized constant PlivoNumber::RestAPI

Basically I want to be able to run @plivo_number.delete_number and have the app hit the api and perform the action. I appreciate that the initialize step not really doing anything with the class, but I can't do the next step without it.

Hope that makes some kind of sense, I get the impression that what I'm doing is probably a bit confused....

Carpela
  • 2,155
  • 1
  • 24
  • 55

2 Answers2

3

You should be able to access RestAPI class after including Plivo module. Make sure you have installed plivo gem correctly. Here is more rubyish version of your code:

class PlivoNumber < ActiveRecord::Base
  include Plivo 

  def delete_number
    api.unrent_number('number' => number)
  end

  private

  def api
    @api ||= RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
  end
end

Also you don't need to include Plivo module into PlivoNumber class, you could just use Plivo::RestAPI instead.

dostu
  • 1,498
  • 13
  • 18
2

Just to explain Donatas' answer a bit further:

If you check plivo's source code (https://github.com/plivo/plivo-ruby/blob/master/lib/plivo.rb), you'll see a structure like:

module Plivo

   (...)
   class RestAPI
      (...)
   end
   (...)
end

In this scenario, if you include Plivo, you'll get all classes defined inside it (including RestAPI) as if defined locally. So, you can access them directly, as in Donatas' code snippet:

def api
  @api ||= RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
end

You could also, as he mentioned, just require Plivo in any Ruby class, and you'll have access to the RestAPI through its namespaced path. In a controller, for example:

require 'plivo'

class OperationsController < ApplicationController

  def plivo
    plivo = Plivo::RestAPI.new(ENV['PLIVO_AUTH_ID'], ENV['PLIVO_AUTH_TOKEN'])
    ...
  end
end
  • 1
    Thanks for clarifying. You can use gems defined in Gemfile without explicitly requiring them, Bundler does this automatically. – dostu Feb 20 '15 at 11:56
  • I'm trying this, but @p (or @api in your example) ends up returning nil, even though it seems to have authenticated correctly. – Carpela Feb 20 '15 at 12:36
  • So when I run @p.get_numbers() (a plivo list method) I end up with an undefined method error – Carpela Feb 20 '15 at 12:38
  • Ok. So long as I keep everything inside one method, then we're good. i.e. I initialize and carry out the api call inside the same method. Not sure what I was trying but I sort of expected @p to persist in the console so I could reuse it. I guess that's probably me being dumb. – Carpela Feb 20 '15 at 12:42