toolhound-ruby 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,113 @@
1
+ # require "set"
2
+ require "toolhound-ruby/authentication"
3
+ require "toolhound-ruby/configurable"
4
+ require "toolhound-ruby/client/projects"
5
+ # require "toolhound-ruby/incident"
6
+
7
+
8
+ module Toolhound
9
+
10
+ class Client
11
+ include Toolhound::Authentication
12
+ include Toolhound::Configurable
13
+
14
+ include Toolhound::Client::Projects
15
+ # include Toolhound::Client::Bookmarks
16
+ # include Toolhound::Client::Categories
17
+ # include Toolhound::Client::Incidents
18
+ # include Toolhound::Client::Notifications
19
+ # include Toolhound::Client::Projects
20
+ # include Toolhound::Client::RateLimit
21
+ # include Toolhound::Client::Users
22
+ # include Toolhound::Client::Companies
23
+ # include Toolhound::Client::Attachments
24
+
25
+ # include Toolhound::Client::Users
26
+ # include Toolhound::Client::ProjectLibrary
27
+ # include Toolhound::Client::Projects
28
+ # include Toolhound::Client::Templates
29
+ # include Toolhound::Client::Checklists
30
+ # include Toolhound::Client::Tasks
31
+ # include Toolhound::Client::Issues
32
+ # include Toolhound::Client::Utils
33
+
34
+ # def projects
35
+ #
36
+ # end
37
+ # attr_accessor :access_token, :client_id, :uid, :expiry, :me
38
+ attr_accessor :connection, :dataserver, :username, :password, :port
39
+
40
+ def initialize(options = {})
41
+
42
+ # Use options passed in, but fall back to module defaults
43
+ Toolhound::Configurable.keys.each do |key|
44
+ instance_variable_set(:"@#{key}", options[key] || Toolhound.instance_variable_get(:"@#{key}"))
45
+ end
46
+
47
+ sign_in if authenticatable?
48
+ end
49
+
50
+ # Compares client options to a Hash of requested options
51
+ #
52
+ # @param opts [Hash] Options to compare with current client options
53
+ # @return [Boolean]
54
+ def same_options?(opts)
55
+ opts.hash == options.hash
56
+ end
57
+
58
+
59
+ def inspect # :nodoc:
60
+
61
+ inspected = super
62
+
63
+ # mask password
64
+ inspected = inspected.gsub! @password, "*******" if @password
65
+ # Only show last 4 of token, secret
66
+ # if @access_token
67
+ # inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
68
+ # end
69
+ # if @client_secret
70
+ # inspected = inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}"
71
+ # end
72
+
73
+ inspected
74
+ end
75
+
76
+
77
+ # Set username for authentication
78
+ #
79
+ # @param value [String] Toolhound-field username
80
+ def username=(value)
81
+ reset_connection
82
+ @email = value
83
+ end
84
+
85
+ # Set password for authentication
86
+ #
87
+ # @param value [String] Toolhound-field password
88
+ def password=(value)
89
+ reset_connection
90
+ @password = value
91
+ end
92
+
93
+ def reset_connection
94
+ @connection = nil
95
+ end
96
+
97
+
98
+ # Wrapper around Kernel#warn to print warnings unless
99
+ # TOOLHOUND_SILENT is set to true.
100
+ #
101
+ # @return [nil]
102
+ def nearmiss_warn(*message)
103
+ unless ENV['TOOLHOUND_SILENT']
104
+ warn message
105
+ end
106
+ end
107
+
108
+
109
+
110
+ end
111
+
112
+
113
+ end
@@ -0,0 +1,14 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Bookmarks API
5
+ #
6
+ module Account
7
+
8
+ def account(options = {})
9
+ get "account", options
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,88 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Attachments API
5
+ #
6
+ module Attachments
7
+
8
+ # List nearmiss attachments
9
+ #
10
+ # @return [Array<Sawyer::Resource>] List of attachments
11
+ #
12
+ def attachments(options = {})
13
+ since = options[:since] || options["since"]
14
+
15
+ options.merge!(since: iso8601(parse_date(since))) if since
16
+
17
+ paginate "attachments", options
18
+ end
19
+ alias :list_attachments :attachments
20
+
21
+ # Get a single attachment
22
+ #
23
+ # @param attachment [String] ID of attachment to fetch
24
+ # @return [Sawyer::Resource] Incident information
25
+ #
26
+ def attachment(attachment, options = {})
27
+ get "attachments/#{attachment}", options
28
+ end
29
+
30
+ # Project attachments
31
+ #
32
+ # @param project [String, Hash, Incident] Incident
33
+ # @return [Sawyer::Resource] Incident information
34
+ #
35
+ def incident_attachments(incident, options = {})
36
+
37
+ paginate "#{Incident.new(incident).path}/attachments", options
38
+
39
+ end
40
+ alias :nearmiss_attachments :incident_attachments
41
+
42
+
43
+ # Create an attachment
44
+ #
45
+ # @param options [Hash] Attachment information.
46
+ # @option options [String] :created_by_id Id of person who created this attachment
47
+ # @option options [String] :original_filename title of file
48
+ # @option options [String] :content_type e.g. `image/jpeg`
49
+ # @option options [String] :attachable_id associated ID of resource this attachment belongs to
50
+ # @option options [String] :attachable_type e.g. Incident
51
+
52
+ #
53
+ # @return [Sawyer::Resource] Newly created attachment info
54
+ def create_attachment(options = {})
55
+ post 'attachments', options
56
+ end
57
+
58
+
59
+ def update_attachment(attachment, options = {})
60
+ patch "attachments/#{attachment}", options
61
+ end
62
+ alias :edit_attachment :update_attachment
63
+
64
+
65
+ protected
66
+
67
+ def iso8601(date)
68
+ if date.respond_to?(:iso8601)
69
+ date.iso8601
70
+ else
71
+ date.strftime("%Y-%m-%dT%H:%M:%S%Z")
72
+ end
73
+ end
74
+ # Parses the given string representation of a date, throwing a meaningful exception
75
+ # (containing the date that failed to parse) in case of failure.
76
+ #
77
+ # @param date [String] String representation of a date
78
+ # @return [DateTime]
79
+ def parse_date(date)
80
+ date = DateTime.parse(date.to_s)
81
+ rescue ArgumentError
82
+ raise ArgumentError, "#{date} is not a valid date"
83
+ end
84
+
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,39 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Bookmarks API
5
+ #
6
+ module Bookmarks
7
+
8
+ # List bookmarks
9
+ #
10
+ # @return [Array<Sawyer::Resource>] List of bookmarks
11
+ def bookmarks(options = {})
12
+ paginate "bookmarks", options
13
+ end
14
+ alias :list_bookmarks :bookmarks
15
+
16
+ # Get a single bookmark
17
+ #
18
+ # @param bookmark [String] ID of bookmark to fetch
19
+ # @return [Sawyer::Resource] Bookmark information
20
+ #
21
+ def bookmark(bookmark, options={})
22
+ get "bookmarks/#{bookmark}", options
23
+ end
24
+
25
+ # Delete a bookmark
26
+ #
27
+ # @param bookmark_id [String] Id of the bookmark.
28
+ # @return [Boolean] True if bookmark deleted, false otherwise.
29
+ # @example
30
+ # @client.delete_bookmark('208sdaz3')
31
+ #
32
+ def delete_bookmark(bookmark_id, options={})
33
+ boolean_from_response(:delete, "bookmarks/#{bookmark_id}", options)
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,72 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Categories API
5
+ #
6
+ module Categories
7
+
8
+ # List categories
9
+ #
10
+ # @return [Array<Sawyer::Resource>] List of categories
11
+ def categories(options = {})
12
+ paginate "categories", options
13
+ end
14
+ alias :list_categories :categories
15
+ alias :list_cats :categories
16
+ alias :cats :categories
17
+
18
+ # Get a single category
19
+ #
20
+ # @param category [String] ID of category to fetch
21
+ # @return [Sawyer::Resource] Category information
22
+ #
23
+ def category(category, options={})
24
+ get "categories/#{category}", options
25
+ end
26
+ alias :cat :category
27
+
28
+
29
+ # Create a category
30
+ #
31
+ # @param options [Hash] Category information.
32
+ # @option options [String] :name e.g. Name of category
33
+ # @return [Sawyer::Resource] Newly created category info
34
+ def create_category(options = {})
35
+ post 'categories', options
36
+ end
37
+ alias :create_cat :create_category
38
+
39
+ # Edit a category
40
+ #
41
+ # @param options [Hash] Project information.
42
+ # @option options [String] :name e.g. Tools
43
+ #
44
+ # @return
45
+ # [Sawyer::Resource] Edited category info
46
+ # @example Update a category
47
+ # @client.edit_category('some_id', {
48
+ # name: "New name of category",
49
+ # })
50
+
51
+ def edit_category(category, options = {})
52
+ patch "categories/#{category}", options
53
+ end
54
+ alias :edit_cat :edit_category
55
+
56
+
57
+ # Delete a category
58
+ #
59
+ # @param category [String] Project ID
60
+ # @return [Boolean] Indicating success of deletion
61
+ #
62
+ def delete_category(category, options = {})
63
+ boolean_from_response :delete, "categories/#{category}", options
64
+ end
65
+ alias :delete_cat :delete_category
66
+ alias :remove_category :delete_category
67
+ alias :remove_cat :delete_category
68
+
69
+
70
+ end
71
+ end
72
+ end
File without changes
@@ -0,0 +1,76 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Companies API
5
+ #
6
+ module Companies
7
+
8
+ # List companies
9
+ #
10
+ # @note Shows a list of companies for the users organization aka account
11
+ #
12
+ # @return [Array<Sawyer::Resource>] List of companys
13
+ def companies(options = {})
14
+ paginate "companies", options
15
+ end
16
+ alias :list_companies :companies
17
+
18
+ # Get a single company
19
+ #
20
+ # @param company [String] UUID of company to fetch
21
+ # @return [Sawyer::Resource] Project information
22
+ #
23
+ def company(company, options = {})
24
+ get "#{company_path(company)}", options
25
+ end
26
+
27
+ # Create a company
28
+ #
29
+ # @param options [Hash] Project information.
30
+ # @option options [String] :name e.g. Berkeley Art Museum
31
+ # @option options [String] :company_id e.g. 10611.70
32
+ # @return [Sawyer::Resource] Newly created company info
33
+ def create_company(options = {})
34
+ post 'companies', options
35
+ end
36
+
37
+ # Edit a company
38
+ #
39
+ # @param options [Hash] Project information.
40
+ # @option options [String] :name e.g. Berkeley Art Museum
41
+ # @option options [String] :company_id e.g. 10611.70
42
+ #
43
+ # @return
44
+ # [Sawyer::Resource] Newly created company info
45
+ # @example Update a company
46
+ # @client.edit_company('some_id', {
47
+ # name: "New name of company",
48
+ # company_id: "1043.32"
49
+ # })
50
+ #
51
+ def edit_company(company, options = {})
52
+ patch "#{company_path(company)}", options
53
+ end
54
+
55
+ # Delete a company
56
+ #
57
+ # @param company [String] Project ID
58
+ # @return [Boolean] Indicating success of deletion
59
+ #
60
+ def delete_company(company, options = {})
61
+ boolean_from_response :delete, "companies/#{company}", options
62
+ end
63
+ alias :remove_company :delete_company
64
+
65
+ private
66
+
67
+ def company_path(id)
68
+ if uuid?(id)
69
+ "companies/#{id}"
70
+ else
71
+ "company/#{id}"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,167 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Incidents API
5
+ #
6
+ module Incidents
7
+
8
+ # List nearmiss incidents
9
+ #
10
+ # @return [Array<Sawyer::Resource>] List of incidents
11
+ #
12
+ def incidents(options = {})
13
+ since = options[:since] || options["since"]
14
+
15
+ options.merge!(since: iso8601(parse_date(since))) if since
16
+
17
+ paginate "incidents", options
18
+ end
19
+ alias :list_incidents :incidents
20
+ alias :list_nearmisses :incidents
21
+ alias :nearmisses :incidents
22
+
23
+
24
+ # Get a single incident
25
+ #
26
+ # @param incident [String] ID of incident to fetch
27
+ # @return [Sawyer::Resource] Incident information
28
+ #
29
+ def incident(incident, options = {})
30
+ get "incidents/#{incident}", options
31
+ end
32
+ alias :nearmiss :incident
33
+
34
+ # Project incidents
35
+ #
36
+ # @param project [String] ID of project
37
+ # @return [Sawyer::Resource] Incident information
38
+ #
39
+ def project_incidents(project, options = {})
40
+
41
+ paginate "#{Project.new(project).path}/incidents", options
42
+
43
+ end
44
+
45
+
46
+ # Create an incident
47
+ #
48
+ # @param options [Hash] Incident information.
49
+ # @option options [String] :title Name of incident
50
+ # @option options [String] :note Description of what happened
51
+ # @option options [String] :category_id ID of associated category
52
+ # @option options [String] :project_id ID of the project where the incident occured
53
+ # @option options [String] :company Which company did the incident
54
+ # @option options [String] :date When did the nearmiss occur
55
+ # @option options [String] :trade e.g. is actually the activity
56
+ # @option options [Boolean] :is_public Submit the nearmiss publically or private
57
+ #
58
+ # @option options [String] :bad_weather "rainy", "sunny", "cloudy", "windy"
59
+ # @option options [String] :injured Answers: "yes", "no"
60
+ # @option options [String] :jha Answers: "yes", "no"
61
+ # @option options [String] :messy Answers: "yes", "no"
62
+ #
63
+ # @option options [Array] :attachments
64
+ #
65
+ # @return [Sawyer::Resource] Newly created incident info
66
+ def create_incident(options = {})
67
+ post 'incidents', options
68
+ end
69
+ alias :create_nearmiss :create_incident
70
+
71
+ def update_incident(incident, options = {})
72
+ patch "incidents/#{incident}", options
73
+ end
74
+ alias :edit_incident :update_incident
75
+ alias :update_nearmiss :update_incident
76
+ alias :edit_nearmiss :update_incident
77
+
78
+ # List incident comments
79
+ #
80
+ # @param incident_id [String] Incident Id.
81
+ # @return [Array<Sawyer::Resource>] Array of hashes representing comments.
82
+ # @example
83
+ # Nearmiss.incident_comments('3528ae645')
84
+ def incident_comments(incident_id, options = {})
85
+ paginate "incidents/#{incident_id}/comments", options
86
+ end
87
+ alias :nearmiss_comments :incident_comments
88
+
89
+ # Get incident comment
90
+ #
91
+ # @param incident_id [String] Id of the incident.
92
+ # @param comment_id [Integer] Id of the incident comment.
93
+ # @return [Sawyer::Resource] Hash representing incident comment.
94
+ # @example
95
+ # Nearmiss.incident_comment('208sdaz3', 1451398)
96
+ def incident_comment(incident_id, comment_id, options = {})
97
+ get "incidents/#{incident_id}/comments/#{comment_id}", options
98
+ end
99
+ alias :nearmiss_comment :incident_comment
100
+
101
+ # Create incident comment
102
+ #
103
+ # @param incident_id [String] Id of the incident.
104
+ # @param comment [String] Comment contents.
105
+ # @return [Sawyer::Resource] Hash representing incident comment.
106
+ # @example
107
+ # Nearmiss.incident_comment('208sdaz3', "Some text")
108
+ def create_incident_comment(incident_id, comment, options = {})
109
+ options.merge!({text: comment})
110
+ post "incidents/#{incident_id}/comments", options
111
+ end
112
+ alias :create_nearmiss_comment :create_incident_comment
113
+
114
+ # Update incident comment
115
+ #
116
+ # @param incident_id [String] Id of the incident.
117
+ # @param comment_id [String] Id of the comment.
118
+ # @param comment [String] Comment contents.
119
+ # @return [Sawyer::Resource] Hash representing incident comment.
120
+ # @example
121
+ # Nearmiss.incident_comment('208sdaz3', "Some text")
122
+ def update_incident_comment(incident_id, comment_id, comment, options = {})
123
+ options.merge!({text: comment})
124
+ patch "incidents/#{incident_id}/comments/#{comment_id}", options
125
+ end
126
+ alias :edit_incident_comment :update_incident_comment
127
+ alias :update_nearmiss_comment :update_incident_comment
128
+ alias :edit_nearmiss_comment :update_incident_comment
129
+
130
+ # Delete incident comment
131
+ #
132
+ # Requires authenticated client.
133
+ #
134
+ # @param incident_id [String] Id of the incident.
135
+ # @param comment_id [Integer] Id of the comment to delete.
136
+ # @return [Boolean] True if comment deleted, false otherwise.
137
+ # @example
138
+ # @client.delete_incident_comment('208sdaz3', '586399')
139
+ def delete_incident_comment(incident_id, comment_id, options = {})
140
+ boolean_from_response(:delete, "incidents/#{incident_id}/comments/#{comment_id}", options)
141
+ end
142
+ alias :delete_nearmiss_comment :delete_incident_comment
143
+
144
+ protected
145
+
146
+ def iso8601(date)
147
+ if date.respond_to?(:iso8601)
148
+ date.iso8601
149
+ else
150
+ date.strftime("%Y-%m-%dT%H:%M:%S%Z")
151
+ end
152
+ end
153
+ # Parses the given string representation of a date, throwing a meaningful exception
154
+ # (containing the date that failed to parse) in case of failure.
155
+ #
156
+ # @param date [String] String representation of a date
157
+ # @return [DateTime]
158
+ def parse_date(date)
159
+ date = DateTime.parse(date.to_s)
160
+ rescue ArgumentError
161
+ raise ArgumentError, "#{date} is not a valid date"
162
+ end
163
+
164
+
165
+ end
166
+ end
167
+ end