Module: Maxcrm

Defined in:
lib/api.rb,
lib/maxcrm.rb,
lib/configuration.rb

Defined Under Namespace

Classes: MissingParameterError, Railtie

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Use eg Maxcrm.config



6
7
8
# File 'lib/maxcrm.rb', line 6

def config
  @config
end

Class Method Details

.configureObject



3
4
5
6
# File 'lib/configuration.rb', line 3

def configure
  Maxcrm.config = {}
  load_configuration_from_heroku || load_configuration_from_yml || die_without_configuration
end

.die_without_configurationObject



26
27
28
# File 'lib/configuration.rb', line 26

def die_without_configuration
  raise "MaxCRM has not been configured. Please set the Heroku environment variables or create config/maxcrm.yml. See the MaxCRM gem's README for more information."
end

.email_to_param(email) ⇒ Object



23
24
25
# File 'lib/api.rb', line 23

def email_to_param(email)
  email.gsub(".","_") 
end

.incident_resource_url(email) ⇒ Object



19
20
21
# File 'lib/api.rb', line 19

def incident_resource_url(email)
  Maxcrm.config["url"] + "users/" + email_to_param(email) + "/incidents.json"
end

.load_configuration_from_herokuObject



8
9
10
11
12
13
14
15
16
# File 'lib/configuration.rb', line 8

def load_configuration_from_heroku
  if ENV['MAXCRM_URL']
    @config={}
    keys = %w(MAXCRM_URL MAXCRM_USERNAME MAXCRM_PASSWORD)
    keys.each {|k| Maxcrm.config[k[7..999].downcase] = ENV[k]} #converts MAXCRM_URL to url
    return true
  end
  false
end

.load_configuration_from_ymlObject



18
19
20
21
22
23
24
# File 'lib/configuration.rb', line 18

def load_configuration_from_yml
  if File.exist?(filename = Rails.root.join("config", "maxcrm.yml"))
    Maxcrm.config = YAML::load_file(filename)[Rails.env]
    return true
  end
  false
end

.send_incident(params) ⇒ Object

Synchronously send an incident notification to maxcrm We need :user_email and :event_name



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/api.rb', line 5

def send_incident(params)
  raise MissingParameterError unless params[:user_email] && params[:event_name]
  
  #Make HTTP call to the Maxcrm server
  resource = RestClient::Resource.new(
    incident_resource_url(params[:user_email]),
    :user=>Maxcrm.config["username"],
    :password=>Maxcrm.config["password"]
  )
  
  response = resource.post({"incident" => {"event_name" => params[:event_name]}}) 
  return (response.code >= 200 and response.code < 300)
end