class DevMate::DevMate

Public Class Methods

CreateCustomer(email, last_name: nil, first_name: nil, note: nil) click to toggle source
# File lib/dev_mate.rb, line 36
def self.CreateCustomer(email, last_name: nil, first_name: nil, note: nil)
  data = { :email => email }
  data[last_name] = last_name if last_name
  data[first_name] = first_name if first_name
  data[note] = note if note
  body = { :data => data }

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  options = { :body => body.to_json, :headers => { "Authorization" => @@auth_header } }
  response = self.post('/customers/', options)

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 400
        raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 409
        raise ConflictError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      else
        raise UnknownError
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end
CreateLicense(customer, licenseId) click to toggle source
# File lib/dev_mate.rb, line 179
def self.CreateLicense(customer, licenseId)
  id = customer["id"]
  data = { :data => { :license_type_id => licenseId } }

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.post("/customers/#{id}/licenses", :body => data.to_json, :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 400
        raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 401
        raise UnauthorizedError, errors[0]["title"]
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end
FindCustomerById(id) click to toggle source
# File lib/dev_mate.rb, line 72
def self.FindCustomerById(id)

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.get("/customers/#{id}", :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 404
        return nil
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end
FindCustomerWithFilters(email: nil, first_name: nil, last_name: nil, license_key: nil, identifier: nil, order_id: nil, activation_id: nil, invoice: nil, offset: nil, limit: nil, with: nil) click to toggle source
# File lib/dev_mate.rb, line 98
def self.FindCustomerWithFilters(email: nil, first_name: nil, last_name: nil, license_key: nil, identifier: nil, order_id: nil, activation_id: nil, invoice: nil, offset: nil, limit: nil, with: nil)

  query = {}
  query["filter[email]"] = email if email
  query["filter[first_name]"] = first_name if first_name
  query["filter[last_name]"] = last_name if last_name
  query["filter[license_key]"] = license_key if license_key
  query["filter[identifier]"] = identifier if identifier
  query["filter[order_id]"] = order_id if order_id
  query["filter[activation_id]"] = activation_id if activation_id
  query["filter[invoice]"] = invoice if invoice
  query["offset"] = offset if offset
  query["limit"] = limit if limit
  query["with"] = with if with

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.get("/customers", :query => query, :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 404
        return []
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end
SetToken(token) click to toggle source
# File lib/dev_mate.rb, line 27
def self.SetToken(token)
  @@token = token
  @@auth_header = "Token #{token}"
end
Token() click to toggle source
# File lib/dev_mate.rb, line 32
def self.Token
  @@token
end
UpdateCustomer(customer) click to toggle source
# File lib/dev_mate.rb, line 137
def self.UpdateCustomer(customer)

  return nil unless customer['id']

  id = customer['id']

  new_customer = {}
  new_customer[:email] = customer["email"] if customer["email"]
  new_customer[:first_name] = customer["first_name"] if customer["first_name"]
  new_customer[:last_name] = customer["last_name"] if customer["last_name"]
  new_customer[:notes] = customer["notes"] if customer["notes"]

  data = { :data => new_customer }

  raise UnauthorizedError, "Please specify a token with SetToken()" unless defined? @@token

  response = self.put("/customers/#{id}", :body => data.to_json, :headers => { "Authorization" => @@auth_header })

  #TODO handle timeouts!
  response_object = JSON.parse response.body

  unless response.code == 201
    #sad path
    errors = response_object["errors"]

    case response.code
      when 400
        raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 401
        raise UnauthorizedError, errors[0]["title"]
      when 404
        raise NotFoundError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
      when 409
        raise ConflictError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
    end
  end

  # return response.body on success
  response_object = JSON.parse response.body
  return response_object['data']
end