autotask_ruby 0.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,25 @@
1
- module AutotaskRuby
1
+ # frozen_string_literal: true
2
2
 
3
- class Configuration
4
- attr_accessor :integration_code
5
- attr_accessor :version
6
- attr_accessor :namespace
3
+ module AutotaskRuby
4
+ class Configuration
5
+ attr_accessor :integration_code
6
+ attr_accessor :version
7
+ attr_accessor :namespace
7
8
 
8
- def initialize
9
- @integration_code = nil
10
- @version = 1.5
11
- @namespace = 'http://autotask.net/ATWS/v1_5/'
12
- end
9
+ def initialize
10
+ @integration_code = nil
11
+ @version = 1.5
12
+ @namespace = 'http://autotask.net/ATWS/v1_5/'
13
13
  end
14
+ end
14
15
 
15
- class << self
16
- def configuration
17
- @configuration ||= Configuration.new
18
- end
19
-
20
- def configure
21
- yield(configuration)
22
- end
16
+ class << self
17
+ def configuration
18
+ @configuration ||= Configuration.new
23
19
  end
24
20
 
21
+ def configure
22
+ yield(configuration)
23
+ end
24
+ end
25
25
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
- module Constants
5
- AUTOTASK_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
6
- end
4
+ module Constants
5
+ AUTOTASK_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
6
+ end
7
7
  end
@@ -1,26 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
+ # Represents the Autotask Entity Contact
5
+ class Contact
6
+ include Entity
4
7
 
5
- # Represents the Autotask Entity Contact
6
- class Contact
7
- include Entity
8
-
9
- FIELDS = %i[id Active AddressLine City Country CreateDate EMailAddress Extension FirstName AccountID LastName MobilePhone Phone State Title ZipCode].freeze
10
- .each do |field|
11
- self.attr_accessor :"#{field.to_s.underscore}"
12
- end
8
+ FIELDS = %i[id Active AddressLine City Country CreateDate EMailAddress Extension FirstName AccountID LastName MobilePhone Phone State Title ZipCode].freeze
9
+ .each do |field|
10
+ attr_accessor :"#{field.to_s.underscore}"
11
+ end
13
12
 
14
- def post_initialize
15
- belongs_to :account
16
- end
13
+ def post_initialize
14
+ belongs_to :account
15
+ end
17
16
 
18
- def full_name
19
- [@first_name, @last_name].join(' ')
20
- end
17
+ def full_name
18
+ [@first_name, @last_name].join(' ')
19
+ end
21
20
 
22
- def email
23
- @e_mail_address
24
- end
21
+ def email
22
+ @e_mail_address
25
23
  end
24
+ end
26
25
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AutotaskRuby
2
- class CreateResponse
3
- include Response
4
- end
4
+ class CreateResponse
5
+ include Response
6
+ end
5
7
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AutotaskRuby
2
- class DeleteResponse
3
- include Response
4
- end
4
+ class DeleteResponse
5
+ include Response
6
+ end
5
7
  end
