Class: Mscrmrails::CRM

Inherits:
Object
  • Object
show all
Defined in:
lib/mscrmrails.rb

Instance Method Summary collapse

Constructor Details

#initializeCRM

Returns a new instance of CRM.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mscrmrails.rb', line 25

def initialize
  HTTPI::Adapter.use = :net_http

  @@client = Savon::Client.new do |wsdl, http|
    wsdl.document = "http://#{Mscrmrails.config.server}:#{Mscrmrails.config.port}/#{Mscrmrails.config.server_path}" + '?wsdl'
    wsdl.element_form_default = :qualified
    http.auth.ntlm(Mscrmrails.config.username, Mscrmrails.config.domain, Mscrmrails.config.password)
    http.auth.ssl.verify_mode = :none
  end

  @@client.config.env_namespace = :soap
end

Instance Method Details

#build_fetchxml(entity, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mscrmrails.rb', line 38

def build_fetchxml entity, options = {}
  limit = options[:limit] ? options[:limit] : 200
  xml = "<fetch mapping='logical' page='1' count='#{limit.to_s}'><entity name='#{entity}'>"

  options[:fields].each do |attribute|
    xml += "<attribute name='" + attribute + "'/>"
  end

  if options[:conditions]
    options[:conditions].each do |condition, op|
      xml += "<filter type='" + condition + "'>"
      op.each do |value|
        o = value[2] ? value[2] : 'eq'
        xml += "<condition attribute='" + value[0] + "' operator='" + o + "' value='" + value[1] + "' />"
      end
      xml += "</filter>"
    end
  end
  xml += "</entity></fetch>"
  return xml
end

#create(entityname, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mscrmrails.rb', line 73

def create entityname, options = {}
  result = @@client.request :create , type: entityname do |soap|
    xml = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><entity xmlns='http://schemas.microsoft.com/crm/2006/WebServices'  xsi:type='#{entityname}'>"
    options[:attributes].each do |key,value|
      xml += "<#{key}>#{value}</#{key}>"
    end
    xml += "</entity></soap:Body></soap:Envelope>"
    soap.xml = xml 
  end
  result.body[:create_result]
end

#fetch(entityname, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mscrmrails.rb', line 60

def fetch entityname, options = {}
  result = @@client.request :fetch do |soap|
    soap.header = ''

    body = Hash.new
    body["fetchXml"] = build_fetchxml entityname, options
    soap.body = body
  end
  #return result
  results = Crack::XML.parse(result.body[:fetch_result])["resultset"]["result"]
  results.class == Hash ? [results] : results
end