nearmiss-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37c92bb3b0a1da8954fc384ba187fff5e0c17cff
4
+ data.tar.gz: 83345b933cb9dcaa559abf80d6a01b5ba12f82b3
5
+ SHA512:
6
+ metadata.gz: 911b84234c34e43330e263e864b46da974fc5cd7efd1e3c06e15dedb1ead89e13b645aeb9c94950682efe587c2cacdff3558a16de18808524188d17d0611a0d8
7
+ data.tar.gz: d15b9d4d665d0d824118c47d0c4546975e3415e8035084c72640862880ccc6b710e3e0efd2b181aa00395c378f01850fe27699f0c2a8ecebd92e13d888ae758d
data/AUTHORS.md ADDED
@@ -0,0 +1,7 @@
1
+ Authors
2
+ =======
3
+
4
+ A huge thanks to all of our contributors:
5
+
6
+
7
+ - This could be you!
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ ruby-nearmiss changelog
2
+ =====================
3
+
4
+ Version 1.0.1
5
+ -------------
6
+
7
+ Release January 4, 2016
8
+
9
+ - Fixed fetching projects
10
+
11
+ Version 1.0.0
12
+ -------------
13
+
14
+ Release January 3, 2016
15
+
16
+ - Intro
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :spec do
6
+ # gem 'jruby-openssl', :platforms => :jruby
7
+ gem 'rake'
8
+ gem 'rspec', '~> 3.3.0'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016, Markus Klooth
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,188 @@
1
+
2
+ # nearmiss-ruby
3
+
4
+ A module for using the Nearmiss REST API and generating valid [TwiML](http://www.nearmissapp.com/docs/api/). [Click here to read the full documentation.][documentation]
5
+
6
+ ## Installation
7
+
8
+ To install using [Bundler][bundler] grab the latest stable version:
9
+
10
+ ```ruby
11
+ gem 'nearmiss-ruby', '~> 1.0.0'
12
+ ```
13
+
14
+ To manually install `nearmiss-ruby` via [Rubygems][rubygems] simply gem install:
15
+
16
+ # nearmiss-ruby
17
+
18
+ A module for using the Nearmiss REST API and generating valid [TwiML](http://www.nearmissapp.com/docs/api/). [Click here to read the full documentation.][documentation]
19
+
20
+ ## Installation
21
+
22
+ To install using [Bundler][bundler] grab the latest stable version:
23
+
24
+ ```ruby
25
+ gem 'nearmiss-ruby', '~> 1.0.0'
26
+ ```
27
+
28
+ To manually install `nearmiss-ruby` via [Rubygems][rubygems] simply gem install:
29
+
30
+ ```bash
31
+ gem install nearmiss-ruby
32
+ ```
33
+
34
+ To build and install the development branch yourself from the latest source:
35
+
36
+ ```bash
37
+ git clone git@github.com:nearmiss/nearmiss-ruby.git
38
+ cd nearmiss-ruby
39
+ make install
40
+ ```
41
+
42
+ ## Getting Started With REST
43
+
44
+ ### Setup Work
45
+
46
+ ``` ruby
47
+ require 'rubygems' # not necessary with ruby 1.9 but included for completeness
48
+ require 'nearmiss-ruby'
49
+
50
+ # put your own credentials here
51
+ account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
52
+ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
53
+
54
+ # set up a client to talk to the Nearmiss REST API
55
+ @client = Nearmiss::REST::Client.new account_sid, auth_token
56
+
57
+ # alternatively, you can preconfigure the client like so
58
+ Nearmiss.configure do |config|
59
+ config.account_sid = account_sid
60
+ config.auth_token = auth_token
61
+ end
62
+
63
+ # and then you can create a new client without parameters
64
+ @client = Nearmiss::REST::Client.new
65
+ ```
66
+
67
+
68
+
69
+ ### List Projects
70
+ You can look up a list of all projects.
71
+
72
+ ``` ruby
73
+ # list calls made or received on or after May 13, 2013
74
+ @client.project_list("start_time>" => "2013-05-13") # Notice we omit the "=" in the "start_time>=" parameter because it is automatically added
75
+ ```
76
+
77
+ ### List Categories
78
+ You can look up a list of all defined categories.
79
+
80
+ ``` ruby
81
+ # list calls made or received on or after May 13, 2013
82
+ @client.categories() # Notice we omit the "=" in the "start_time>=" parameter because it is automatically added
83
+ ```
84
+
85
+
86
+
87
+ ## Supported Ruby Versions
88
+
89
+ This library supports and is [tested against][travis] the following Ruby
90
+ implementations:
91
+
92
+ - Ruby 2.2.0
93
+ - Ruby 2.1.0
94
+ - Ruby 2.0.0
95
+ - Ruby 1.9.3
96
+ - [JRuby][jruby]
97
+ - [Rubinius][rubinius]
98
+
99
+ ## Getting help
100
+
101
+ If you need help installing or using the library, please contact Nearmiss Support at help@nearmiss.com first. Nearmiss's Support staff are well-versed in all of the Nearmiss Helper Libraries, and usually reply within 24 hours.
102
+
103
+ If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!
104
+
105
+ ## More Information
106
+
107
+ There are more detailed examples in the included [examples][examples]
108
+ directory. Also for those upgrading, the [upgrade guide][upgrade] is available in the [nearmiss-ruby github wiki][wiki].
109
+
110
+ ```bash
111
+ gem install nearmiss-ruby
112
+ ```
113
+
114
+ To build and install the development branch yourself from the latest source:
115
+
116
+ ```bash
117
+ git clone git@github.com:nearmiss/nearmiss-ruby.git
118
+ cd nearmiss-ruby
119
+ make install
120
+ ```
121
+
122
+ ## Getting Started With REST
123
+
124
+ ### Setup Work
125
+
126
+ ``` ruby
127
+ require 'rubygems' # not necessary with ruby 1.9 but included for completeness
128
+ require 'nearmiss-ruby'
129
+
130
+ # put your own credentials here
131
+ account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
132
+ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
133
+
134
+ # set up a client to talk to the Nearmiss REST API
135
+ @client = Nearmiss::REST::Client.new account_sid, auth_token
136
+
137
+ # alternatively, you can preconfigure the client like so
138
+ Nearmiss.configure do |config|
139
+ config.account_sid = account_sid
140
+ config.auth_token = auth_token
141
+ end
142
+
143
+ # and then you can create a new client without parameters
144
+ @client = Nearmiss::REST::Client.new
145
+ ```
146
+
147
+
148
+
149
+ ### List Projects
150
+ You can look up a list of all projects.
151
+
152
+ ``` ruby
153
+ # list calls made or received on or after May 13, 2013
154
+ @client.project_list("start_time>" => "2013-05-13") # Notice we omit the "=" in the "start_time>=" parameter because it is automatically added
155
+ ```
156
+
157
+ ### List Categories
158
+ You can look up a list of all defined categories.
159
+
160
+ ``` ruby
161
+ # list calls made or received on or after May 13, 2013
162
+ @client.categories() # Notice we omit the "=" in the "start_time>=" parameter because it is automatically added
163
+ ```
164
+
165
+
166
+
167
+ ## Supported Ruby Versions
168
+
169
+ This library supports and is [tested against][travis] the following Ruby
170
+ implementations:
171
+
172
+ - Ruby 2.2.0
173
+ - Ruby 2.1.0
174
+ - Ruby 2.0.0
175
+ - Ruby 1.9.3
176
+ - [JRuby][jruby]
177
+ - [Rubinius][rubinius]
178
+
179
+ ## Getting help
180
+
181
+ If you need help installing or using the library, please contact Nearmiss Support at help@nearmiss.com first. Nearmiss's Support staff are well-versed in all of the Nearmiss Helper Libraries, and usually reply within 24 hours.
182
+
183
+ If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!
184
+
185
+ ## More Information
186
+
187
+ There are more detailed examples in the included [examples][examples]
188
+ directory. Also for those upgrading, the [upgrade guide][upgrade] is available in the [nearmiss-ruby github wiki][wiki].
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rspec/core/rake_task'
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
10
+ task test: :spec
@@ -0,0 +1,14 @@
1
+ module Nearmiss
2
+
3
+ # Extracts options from method arguments
4
+ # @private
5
+ class Arguments < Array
6
+ attr_reader :options
7
+
8
+ def initialize(args)
9
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
10
+ super(args)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ module Nearmiss
2
+
3
+ # Authentication methods for {Nearmiss::Client}
4
+ module Authentication
5
+
6
+ # Indicates if the client was supplied Basic Auth
7
+ # username and password
8
+ #
9
+ # @see
10
+ # @return [Boolean]
11
+ def basic_authenticated?
12
+ !!(@email && @password)
13
+ end
14
+
15
+ # Indicates if the client was supplied an OAuth
16
+ # access token
17
+ #
18
+ # @see
19
+ # @return [Boolean]
20
+ def token_authenticated?
21
+ !!@access_token
22
+ end
23
+
24
+ def sign_in
25
+
26
+ response = post 'auth/sign_in', { email: @email, password: @password}
27
+ update_headers(last_response.headers)
28
+ reset_agent
29
+ @me = response[:data]
30
+ end
31
+ alias :login :sign_in
32
+
33
+ def update_headers(headers)
34
+ # puts "update"
35
+ # last_response.headers
36
+ @client_id = headers["client"]
37
+ @access_token = headers["access-token"]
38
+ @expiry = headers["expiry"]
39
+ @uid = headers["uid"]
40
+
41
+ end
42
+
43
+ # Closes the current active session by expiring the ticket.
44
+ #
45
+ def sign_out
46
+ post "api/logout"
47
+ @me = nil
48
+ end
49
+ alias :logout :sign_out
50
+
51
+ # Check is a user is currently signed in.
52
+ #
53
+ # @return [Boolean]
54
+ def signed_in?
55
+ !!@me
56
+ end
57
+
58
+ end
59
+
60
+ 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,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
@@ -0,0 +1,155 @@
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
+ # Create an incident
35
+ #
36
+ # @param options [Hash] Incident information.
37
+ # @option options [String] :title Name of incident
38
+ # @option options [String] :note Description of what happened
39
+ # @option options [String] :category_id ID of associated category
40
+ # @option options [String] :project_id ID of the project where the incident occured
41
+ # @option options [String] :company Which company did the incident
42
+ # @option options [String] :date When did the nearmiss occur
43
+ # @option options [String] :trade e.g. is actually the activity
44
+ # @option options [Boolean] :is_public Submit the nearmiss publically or private
45
+ #
46
+ # @option options [String] :bad_weather "rainy", "sunny", "cloudy", "windy"
47
+ # @option options [String] :injured Answers: "yes", "no"
48
+ # @option options [String] :jha Answers: "yes", "no"
49
+ # @option options [String] :messy Answers: "yes", "no"
50
+ #
51
+ # @option options [Array] :attachments
52
+ #
53
+ # @return [Sawyer::Resource] Newly created incident info
54
+ def create_incident(options = {})
55
+ post 'incidents', options
56
+ end
57
+ alias :create_nearmiss :create_incident
58
+
59
+ def update_incident(incident, options = {})
60
+ patch "incidents/#{incident}", options
61
+ end
62
+ alias :edit_incident :update_incident
63
+ alias :update_nearmiss :update_incident
64
+ alias :edit_nearmiss :update_incident
65
+
66
+ # List incident comments
67
+ #
68
+ # @param incident_id [String] Incident Id.
69
+ # @return [Array<Sawyer::Resource>] Array of hashes representing comments.
70
+ # @example
71
+ # Nearmiss.incident_comments('3528ae645')
72
+ def incident_comments(incident_id, options = {})
73
+ paginate "incidents/#{incident_id}/comments", options
74
+ end
75
+ alias :nearmiss_comments :incident_comments
76
+
77
+ # Get incident comment
78
+ #
79
+ # @param incident_id [String] Id of the incident.
80
+ # @param comment_id [Integer] Id of the incident comment.
81
+ # @return [Sawyer::Resource] Hash representing incident comment.
82
+ # @example
83
+ # Nearmiss.incident_comment('208sdaz3', 1451398)
84
+ def incident_comment(incident_id, comment_id, options = {})
85
+ get "incidents/#{incident_id}/comments/#{comment_id}", options
86
+ end
87
+ alias :nearmiss_comment :incident_comment
88
+
89
+ # Create incident comment
90
+ #
91
+ # @param incident_id [String] Id of the incident.
92
+ # @param comment [String] Comment contents.
93
+ # @return [Sawyer::Resource] Hash representing incident comment.
94
+ # @example
95
+ # Nearmiss.incident_comment('208sdaz3', "Some text")
96
+ def create_incident_comment(incident_id, comment, options = {})
97
+ options.merge!({text: comment})
98
+ post "incidents/#{incident_id}/comments", options
99
+ end
100
+ alias :create_nearmiss_comment :create_incident_comment
101
+
102
+ # Update incident comment
103
+ #
104
+ # @param incident_id [String] Id of the incident.
105
+ # @param comment_id [String] Id of the comment.
106
+ # @param comment [String] Comment contents.
107
+ # @return [Sawyer::Resource] Hash representing incident comment.
108
+ # @example
109
+ # Nearmiss.incident_comment('208sdaz3', "Some text")
110
+ def update_incident_comment(incident_id, comment_id, comment, options = {})
111
+ options.merge!({text: comment})
112
+ patch "incidents/#{incident_id}/comments/#{comment_id}", options
113
+ end
114
+ alias :edit_incident_comment :update_incident_comment
115
+ alias :update_nearmiss_comment :update_incident_comment
116
+ alias :edit_nearmiss_comment :update_incident_comment
117
+
118
+ # Delete incident comment
119
+ #
120
+ # Requires authenticated client.
121
+ #
122
+ # @param incident_id [String] Id of the incident.
123
+ # @param comment_id [Integer] Id of the comment to delete.
124
+ # @return [Boolean] True if comment deleted, false otherwise.
125
+ # @example
126
+ # @client.delete_incident_comment('208sdaz3', '586399')
127
+ def delete_incident_comment(incident_id, comment_id, options = {})
128
+ boolean_from_response(:delete, "incidents/#{incident_id}/comments/#{comment_id}", options)
129
+ end
130
+ alias :delete_nearmiss_comment :delete_incident_comment
131
+
132
+ protected
133
+
134
+ def iso8601(date)
135
+ if date.respond_to?(:iso8601)
136
+ date.iso8601
137
+ else
138
+ date.strftime("%Y-%m-%dT%H:%M:%S%Z")
139
+ end
140
+ end
141
+ # Parses the given string representation of a date, throwing a meaningful exception
142
+ # (containing the date that failed to parse) in case of failure.
143
+ #
144
+ # @param date [String] String representation of a date
145
+ # @return [DateTime]
146
+ def parse_date(date)
147
+ date = DateTime.parse(date.to_s)
148
+ rescue ArgumentError
149
+ raise ArgumentError, "#{date} is not a valid date"
150
+ end
151
+
152
+
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,31 @@
1
+ module Nearmiss
2
+ class Client
3
+
4
+ # Methods for the Notifications API
5
+ #
6
+ module Notifications
7
+
8
+ # List notifications for the current user
9
+ #
10
+ # If user is not supplied, repositories for the current
11
+ # authenticated user are returned.
12
+ #
13
+ # @note If the user provided is a GitHub organization, only the
14
+ # organization's public repositories will be listed. For retrieving
15
+ # organization repositories the {Organizations#organization_repositories}
16
+ # method should be used instead.
17
+ # @see https://developer.github.com/v3/repos/#list-your-repositories
18
+ # @see https://developer.github.com/v3/repos/#list-user-repositories
19
+ # @param user [Integer, String] Optional GitHub user login or id for which
20
+ # to list repos.
21
+ # @return [Array<Sawyer::Resource>] List of projects
22
+ def notifications(options = {})
23
+ paginate "notifications", options
24
+ end
25
+ alias :list_notifications :notifications
26
+
27
+
28
+
29
+ end
30
+ end
31
+ end