@@ -1,113 +1,112 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
- # The base type for all objects represented by AutotaskRuby.
5
- # This module should extend any object type being used with AutoTask.
6
- module Entity
7
- include AutotaskRuby::Constants
8
- include AutotaskRuby::Association
9
-
10
- FIELDS = %i[id].freeze
11
-
12
- def self.included(base)
13
- base.const_set :FIELDS, Entity::FIELDS unless base.const_defined?(:FIELDS)
14
- end
15
-
16
- def initialize(options = {})
17
- @client = options if options.instance_of?(Client)
18
- return unless options.kind_of?(Hash)
19
-
20
- options.each do |k, v|
21
- instance_variable_set("@#{k}", v)
22
- end
23
-
24
- post_initialize
25
- end
26
-
27
- # default post_initialize methods.
28
- # Other classes that implement the Entity Class may override this.
29
- def post_initialize
30
-
31
- end
32
-
33
- # Iterates the fields of a Entity Class Type.
34
- # The fields are turned into instance variables.
35
- # Fields could include id, StartDateTime, Title, etc.
36
- def build(entity)
37
- self.class.const_get(:FIELDS).each do |field|
38
- instance_variable_set("@#{field}".underscore, field_set(entity, field))
39
- end
40
- end
41
-
42
- # updates the entity in the AutoTask API.
43
- # All fields are iterated and this builds the XML message that is sent to AutoTask.
44
- # Any field that is not filled out will be sent as empty. This means that it will wipe
45
- # any value that AutoTask has for that field.
46
- def update
47
- @client.soap_client.call(:update, message: "<Entity xsi:type=\"#{self.class.to_s.demodulize}\">#{fields_to_xml}</Entity>")
48
- end
49
-
50
- # creates an entity in AutoTask.
51
- def create
52
- result = @client.soap_client.call(:create, message: "<Entity xsi:type=\"#{self.class.to_s.demodulize}\">#{fields_to_xml}</Entity>")
53
- CreateResponse.new(@client, result)
54
- end
55
-
56
- # converts the AutoTask dateTime string value to a ActiveSupport::TimeWithZone object.
57
- # All dateTimes in AutoTask are in the Eastern Timezone.
58
- def to_date_time(arg)
59
- Time.find_zone!('Eastern Time (US & Canada)').parse(arg)
60
- end
61
-
62
- # Converts the specified field in the AutoTask XML response to the entity object field/attribute.
63
- # @param
64
- # - entity
65
- # - field
66
- def field_set(entity, field)
67
- node = entity.xpath("Autotask:#{field}", Autotask: AutotaskRuby.configuration.namespace)
68
-
69
- # entity may not contain all fields that are possible.
70
- # Example: The entity may not have a contact specified.
71
- return if node.blank?
72
-
73
- return node.text.to_i if node.attr('type').blank? || node.attr('type').text.eql?('xsd:int')
74
- return to_date_time(node.text) if node.attr('type').text.eql?('xsd:dateTime')
75
- return node.text.to_f if node.attr('type').text.eql?('xsd:double')
76
- return node.text.to_f if node.attr('type').text.eql?('xsd:decimal')
77
-
78
- node.text
79
- end
80
-
81
- def to_bool(arg)
82
- return true if arg == true || arg =~ (/(true|t|yes|y|1)$/i)
83
- return false if arg == false || arg.empty? || arg =~ (/(false|f|no|n|0)$/i)
84
- raise ArgumentError.new("invalid value for Boolean: \"#{arg}\"")
85
- end
86
-
87
- # converts the entity attributes to XML representation.
88
- # This is used when sending the object to the AutoTask API.
89
- def fields_to_xml
90
- str = ++''
91
-
92
- self.class.const_get(:FIELDS).each do |field|
93
- obj = instance_variable_get("@#{field}".underscore)
94
- next unless obj
95
-
96
- str << format_field_to_xml(field, obj)
97
- end
98
- str
99
- end
100
-
101
- # Returns the specified field as an XML element for the XML Body.
102
- # I.E. <id>xxx</id>
103
- # @param field_name
104
- # the field name
105
- # @param field_value
106
- # the field value.
107
- def format_field_to_xml(field_name, field_value)
108
- return "<#{field_name}>#{field_value.strftime(AUTOTASK_TIME_FORMAT)}</#{field_name}>" if field_value.instance_of?(ActiveSupport::TimeWithZone)
109
-
110
- "<#{field_name}>#{field_value}</#{field_name}>"
111
- end
4
+ # The base type for all objects represented by AutotaskRuby.
5
+ # This module should extend any object type being used with AutoTask.
6
+ module Entity
7
+ include AutotaskRuby::Constants
8
+ include AutotaskRuby::Association
9
+
10
+ FIELDS = %i[id].freeze
11
+
12
+ def self.included(base)
13
+ base.const_set :FIELDS, Entity::FIELDS unless base.const_defined?(:FIELDS)
14
+ end
15
+
16
+ def initialize(options = {})
17
+ @client = options if options.instance_of?(Client)
18
+ return unless options.is_a?(Hash)
19
+
20
+ options.each do |k, v|
21
+ instance_variable_set("@#{k}", v)
22
+ end
23
+
24
+ post_initialize
25
+ end
26
+
27
+ # default post_initialize methods.
28
+ # Other classes that implement the Entity Class may override this.
29
+ def post_initialize; end
30
+
31
+ # Iterates the fields of a Entity Class Type.
32
+ # The fields are turned into instance variables.
33
+ # Fields could include id, StartDateTime, Title, etc.
34
+ def build(entity)
35
+ self.class.const_get(:FIELDS).each do |field|
36
+ instance_variable_set("@#{field}".underscore, field_set(entity, field))
37
+ end
38
+ end
39
+
40
+ # updates the entity in the AutoTask API.
41
+ # All fields are iterated and this builds the XML message that is sent to AutoTask.
42
+ # Any field that is not filled out will be sent as empty. This means that it will wipe
43
+ # any value that AutoTask has for that field.
44
+ def update
45
+ @client.soap_client.call(:update, message: "<Entity xsi:type=\"#{self.class.to_s.demodulize}\">#{fields_to_xml}</Entity>")
46
+ end
47
+
48
+ # creates an entity in AutoTask.
49
+ def create
50
+ result = @client.soap_client.call(:create, message: "<Entity xsi:type=\"#{self.class.to_s.demodulize}\">#{fields_to_xml}</Entity>")
51
+ CreateResponse.new(@client, result)
52
+ end
53
+
54
+ # converts the AutoTask dateTime string value to a ActiveSupport::TimeWithZone object.
55
+ # All dateTimes in AutoTask are in the Eastern Timezone.
56
+ def to_date_time(arg)
57
+ Time.find_zone!('Eastern Time (US & Canada)').parse(arg)
58
+ end
59
+
60
+ # Converts the specified field in the AutoTask XML response to the entity object field/attribute.
61
+ # @param
62
+ # - entity
63
+ # - field
64
+ def field_set(entity, field)
65
+ node = entity.xpath("Autotask:#{field}", Autotask: AutotaskRuby.configuration.namespace)
66
+
67
+ # entity may not contain all fields that are possible.
68
+ # Example: The entity may not have a contact specified.
69
+ return if node.blank?
70
+
71
+ return node.text.to_i if node.attr('type').blank? || node.attr('type').text.eql?('xsd:int')
72
+ return to_date_time(node.text) if node.attr('type').text.eql?('xsd:dateTime')
73
+ return node.text.to_f if node.attr('type').text.eql?('xsd:double')
74
+ return node.text.to_f if node.attr('type').text.eql?('xsd:decimal')
75
+
76
+ node.text
77
+ end
78
+
79
+ def to_bool(arg)
80
+ return true if arg == true || arg =~ /(true|t|yes|y|1)$/i
81
+ return false if arg == false || arg.empty? || arg =~ /(false|f|no|n|0)$/i
82
+
83
+ raise ArgumentError, "invalid value for Boolean: \"#{arg}\""
84
+ end
85
+
86
+ # converts the entity attributes to XML representation.
87
+ # This is used when sending the object to the AutoTask API.
88
+ def fields_to_xml
89
+ str = ++''
90
+
91
+ self.class.const_get(:FIELDS).each do |field|
92
+ obj = instance_variable_get("@#{field}".underscore)
93
+ next unless obj
94
+
95
+ str << format_field_to_xml(field, obj)
96
+ end
97
+ str
98
+ end
99
+
100
+ # Returns the specified field as an XML element for the XML Body.
101
+ # I.E. <id>xxx</id>
102
+ # @param field_name
103
+ # the field name
104
+ # @param field_value
105
+ # the field value.
106
+ def format_field_to_xml(field_name, field_value)
107
+ return "<#{field_name}>#{field_value.strftime(AUTOTASK_TIME_FORMAT)}</#{field_name}>" if field_value.instance_of?(ActiveSupport::TimeWithZone)
108
+
109
+ "<#{field_name}>#{field_value}</#{field_name}>"
112
110
  end
