Class: Totango::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/totango-api/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/totango-api/client.rb', line 19

def initialize(hash={})
  @sid = hash[:sid] || hash["sid"] || nil
  load_sid_from_file if @sid == nil
  raise Totango::Error::NoSID, "No SID provided" unless @sid
  @user_attributes = hash[:user] || hash["user"] || {}
  @account_attributes = hash[:account] || hash["account"] || {}
  @attributes = {}
  @attributes["sdr_s"] = @sid
  @attributes["sdr_a"] = hash[:action] || hash["action"]
  @attributes["sdr_m"] = hash[:module] || hash["module"]
  @account = Totango::Account.new(@account_attributes)
  @user = Totango::User.new(@user_attributes)

  @api = Faraday.new(url: "https://sdr.totango.com/")  do |faraday|
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



18
19
20
# File 'lib/totango-api/client.rb', line 18

def 
  @account
end

#sidObject (readonly)

Returns the value of attribute sid.



18
19
20
# File 'lib/totango-api/client.rb', line 18

def sid
  @sid
end

#userObject (readonly)

Returns the value of attribute user.



18
19
20
# File 'lib/totango-api/client.rb', line 18

def user
  @user
end

Class Method Details

.create(args) ⇒ Object



62
63
64
65
66
# File 'lib/totango-api/client.rb', line 62

def self.create(args)
  event = Client.new(args)
  event.save
  return event
end

Instance Method Details

#attributesObject



38
39
40
# File 'lib/totango-api/client.rb', line 38

def attributes
  @attributes.merge(@user.attributes).merge(@account.attributes)
end

#saveObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/totango-api/client.rb', line 42

def save
  if valid? then
    begin
      Timeout.timeout(Totango.timeout) { @api.get '/pixel.gif/', self.attributes }
    rescue Timeout::Error
      Totango.error_proc.call("Timed out getting data from Totango.") unless Totango.error_proc == nil
    rescue Exception => e
      Totango.error_proc.call("Totango API error #{e.message}") unless Totango.error_proc == nil
    end
  else
    raise Totango::Error::InvalidEvent
  end
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/totango-api/client.rb', line 56

def valid?
  return false if @account.id == nil
  return false if @user.attributes.keys.grep(/sdr_u\./).count > 0 and @user.attributes["sdr_u"] == nil
  return true
end