Class: MSDynamics
- Inherits:
-
Object
- Object
- MSDynamics
- Defined in:
- lib/msdynamics.rb
Overview
Public: Various methods for accessing MS Dynamics.
Instance Method Summary collapse
-
#get_entity_records(entity_name = "") ⇒ Object
Public: Gets all the records for a given MS Dynamics entity.
-
#initialize(config = { hostname: nil, access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil}) ⇒ MSDynamics
constructor
Public: Initialize a MS Dynamics client instance.
- #refresh_token ⇒ Object
Constructor Details
#initialize(config = { hostname: nil, access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil}) ⇒ MSDynamics
Public: Initialize a MS Dynamics client instance.
config - A configuration object.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/msdynamics.rb', line 12 def initialize(config={ hostname: nil, access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil}) # Validate the input. if config[:hostname].nil? && config[:access_token].nil? raise RuntimeError.new("hostname and access_token are required") end # Set up the variables @access_token = config[:access_token] @refresh_token = config[:refresh_token] @hostname = config[:hostname] @client_id = config[:client_id] @client_secret = config[:client_secret] @endpoint = "#{@hostname}/api/data/v8.0/" # Get the authenticated user's information ('WhoAmI') # This also validates the access tokens and client secrets. # If validation fails, it will raise an exception back to the calling app. response = DynamicsHTTPClient.request("#{@endpoint}WhoAmI", @access_token) @user_id = JSON.parse(response.body)['UserId'] end |
Instance Method Details
#get_entity_records(entity_name = "") ⇒ Object
Public: Gets all the records for a given MS Dynamics entity.
entity_name - ‘accounts’, ‘leads’, ‘opportunities’ or ‘contacts’.
Examples
get_entity_records('accounts')
# => [
{
"@odata.etag": "W/\"640532\"",
"name": "A. Datum",
"emailaddress1": "[email protected]",
"telephone1": "+86-23-4444-0100",
"int_twitter": null,
"int_facebook": null,
"accountid": "475b158c-541c-e511-80d3-3863bb347ba8"
}
]
Returns an object with all records for the given entity.
53 54 55 56 57 58 59 60 |
# File 'lib/msdynamics.rb', line 53 def get_entity_records(entity_name="") # Add a filter so we only get records that belong to the authenticated user. filter = "_ownerid_value eq (#{@user_id})" request_url = "#{@endpoint}#{entity_name}?$filter=#{filter}" # Return the array of records response = DynamicsHTTPClient.request(request_url, @access_token) Hashie::Mash.new(JSON.parse(response.body)).value end |
#refresh_token ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/msdynamics.rb', line 62 def refresh_token() response = DynamicsHTTPClient.refresh_token( "https://login.windows.net/common/oauth2/token", @refresh_token, @client_id, @client_secret, @hostname) token_object = Hashie::Mash.new(JSON.parse(response.body)) @access_token = token_object.access_token @refresh_token = token_object.refresh_token token_object end |