Class: DynamicsCRM::Client
- Inherits:
-
Object
- Object
- DynamicsCRM::Client
- Extended by:
- Forwardable
- Includes:
- XML::MessageBuilder
- Defined in:
- lib/dynamics_crm/client.rb
Constant Summary collapse
- OCP_LOGIN_URL =
'https://login.microsoftonline.com/RST2.srf'
- REGION =
{ "crm9.dynamics.com" => "urn:crmgcc:dynamics.com", "crm8.dynamics.com" => "urn:crmind:dynamics.com", "crm7.dynamics.com" => "urn:crmjpn:dynamics.com", "crm6.dynamics.com" => "urn:crmoce:dynamics.com", "crm5.dynamics.com" => "urn:crmapac:dynamics.com", "crm4.dynamics.com" => "urn:crmemea:dynamics.com", "crm3.dynamics.com" => "urn:crmcan:dynamics.com", "crm2.dynamics.com" => "urn:crmsam:dynamics.com", "crm.dynamics.com" => "urn:crmna:dynamics.com", }
Instance Attribute Summary collapse
-
#caller_id ⇒ Object
Returns the value of attribute caller_id.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#organization_endpoint ⇒ Object
readonly
Returns the value of attribute organization_endpoint.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #associate(entity_name, guid, relationship, related_entities) ⇒ Object
-
#authenticate(username, password) ⇒ Object
Public: Authenticate User.
-
#create(entity_name, attributes) ⇒ Object
These are all the operations defined by the Dynamics WSDL.
- #create_attachment(entity_name, entity_id, options = {}) ⇒ Object
- #delete(entity_name, guid) ⇒ Object
- #disassociate(entity_name, guid, relationship, related_entities) ⇒ Object
- #execute(action, parameters = {}, response_class = nil) ⇒ Object
- #fetch(fetchxml) ⇒ Object
-
#initialize(config = {organization_name: nil, hostname: nil, caller_id: nil, login_url: nil, region: nil}) ⇒ Client
constructor
Initializes Client instance.
- #load_entity(logical_name, id) ⇒ Object
- #retrieve(entity_name, guid, columns = []) ⇒ Object
-
#retrieve_all_entities ⇒ Object
Metadata Calls EntityFilters Enum: Default, Entity, Attributes, Privileges, Relationships, All.
- #retrieve_attachments(entity_id, columns = ["filename", "documentbody", "mimetype"]) ⇒ Object
- #retrieve_attribute(entity_logical_name, logical_name) ⇒ Object
-
#retrieve_entity(logical_name, entity_filter = "Attributes") ⇒ Object
EntityFilters Enum: Default, Entity, Attributes, Privileges, Relationships, All.
- #retrieve_metadata_changes(entity_query) ⇒ Object
-
#retrieve_multiple(entity_name, criteria = [], columns = [], operator = nil) ⇒ Object
Suports parameter list or QueryExpression object.
- #rollup(target_entity, query, rollup_type = "Related") ⇒ Object
-
#update(entity_name, guid, attributes) ⇒ Object
Update entity attributes.
- #who_am_i ⇒ Object
Methods included from XML::MessageBuilder
#associate_request, #build_envelope, #build_header, #build_ocp_header, #build_ocp_request, #build_on_premise_header, #build_on_premise_request, #create_request, #delete_request, #disassociate_request, #execute_request, #get_current_time, #get_current_time_plus_hour, #get_tomorrow_time, #modify_association, #retrieve_multiple_request, #retrieve_request, #update_request, #uuid
Constructor Details
#initialize(config = {organization_name: nil, hostname: nil, caller_id: nil, login_url: nil, region: nil}) ⇒ Client
Initializes Client instance. Requires: organization_name Optional: hostname
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dynamics_crm/client.rb', line 40 def initialize(config={organization_name: nil, hostname: nil, caller_id: nil, login_url: nil, region: nil}) raise RuntimeError.new("organization_name or hostname is required") if config[:organization_name].nil? && config[:hostname].nil? @organization_name = config[:organization_name] @hostname = config[:hostname] || "#{@organization_name}.api.crm.dynamics.com" @organization_endpoint = "https://#{@hostname}/XRMServices/2011/Organization.svc" REGION.default = @organization_endpoint @caller_id = config[:caller_id] @timeout = config[:timeout] || 120 # The Login URL and Region are located in the client's Organization WSDL. # https://tinderboxdev.api.crm.dynamics.com/XRMServices/2011/Organization.svc?wsdl=wsdl0 # # Login URL: Policy -> Issuer -> Address # Region: SecureTokenService -> AppliesTo @login_url = config[:login_url] @region = config[:region] || determine_region end |
Instance Attribute Details
#caller_id ⇒ Object
Returns the value of attribute caller_id.
20 21 22 |
# File 'lib/dynamics_crm/client.rb', line 20 def caller_id @caller_id end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
21 22 23 |
# File 'lib/dynamics_crm/client.rb', line 21 def hostname @hostname end |
#logger ⇒ Object
Returns the value of attribute logger.
20 21 22 |
# File 'lib/dynamics_crm/client.rb', line 20 def logger @logger end |
#organization_endpoint ⇒ Object (readonly)
Returns the value of attribute organization_endpoint.
21 22 23 |
# File 'lib/dynamics_crm/client.rb', line 21 def organization_endpoint @organization_endpoint end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
21 22 23 |
# File 'lib/dynamics_crm/client.rb', line 21 def region @region end |
#timeout ⇒ Object
Returns the value of attribute timeout.
20 21 22 |
# File 'lib/dynamics_crm/client.rb', line 20 def timeout @timeout end |
Instance Method Details
#associate(entity_name, guid, relationship, related_entities) ⇒ Object
190 191 192 193 194 |
# File 'lib/dynamics_crm/client.rb', line 190 def associate(entity_name, guid, relationship, ) request = associate_request(entity_name, guid, relationship, ) xml_response = post(organization_endpoint, request) Response::AssociateResponse.new(xml_response) end |
#authenticate(username, password) ⇒ Object
Public: Authenticate User
Examples
client.authenticate('[email protected]', 'password')
# => true || raised Fault
Returns true on success or raises Fault
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/dynamics_crm/client.rb', line 67 def authenticate(username, password) @username = username @password = password auth_request = if on_premise? build_on_premise_request(username, password, region, login_url) else build_ocp_request(username, password, region, login_url) end soap_response = post(login_url, auth_request) document = REXML::Document.new(soap_response) # Check for Fault fault_xml = document.get_elements("//[local-name() = 'Fault']") raise XML::Fault.new(fault_xml) if fault_xml.any? if on_premise? @security_token0 = document.get_elements("//e:CipherValue").first.text.to_s @security_token1 = document.get_elements("//xenc:CipherValue").last.text.to_s @key_identifier = document.get_elements("//o:KeyIdentifier").first.text @cert_issuer_name = document.get_elements("//X509IssuerName").first.text @cert_serial_number = document.get_elements("//X509SerialNumber").first.text @server_secret = document.get_elements("//trust:BinarySecret").first.text @header_current_time = get_current_time @header_expires_time = get_current_time_plus_hour @timestamp = '<u:Timestamp xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" u:Id="_0"><u:Created>' + @header_current_time + '</u:Created><u:Expires>' + @header_expires_time + '</u:Expires></u:Timestamp>' @digest_value = Digest::SHA1.base64digest @timestamp @signature = '<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></CanonicalizationMethod><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1"></SignatureMethod><Reference URI="#_0"><Transforms><Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></Transform></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></DigestMethod><DigestValue>' + @digest_value + '</DigestValue></Reference></SignedInfo>' @signature_value = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), Base64.decode64(@server_secret), @signature)).chomp else cipher_values = document.get_elements("//CipherValue") if cipher_values && cipher_values.length > 0 @security_token0 = cipher_values[0].text @security_token1 = cipher_values[1].text # Use local-name() to ignore namespace. @key_identifier = document.get_elements("//[local-name() = 'KeyIdentifier']").first.text else raise RuntimeError.new(soap_response) end end true end |
#create(entity_name, attributes) ⇒ Object
These are all the operations defined by the Dynamics WSDL. Tag names are case-sensitive.
116 117 118 119 120 121 122 |
# File 'lib/dynamics_crm/client.rb', line 116 def create(entity_name, attributes) entity = XML::Entity.new(entity_name) entity.attributes = XML::Attributes.new(attributes) xml_response = post(organization_endpoint, create_request(entity)) Response::CreateResult.new(xml_response) end |
#create_attachment(entity_name, entity_id, options = {}) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/dynamics_crm/client.rb', line 202 def (entity_name, entity_id, ={}) raise "options must contain a document entry" unless [:document] file_name = [:filename] document = [:document] subject = [:subject] text = [:text] || "" if document.is_a?(String) && File.exists?(document) file = File.new(document) elsif document.is_a?(String) && document.start_with?("http") require 'open-uri' file = open(document) else file = document end if file.respond_to?(:base_uri) file_name ||= File.basename(file.base_uri.path) mime_type = MimeMagic.by_path(file.base_uri.path) elsif file.respond_to?(:path) file_name ||= File.basename(file.path) mime_type = MimeMagic.by_path(file.path) else raise "file must be a valid File object, file path or URL" end documentbody = file.read attributes = { objectid: {id: entity_id, logical_name: entity_name}, subject: subject || file_name, notetext: text || "", filename: file_name, isdocument: true, documentbody: ::Base64.encode64(documentbody), filesize: documentbody.length, mimetype: mime_type } self.create("annotation", attributes) end |
#delete(entity_name, guid) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/dynamics_crm/client.rb', line 175 def delete(entity_name, guid) request = delete_request(entity_name, guid) xml_response = post(organization_endpoint, request) Response::DeleteResponse.new(xml_response) end |
#disassociate(entity_name, guid, relationship, related_entities) ⇒ Object
196 197 198 199 200 |
# File 'lib/dynamics_crm/client.rb', line 196 def disassociate(entity_name, guid, relationship, ) request = disassociate_request(entity_name, guid, relationship, ) xml_response = post(organization_endpoint, request) Response::DisassociateResponse.new(xml_response) end |
#execute(action, parameters = {}, response_class = nil) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/dynamics_crm/client.rb', line 182 def execute(action, parameters={}, response_class=nil) request = execute_request(action, parameters) xml_response = post(organization_endpoint, request) response_class ||= Response::ExecuteResult response_class.new(xml_response) end |
#fetch(fetchxml) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/dynamics_crm/client.rb', line 156 def fetch(fetchxml) response = self.execute("RetrieveMultiple", { Query: XML::FetchExpression.new(fetchxml) }) response['EntityCollection'] end |
#load_entity(logical_name, id) ⇒ Object
290 291 292 293 294 295 296 297 |
# File 'lib/dynamics_crm/client.rb', line 290 def load_entity(logical_name, id) case logical_name when "opportunity" Model::Opportunity.new(id, self) else Model::Entity.new(logical_name, id, self) end end |
#retrieve(entity_name, guid, columns = []) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/dynamics_crm/client.rb', line 125 def retrieve(entity_name, guid, columns=[]) column_set = XML::ColumnSet.new(columns) request = retrieve_request(entity_name, guid, column_set) xml_response = post(organization_endpoint, request) Response::RetrieveResult.new(xml_response) end |
#retrieve_all_entities ⇒ Object
Metadata Calls EntityFilters Enum: Default, Entity, Attributes, Privileges, Relationships, All
250 251 252 253 254 255 256 |
# File 'lib/dynamics_crm/client.rb', line 250 def retrieve_all_entities response = self.execute("RetrieveAllEntities", { EntityFilters: "Entity", RetrieveAsIfPublished: true }, Metadata::RetrieveAllEntitiesResponse) end |
#retrieve_attachments(entity_id, columns = ["filename", "documentbody", "mimetype"]) ⇒ Object
244 245 246 |
# File 'lib/dynamics_crm/client.rb', line 244 def (entity_id, columns=["filename", "documentbody", "mimetype"]) self.retrieve_multiple("annotation", [["objectid", "Equal", entity_id], ["isdocument", "Equal", true]], columns) end |
#retrieve_attribute(entity_logical_name, logical_name) ⇒ Object
269 270 271 272 273 274 275 276 277 |
# File 'lib/dynamics_crm/client.rb', line 269 def retrieve_attribute(entity_logical_name, logical_name) self.execute("RetrieveAttribute", { EntityLogicalName: entity_logical_name, LogicalName: logical_name, MetadataId: "00000000-0000-0000-0000-000000000000", RetrieveAsIfPublished: true }, Metadata::RetrieveAttributeResponse) end |
#retrieve_entity(logical_name, entity_filter = "Attributes") ⇒ Object
EntityFilters Enum: Default, Entity, Attributes, Privileges, Relationships, All
259 260 261 262 263 264 265 266 267 |
# File 'lib/dynamics_crm/client.rb', line 259 def retrieve_entity(logical_name, entity_filter="Attributes") self.execute("RetrieveEntity", { LogicalName: logical_name, MetadataId: "00000000-0000-0000-0000-000000000000", EntityFilters: entity_filter, RetrieveAsIfPublished: true }, Metadata::RetrieveEntityResponse) end |
#retrieve_metadata_changes(entity_query) ⇒ Object
279 280 281 282 283 284 |
# File 'lib/dynamics_crm/client.rb', line 279 def (entity_query) self.execute("RetrieveMetadataChanges", { Query: entity_query }, Metadata::RetrieveMetadataChangesResponse) end |
#retrieve_multiple(entity_name, criteria = [], columns = [], operator = nil) ⇒ Object
Suports parameter list or QueryExpression object.
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/dynamics_crm/client.rb', line 142 def retrieve_multiple(entity_name, criteria = [], columns = [], operator = nil) if entity_name.is_a?(XML::QueryExpression) query = entity_name else query = XML::QueryExpression.new(entity_name) query.columns = columns query.criteria = XML::Criteria.new(criteria, filter_operator: operator) end request = retrieve_multiple_request(query) xml_response = post(organization_endpoint, request) Response::RetrieveMultipleResult.new(xml_response) end |
#rollup(target_entity, query, rollup_type = "Related") ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/dynamics_crm/client.rb', line 133 def rollup(target_entity, query, rollup_type="Related") self.execute("Rollup", { Target: target_entity, Query: query, RollupType: rollup_type }) end |
#update(entity_name, guid, attributes) ⇒ Object
Update entity attributes
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/dynamics_crm/client.rb', line 164 def update(entity_name, guid, attributes) entity = XML::Entity.new(entity_name) entity.id = guid entity.attributes = XML::Attributes.new(attributes) request = update_request(entity) xml_response = post(organization_endpoint, request) Response::UpdateResponse.new(xml_response) end |
#who_am_i ⇒ Object
286 287 288 |
# File 'lib/dynamics_crm/client.rb', line 286 def who_am_i self.execute('WhoAmI') end |