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'
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.
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_multiple(entity_name, criteria = [], columns = []) ⇒ 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dynamics_crm/client.rb', line 29 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" @caller_id = config[:caller_id] # 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.
21 22 23 |
# File 'lib/dynamics_crm/client.rb', line 21 def caller_id @caller_id end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
22 23 24 |
# File 'lib/dynamics_crm/client.rb', line 22 def hostname @hostname end |
#logger ⇒ Object
Returns the value of attribute logger.
21 22 23 |
# File 'lib/dynamics_crm/client.rb', line 21 def logger @logger end |
#organization_endpoint ⇒ Object (readonly)
Returns the value of attribute organization_endpoint.
22 23 24 |
# File 'lib/dynamics_crm/client.rb', line 22 def organization_endpoint @organization_endpoint end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
22 23 24 |
# File 'lib/dynamics_crm/client.rb', line 22 def region @region end |
Instance Method Details
#associate(entity_name, guid, relationship, related_entities) ⇒ Object
177 178 179 180 181 |
# File 'lib/dynamics_crm/client.rb', line 177 def associate(entity_name, guid, relationship, ) request = associate_request(entity_name, guid, relationship, ) xml_response = post(organization_endpoint, request) return 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
54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
# File 'lib/dynamics_crm/client.rb', line 54 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::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.
105 106 107 108 109 110 111 112 |
# File 'lib/dynamics_crm/client.rb', line 105 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)) return Response::CreateResult.new(xml_response) end |
#create_attachment(entity_name, entity_id, options = {}) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 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 |
# File 'lib/dynamics_crm/client.rb', line 189 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
162 163 164 165 166 167 |
# File 'lib/dynamics_crm/client.rb', line 162 def delete(entity_name, guid) request = delete_request(entity_name, guid) xml_response = post(organization_endpoint, request) return Response::DeleteResponse.new(xml_response) end |
#disassociate(entity_name, guid, relationship, related_entities) ⇒ Object
183 184 185 186 187 |
# File 'lib/dynamics_crm/client.rb', line 183 def disassociate(entity_name, guid, relationship, ) request = disassociate_request(entity_name, guid, relationship, ) xml_response = post(organization_endpoint, request) return Response::DisassociateResponse.new(xml_response) end |
#execute(action, parameters = {}, response_class = nil) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/dynamics_crm/client.rb', line 169 def execute(action, parameters={}, response_class=nil) request = execute_request(action, parameters) xml_response = post(organization_endpoint, request) response_class ||= Response::ExecuteResult return response_class.new(xml_response) end |
#fetch(fetchxml) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/dynamics_crm/client.rb', line 143 def fetch(fetchxml) response = self.execute("RetrieveMultiple", { Query: XML::FetchExpression.new(fetchxml) }) response['EntityCollection'] end |
#load_entity(logical_name, id) ⇒ Object
270 271 272 273 274 275 276 277 |
# File 'lib/dynamics_crm/client.rb', line 270 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
115 116 117 118 119 120 121 122 |
# File 'lib/dynamics_crm/client.rb', line 115 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) return Response::RetrieveResult.new(xml_response) end |
#retrieve_all_entities ⇒ Object
Metadata Calls EntityFilters Enum: Default, Entity, Attributes, Privileges, Relationships, All
237 238 239 240 241 242 243 |
# File 'lib/dynamics_crm/client.rb', line 237 def retrieve_all_entities response = self.execute("RetrieveAllEntities", { EntityFilters: "Entity", RetrieveAsIfPublished: true }, Metadata::RetrieveAllEntitiesResponse) end |
#retrieve_attachments(entity_id, columns = ["filename", "documentbody", "mimetype"]) ⇒ Object
231 232 233 |
# File 'lib/dynamics_crm/client.rb', line 231 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
256 257 258 259 260 261 262 263 264 |
# File 'lib/dynamics_crm/client.rb', line 256 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
246 247 248 249 250 251 252 253 254 |
# File 'lib/dynamics_crm/client.rb', line 246 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_multiple(entity_name, criteria = [], columns = []) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dynamics_crm/client.rb', line 132 def retrieve_multiple(entity_name, criteria=[], columns=[]) query = XML::Query.new(entity_name) query.columns = columns query.criteria = XML::Criteria.new(criteria) request = retrieve_multiple_request(query) xml_response = post(organization_endpoint, request) return Response::RetrieveMultipleResult.new(xml_response) end |
#rollup(target_entity, query, rollup_type = "Related") ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/dynamics_crm/client.rb', line 124 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
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/dynamics_crm/client.rb', line 151 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) return Response::UpdateResponse.new(xml_response) end |
#who_am_i ⇒ Object
266 267 268 |
# File 'lib/dynamics_crm/client.rb', line 266 def who_am_i self.execute('WhoAmI') end |