class Userfox::Client

Public Class Methods

new(client_id, client_secret) click to toggle source
# File lib/userfox/client.rb, line 8
def initialize(client_id, client_secret)
  @auth = { username: client_id, password: client_secret }
end

Public Instance Methods

change(from, to, options = {}) click to toggle source
# File lib/userfox/client.rb, line 29
def change(from, to, options = {})
  options[:body] = { from: from, to: to }

  post("/change.json", options)
end
post(path, options = {}) click to toggle source
# File lib/userfox/client.rb, line 12
def post(path, options = {})
  options.merge!({ basic_auth: @auth })
  handle_response { self.class.post(path, options) }
end
send(email, name, delta = {}, options = {}) click to toggle source
# File lib/userfox/client.rb, line 23
def send(email, name, delta = {}, options = {})
  options[:body] = { addr: email, name: name, delta: delta.to_json }

  post("/send.json", options)
end
track(email, url, delta = {}, options = {}) click to toggle source
# File lib/userfox/client.rb, line 17
def track(email, url, delta = {}, options = {})
  options[:body] = { addr: email, url: url, delta: delta.to_json }

  post("/track.json", options)
end
unsubscribes(email, options = {}) click to toggle source
# File lib/userfox/client.rb, line 35
def unsubscribes(email, options = {})
  options[:body] = { addr: email }

  post("/unsubscribes.json", options)
end

Protected Instance Methods

handle_response(&block) click to toggle source
# File lib/userfox/client.rb, line 43
def handle_response(&block)
  response = block.call

  case response.code
    when 401 then raise(UnauthorizedError, "You are unauthorized, check your credentials.")
    # At the moment, 404 errors from the API means lots of things...
    when 404 then raise(UnauthorizedError, "You are unauthorized, check your credentials.")  
  end

  response
end