111
+ end
113
112
  end
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
- # Converts Autotask Entity Fields into methods.
5
- module Fields
6
- def field(method_name)
7
- inst_variable_name = "@#{method_name}".to_sym
8
- define_method method_name do
9
- instance_variable_get inst_variable_name
10
- end
11
- define_method "#{method_name}=" do |new_value|
12
- instance_variable_set inst_variable_name, new_value
13
- end
14
- end
4
+ # Converts Autotask Entity Fields into methods.
5
+ module Fields
6
+ def field(method_name)
7
+ inst_variable_name = "@#{method_name}".to_sym
8
+ define_method method_name do
9
+ instance_variable_get inst_variable_name
10
+ end
11
+ define_method "#{method_name}=" do |new_value|
12
+ instance_variable_set inst_variable_name, new_value
13
+ end
15
14
  end
15
+ end
16
16
  end
@@ -1,20 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
+ # Represents the Autotask Entity Project
5
+ class Project
6
+ include Entity
4
7
 
5
- # Represents the Autotask Entity Project
6
- class Project
7
- include Entity
8
-
9
- FIELDS = %i[id ProjectName AccountID Type ProjectNumber Description CreateDateTime StartDateTime
10
- EndDateTime Duration EstimatedTime Status ProjectLeadResourceID StatusDetail StatusDateTime].freeze
11
- .each do |field|
12
- self.attr_accessor :"#{field.to_s.underscore}"
13
- end
8
+ FIELDS = %i[id ProjectName AccountID Type ProjectNumber Description CreateDateTime StartDateTime
9
+ EndDateTime Duration EstimatedTime Status ProjectLeadResourceID StatusDetail StatusDateTime].freeze
10
+ .each do |field|
11
+ attr_accessor :"#{field.to_s.underscore}"
12
+ end
14
13
 
