class ApiRegistro::Client

Public Class Methods

new(token, environment) click to toggle source
# File lib/api_registro/client.rb, line 5
def initialize(token, environment)
  @token = token
  @env   = environment
end

Public Instance Methods

create_contact(opts={}) click to toggle source
# File lib/api_registro/client.rb, line 10
def create_contact(opts={})
  message = {
    headers:    { "Accept"        => "application/json", 
                  "Content-Type"  => "application/json",
                  "Authorization" => "Token #{@token}"
                },
    body: opts.to_json
  }
  JSON.parse(request(url_for(:create_contact), ApiRegistro::SupportedMethods::POST, message))
end
find_contact(document_number) click to toggle source
# File lib/api_registro/client.rb, line 21
def find_contact(document_number)
  message = {
    headers:    { "Accept" => "application/json", "authorization" => @token }
  }
  JSON.parse(request(url_for(:find_contact, document_number), ApiRegistro::SupportedMethods::GET, message))
end
find_domain(domain_name) click to toggle source
# File lib/api_registro/client.rb, line 39
def find_domain(domain_name)
  message = {
    headers:    { "Accept" => "application/json", "authorization" => @token }
  }
  JSON.parse(request(url_for(:find_domain, domain_name), ApiRegistro::SupportedMethods::GET, message))
end
register_domain(domain, document_number) click to toggle source
# File lib/api_registro/client.rb, line 28
def register_domain(domain, document_number)
  message = {
    headers:    { "Accept"        => "application/json", 
                  "Content-Type"  => "application/json",
                  "Authorization" => "Token #{@token}"
                },
    body: {document: document_number}.to_json
  }
  JSON.parse(request(url_for(:register_domain, domain), ApiRegistro::SupportedMethods::POST, message))
end

Private Instance Methods

base_url() click to toggle source
# File lib/api_registro/client.rb, line 66
def base_url
  @env == ApiRegistro::Environments::PRODUCTION ? 
                "http://apiregistro.com.br/api/v1" : 
                "http://sandbox.apiregistro.com.br/api/v1"
end
request(url, http_method, message) click to toggle source
# File lib/api_registro/client.rb, line 48
def request(url, http_method, message)
  response = http_request(url, http_method, message)
  if(response.code == 200)
    response.body
  else
    nil
  end
end
uris() click to toggle source
# File lib/api_registro/client.rb, line 57
def uris
  {
      create_contact:  "contacts/",
      find_contact:    "contacts/%s/",
      register_domain: "domains/%s/buy/",
      find_domain:     "domains/?search=%s"
  }
end
url_for(resource, *params) click to toggle source
# File lib/api_registro/client.rb, line 72
def url_for(resource, *params)
  uri = uris[resource] % params
  "#{base_url}/#{uri}"
end