class NeverBounce::API
Attributes
accessToken[RW]
apiSecret[RW]
apikey[RW]
host[RW]
options[RW]
path[RW]
Public Class Methods
new(apiKey, apiSecret)
click to toggle source
base_uri 'localhost:8000'
# File libs/NeverBounce.rb, line 17 def initialize(apiKey, apiSecret) @apiKey = apiKey; @apiSecret = apiSecret; @accessToken = nil; @options = {:headers => { "Content-Type" => "application/x-www-form-urlencoded", "User-Agent" => "NeverBounce-Ruby/" + VERSION} } raise AuthError, "You must provide a NeverBounce API key" unless @apiKey raise AuthError, "You must provide a NeverBounce API secret key" unless @apiSecret end
Public Instance Methods
call(endpoint, body)
click to toggle source
Call api endpoint
# File libs/NeverBounce.rb, line 46 def call(endpoint, body) begin opts = body.merge({:access_token => getAccessToken}) request(endpoint, {:body => opts}) # If access token is expired we'll retry the request rescue AccessTokenExpired @accessToken = nil opts = body.merge({:access_token => getAccessToken}) request(endpoint, {:body => opts}) end end
getAccessToken()
click to toggle source
Lets get the access token If we don't have one available already we'll request a new one
# File libs/NeverBounce.rb, line 81 def getAccessToken # Get existing access token if available if @accessToken != nil return @accessToken end # Perform request if no existing access token response = request('/v3/access_token', :body => {:grant_type => 'client_credentials', :scope => 'basic user'}, :basic_auth => {:username => @apiKey, :password => @apiSecret} ) if response['error'] != nil raise AuthError, "We were unable to complete your request. " \ "The following information was supplied: " \ "#{response['error_description']}" \ "\n\n(Request error [#{response['error']}])" end @accessToken = response['access_token'] end
request(endpoint, params)
click to toggle source
Makes the actual api request
# File libs/NeverBounce.rb, line 59 def request(endpoint, params) opts = options.merge(params) response = self.class.post(endpoint, opts) # Handle non successful requests if response['success'] === false if response['msg'] === 'Authentication failed' raise AccessTokenExpired end msg = response['msg'] || response['error_msg']; raise RequestError, "We were unable to complete your request. " \ "The following information was supplied: " \ "#{msg} " \ "\n\n(Request error)" end response end
single()
click to toggle source
Initializes the single method
# File libs/NeverBounce.rb, line 104 def single Single.new(self) end