Sha256: 0149608f9721146f8061f1c92f345fc437c0a90813ca34867fad4de4ba950de6

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

module RdInsightly
  module ApiInsightly
    AUTHORIZED = true
    UNAUTHORIZED = false

    def self.authentication
      RestClient.get("#{BASE_URL}contacts", Authorization: authorization_string, accept: :json)
      AUTHORIZED
    rescue
      UNAUTHORIZED
    end

    def self.leads
      JSON.parse(RestClient.get("#{BASE_URL}leads", Authorization: authorization_string, accept: :json))
    rescue
      nil
    end

    def self.create_lead(lead)
      lead_hash = SerializerInsightly.lead_to_hash(lead)
      RestClient.post("#{BASE_URL}leads", lead_hash.to_json, Authorization: authorization_string, accept: :json, content_type: :json)
      lead
    end

    def self.delete_lead(lead_id)
      RestClient.delete("#{BASE_URL}leads/#{lead_id}", Authorization: authorization_string, accept: :json, content_type: :json)
    rescue
      raise LeadException, 'Lead não pode ser excluido!'
    end

    def self.find_lead(lead_id)
      lead_json = JSON.parse(RestClient.get("#{BASE_URL}leads/#{lead_id}", Authorization: authorization_string, accept: :json))
      SerializerInsightly.lead(lead_json)
    rescue
      raise LeadException, 'Lead não foi encontrado!'
    end

    def self.update_lead(lead)
      lead_hash = SerializerInsightly.lead_to_hash(lead)
      response = RestClient.put("#{BASE_URL}leads", lead_hash.to_json, Authorization: authorization_string, accept: :json, content_type: :json)
      SerializerInsightly.lead(JSON.parse(response))
    rescue
      raise LeadException, 'Lead não foi atualizado!'
    end

    def self.authorization_string
      "Basic #{Base64.encode64(RdInsightly.api_token)}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rd_insightly-0.2.1 lib/rd_insightly/insightly/api_insightly.rb
rd_insightly-0.2.0 lib/rd_insightly/insightly/api_insightly.rb
rd_insightly-0.1.9 lib/rd_insightly/insightly/api_insightly.rb