15
- def post_initialize
16
- has_many :tasks
17
- belongs_to :account
18
- end
14
+ def post_initialize
15
+ has_many :tasks
16
+ belongs_to :account
19
17
  end
18
+ end
20
19
  end
@@ -1,22 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
- module Query
5
- def query(entity_type, field = 'id', op = 'equals', value)
6
- result = @client.soap_client.call(:query, message: "<sXML><![CDATA[<queryxml><entity>#{entity_type}</entity><query><field>#{field}<expression op=\"#{op}\">#{value}</expression></field></query></queryxml>]]></sXML>")
7
- AutotaskRuby::QueryResponse.new(@client, result)
8
- end
4
+ module Query
5
+ def query(entity_type, field = 'id', op = 'equals', value)
6
+ result = @client.soap_client.call(:query, message: "<sXML><![CDATA[<queryxml><entity>#{entity_type}</entity><query><field>#{field}<expression op=\"#{op}\">#{value}</expression></field></query></queryxml>]]></sXML>")
7
+ AutotaskRuby::QueryResponse.new(@client, result)
8
+ end
9
9
 
10
- # @param entity, id
11
- # pass in the entity_type, I.E. AccountToDo, Resource, etc. and the ID of the entity.
12
- # @return Entity
13
- # Returns a single Entity if a match was found.
14
- # Returns nil if no match is found.
15
- def find(entity, field = 'id', id)
16
- response = query(entity, field, id)
17
- return nil if response.entities.empty?
10
+ # @param entity, id
11
+ # pass in the entity_type, I.E. AccountToDo, Resource, etc. and the ID of the entity.
12
+ # @return Entity
13
+ # Returns a single Entity if a match was found.
14
+ # Returns nil if no match is found.
15
+ def find(entity, field = 'id', id)
16
+ response = query(entity, field, id)
17
+ return nil if response.entities.empty?
18
18
 
19
- response.entities.first
20
- end
19
+ response.entities.first
21
20
  end
21
+ end
22
22
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutotaskRuby
4
- class QueryResponse
5
- include Constants
6
- include Response
7
- end
4
+ class QueryResponse
5
+ include Constants
6
+ include Response
7
+ end
8
8
  end