mvclient 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea542d1decef8e9c2785aa140ca9b2077d11f6eb
4
+ data.tar.gz: 26e612d47a42c82064ed90dfd9c496b3053a0ba2
5
+ SHA512:
6
+ metadata.gz: 9b23c70bdbcddf3f924094247a38af94d677e1e618c0a6fa09eba1c37000d48f1b0c797f096a3016ccd941dcc78d9213e2869bde7f9ef06ccb40f348239deb07
7
+ data.tar.gz: fb0cb02cff29445a011c81d3ef976f38e1dab4a9ba2dfbcfa8e430757491b6d4327465821b876197a522b4a76dc4f2fef759dc8c39e23071abc5c657093c09c9
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem 'byebug'
3
+ gem 'httparty', '~> 0'
4
+ gem 'http-cookie', '~> 1'
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,136 @@
1
+ # mvclient
2
+
3
+ This is a minimal client wrapper for the (not yet published!) Motivosity API. It allows you to check balances,
4
+ send money, and retrieve the announcements and news feed. A limited [command-line tool](#command-line-tool) is also provided.
5
+
6
+ ## client library
7
+
8
+ ### Installation
9
+
10
+ ```
11
+ gem install mvclient
12
+ ```
13
+
14
+ ### Instantiation
15
+
16
+ ```ruby
17
+ require 'mvclient'
18
+ @client = Motivosity::Client.new
19
+ ```
20
+
21
+ ### Log in
22
+
23
+ ```ruby
24
+ @client.login! 'username@example.com', 'correct horse battery staple'
25
+ ```
26
+ Note that session information is stored in `~/.motivosity-session`, so it is not necessary to log in every time you create a Motivosity::Client object. This is useful for command-line operation (see below).
27
+
28
+ ### Log out
29
+
30
+ ```ruby
31
+ @client.logout!
32
+ ```
33
+ This will clear stored session information in `~/.motivosity-session`
34
+
35
+ ### Search for user
36
+
37
+ ```ruby
38
+ @client.search_for_user(search_term, ignore_self)
39
+ ```
40
+ returns a list of matching users
41
+ ```ruby
42
+ [{
43
+ "id" => "00000000-0000-user-0000-000000000000",
44
+ "fullName" => "Jane Doe",
45
+ "avatarUrl" => "user-placeholder.png",
46
+ }, ...]
47
+ ```
48
+
49
+ ### Get company values
50
+
51
+ ```ruby
52
+ @client.get_values
53
+ ```
54
+ returns a list of company values
55
+ ```ruby
56
+ [{
57
+ "id" : "39602196-7348-cval-aa03-4f8ef9ce45b8",
58
+ "name": "Customer Experience",
59
+ "description": "We aspire to create an awesome customer experience in every interaction with our product and people.",
60
+ ...}, ...]
61
+ ```
62
+
63
+ ### Get balances
64
+
65
+ ```ruby
66
+ @client.get_balances
67
+ ```
68
+ returns balance information
69
+ ```ruby
70
+ {
71
+ "cashReceiving" : 39, # money received
72
+ "cashGiving" : 10 # money available to give
73
+ }
74
+ ```
75
+
76
+ ### Send appreciation
77
+
78
+ ```ruby
79
+ @client.send_appreciation! user_id, 1.00, "Here, have a dollar", value_id
80
+ ```
81
+
82
+ ### Get announcements
83
+
84
+ ```ruby
85
+ @client.get_announcements(page_no) # 0-based
86
+ ```
87
+
88
+ ### Get news feed
89
+ ```ruby
90
+ # scope is one of :team, :extended_team, :department, or :company
91
+ @client.feed(:team, page_no) # 0-based
92
+ ```
93
+
94
+ <a id="command-line-tool"></a>
95
+ ## command-line tool
96
+
97
+ A command-line tool is also provided in `bin/mvclient.rb`. Use `--help` to receive usage information on the command line.
98
+
99
+ ### Log in
100
+
101
+ ```
102
+ mvclient login -u "user@example.com" -p "correct horse battery staple"
103
+ ```
104
+
105
+ ### Log out
106
+
107
+ ```
108
+ mvclient logout
109
+ ```
110
+
111
+ ### Get balance
112
+
113
+ ```
114
+ mvclient get_balance
115
+ ```
116
+ Example output:
117
+ ```
118
+ You can give $7.00
119
+ You can spend $21.00
120
+ ```
121
+
122
+ ### Send appreciation
123
+
124
+ ```
125
+ mvclient send_appreciation -u "Jane Doe" -a "1.00" -n "Note" -v "Customer Experience"
126
+ ```
127
+ The `-u` option accepts either a user ID or a name. Partial names are acceptable, but the call will fail unless exactly one name matches.
128
+
129
+ Example output:
130
+ ```
131
+ Success! Jane Doe has received your appreciation.
132
+ ```
133
+
134
+ ### Debugging
135
+
136
+ Set MOTIVOSITY_DEBUG=1 to enable logging of all communication with Motivosity to stderr
@@ -0,0 +1,166 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/mvclient'
5
+
6
+ USER_ID_REGEX = /\A\h{8}-\h{4}-user-\h{4}-\h{12}\z/
7
+
8
+ $options = {}
9
+
10
+ def money_str(value)
11
+ sprintf('$%.2f', value)
12
+ end
13
+
14
+ def get_options
15
+ OptionParser.new do |opts|
16
+ yield opts
17
+ opts.on("-h", "--help", "Prints this help") do
18
+ puts opts
19
+ exit(1)
20
+ end
21
+ end.parse!
22
+ end
23
+
24
+ def require_options(*required_options)
25
+ missing = false
26
+ required_options.each do |opt|
27
+ unless $options[opt] && $options[opt].length > 0
28
+ missing = true
29
+ $stderr.puts "missing required parameter: #{opt.to_s}; use --help for more information"
30
+ end
31
+ end
32
+ exit(1) if missing
33
+ end
34
+
35
+ COMMANDS = {
36
+ login: -> {
37
+ get_options do |opts|
38
+ opts.banner = "login: Log in to motivosity"
39
+ opts.on("-uUSERNAME", "--username=USERNAME", "Username (email address)") do |username|
40
+ $options[:username] = username
41
+ end
42
+ opts.on("-pPASSWORD", "--password=PASSWORD", "Password") do |password|
43
+ $options[:password] = password
44
+ end
45
+ end
46
+ require_options(:username, :password)
47
+ $client.login!($options[:username], $options[:password])
48
+ nil
49
+ },
50
+
51
+ logout: -> {
52
+ get_options do |opts|
53
+ opts.banner = "logout: Log out of motivosity"
54
+ end
55
+ $client.logout!
56
+ nil
57
+ },
58
+
59
+ get_balance: -> {
60
+ get_options do |opts|
61
+ opts.banner = "get_balance: Check balances"
62
+ end
63
+ response = $client.get_balance
64
+ puts "You can give #{money_str(response['cashGiving'])}"
65
+ puts "You can spend #{money_str(response['cashReceiving'])}"
66
+ nil
67
+ },
68
+
69
+ find_users: -> {
70
+ get_options do |opts|
71
+ opts.banner = "find_users: Search for users by name"
72
+ opts.on("-sTERM", "--search=TERM", "Search term (all or part of user's name)") do |search|
73
+ $options[:search] = search
74
+ end
75
+ opts.on("-i", "--[no-]ignore_self", "Exclude yourself from the search results") do |is|
76
+ $options[:ignore_self] = is
77
+ end
78
+ end
79
+ require_options(:search)
80
+
81
+ result = $client.search_for_user($options[:search], !!$options[:ignore_self])
82
+ result.each do |name|
83
+ puts "#{name["id"]} #{name["fullName"]}"
84
+ end
85
+ nil
86
+ },
87
+
88
+ send_appreciation: -> {
89
+ get_options do |opts|
90
+ opts.banner = "send_appreciation: Send appreciation (and optionally a cash bonus) to another user"
91
+ opts.on("-uUSER", "--user=USER", "Name or ID of user to send thanks to") do |user|
92
+ $options[:user] = user
93
+ end
94
+
95
+ opts.on("-aAMOUNT", "--amount=AMOUNT", "Amount to send") do |amount|
96
+ $options[:amount] = amount
97
+ end
98
+ opts.on("-nNOTE", "--note=NOTE", "Attached note") do |note|
99
+ $options[:note] = note
100
+ end
101
+ opts.on("-vVALUE", "--value=VALUE", "Company value") do |value|
102
+ $options[:value] = value
103
+ end
104
+ opts.on("-p", "--[no-]private", "Send private thanks") do |private|
105
+ $options[:private] = private
106
+ end
107
+ end
108
+ require_options(:user)
109
+
110
+ # find value, if given
111
+ value_id = nil
112
+ if $options[:value]
113
+ values = $client.get_values
114
+ value = values.detect { |value| value['name'] == $options[:value] }
115
+ if value
116
+ value_id = value['id']
117
+ else
118
+ puts "Warning: No company value matches '#{$options[:value]}'"
119
+ end
120
+ end
121
+
122
+ # find user
123
+ user_id = nil
124
+ if $options[:user] =~ USER_ID_REGEX
125
+ user_id = $options[:user]
126
+ else
127
+ users = $client.search_for_user($options[:user])
128
+ if users.empty?
129
+ puts "User '#{$options[:user]}' not found"
130
+ exit(1)
131
+ elsif users.size > 1
132
+ # check for an exact match among the search results
133
+ matching_users = users.select { |user| user['fullName'] == $options[:user] }
134
+ if matching_users.size > 1
135
+ puts "Multiple users match '#{$options[:user]}'; please be more specific."
136
+ puts "Try one of #{users.map{|user| "'#{user['fullName']}'"}.join(', ')}"
137
+ exit(1)
138
+ else
139
+ users = matching_users
140
+ end
141
+ end
142
+ user_id = users[0]['id']
143
+ end
144
+
145
+ response = $client.send_appreciation! user_id, $options[:amount] || 0, $options[:note] || "", value_id, !!$options[:private]
146
+ puts response['growl']['title'] + " " + response['growl']['content']
147
+ }
148
+ }
149
+
150
+ def exit_with_help_message
151
+ $stderr.puts "available commands: #{COMMANDS.keys.map(&:to_s).join(' ')} "
152
+ $stderr.puts "use mvclient <command> --help for more information"
153
+ exit(1)
154
+ end
155
+
156
+ exit_with_help_message if ARGV.empty?
157
+ command = COMMANDS[ARGV.shift.to_sym]
158
+ exit_with_help_message unless command
159
+
160
+ $client = Motivosity::Client.new
161
+ begin
162
+ command.call
163
+ rescue Motivosity::Error => e
164
+ puts e.message
165
+ exit(e.response.code)
166
+ end
@@ -0,0 +1 @@
1
+ require_relative 'mvclient/client'
@@ -0,0 +1,61 @@
1
+ require 'json'
2
+ require 'httparty'
3
+ require 'fileutils'
4
+ require 'http-cookie'
5
+ require_relative 'error'
6
+
7
+ module Motivosity
8
+ class Auth
9
+ include HTTParty
10
+ base_uri 'https://www.motivosity.com'
11
+ follow_redirects false
12
+ debug_output $stderr if ENV['MOTIVOSITY_DEBUG'].to_i == 1
13
+
14
+ def initialize
15
+ @cookies = HTTP::CookieJar.new(store: :mozilla, filename: File.expand_path("~/.motivosity-session"))
16
+ end
17
+
18
+ def login!(username, password)
19
+ @cookies.clear
20
+ response = self.class.post('/login.xhtml', {
21
+ body: {
22
+ "loginForm" => 'loginForm',
23
+ "email" => username,
24
+ "j_password" => password,
25
+ "rememberMe" => 'on',
26
+ "signInLink" => 'Sign In',
27
+ "javax.faces.ViewState" => "3465473682371097839:-947621468971335341"
28
+ }
29
+ })
30
+ raise UnauthorizedError.new("invalid username or password") unless response.code == 302
31
+ process_response_headers(response)
32
+ @cookies.cookies.length > 0
33
+ end
34
+
35
+ def logout!
36
+ @cookies.clear
37
+ true
38
+ end
39
+
40
+ def auth_headers
41
+ { "Cookie" => HTTP::Cookie.cookie_value(@cookies.cookies) }
42
+ end
43
+
44
+ def process_response_headers(response)
45
+ split_cookie_headers(response.headers['Set-Cookie']).each do |cookie_string|
46
+ @cookies.parse(cookie_string, "https://www.motivosity.com/") do |cookie|
47
+ cookie.max_age ||= 604800 if cookie.name == 'JSESSIONID' # force the gem to persist the session key (for one week)
48
+ cookie
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # this is only necessary because HTTParty is stupid and it combines Set-Cookie headers into a single
56
+ # comma-separated string which can't be naively split because there are commas in expiration dates
57
+ def split_cookie_headers(stupidly_joined_headers)
58
+ stupidly_joined_headers.split(/(?<!Expires=[A-Z][a-z][a-z]), /)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,125 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require_relative 'auth'
4
+
5
+ module Motivosity
6
+ class Client
7
+ def initialize
8
+ @auth = Auth.new
9
+ end
10
+
11
+ def login!(username, password)
12
+ @auth.login! username, password
13
+ end
14
+
15
+ def logout!
16
+ @auth.logout!
17
+ end
18
+
19
+ # supply a name or part of a name
20
+ # returns a list of matching users
21
+ # [{
22
+ # "id" => "00000000-0000-user-0000-000000000000",
23
+ # "fullName" => "Jane Doe",
24
+ # "avatarUrl" => "user-placeholder.png",
25
+ # }, ...]
26
+ def search_for_user(search_term, ignore_self = true)
27
+ get "/api/v1/usertypeahead", name: search_term, ignoreSelf: ignore_self
28
+ end
29
+
30
+ # returns a list of Values
31
+ # [{
32
+ # "id" : "39602196-7348-cval-aa03-4f8ef9ce45b8",
33
+ # "name": "Customer Experience",
34
+ # "description": "We aspire to create an awesome customer experience in every interaction with our product and people.",
35
+ # ...}, ...]
36
+ def get_values
37
+ get "/api/v1/companyvalue"
38
+ end
39
+
40
+ # returns balances
41
+ # {
42
+ # "cashReceiving" : 39, # money received
43
+ # "cashGiving" : 10 # money available to give
44
+ # }
45
+ def get_balance
46
+ get "/api/v1/usercash"
47
+ end
48
+
49
+ # sends appreciation to another User
50
+ # raises BalanceError if insufficient funds exist
51
+ def send_appreciation!(toUser_id, amount, note, company_value_id = nil, private = false)
52
+ options = {}
53
+ options["companyValueID"] = company_value_id if company_value_id
54
+ options["amount"] = amount.to_s
55
+ options["note"] = note
56
+ options["privateAppreciation"] = private
57
+ options["toUserID"] = toUser_id
58
+ put "/api/v1/user/#{toUser_id}/appreciation", {}, options
59
+ end
60
+
61
+ # returns recent announcements
62
+ def get_announcements(page = 0)
63
+ get "/api/v1/announcement", pageNo: page
64
+ end
65
+
66
+ # returns feed
67
+ # scope is one of :team, :extended_team, :department, or :company
68
+ def feed(scope = :team, page = 0, comment = true)
69
+ scope_param = case scope
70
+ when :team
71
+ "TEAM"
72
+ when :extended_team
73
+ "EXTM"
74
+ when :department
75
+ "DEPT"
76
+ when :company
77
+ "CMPY"
78
+ else
79
+ scope.to_s
80
+ end
81
+ get "/api/v1/feed", scope: scope_param, page: page, comment: comment
82
+ end
83
+
84
+ private
85
+ class Request
86
+ include HTTParty
87
+ base_uri 'https://www.motivosity.com'
88
+ follow_redirects false
89
+ debug_output $stderr if ENV['MOTIVOSITY_DEBUG'].to_i == 1
90
+ end
91
+
92
+ def request_options(path, url_params = {}, body = nil)
93
+ { headers: @auth.auth_headers.merge({'Content-Type' => 'application/json'}), query: url_params, body: body }
94
+ end
95
+
96
+ def get(path, url_params = {})
97
+ process_response(Request.get(path, request_options(path, url_params)))
98
+ end
99
+
100
+ def put(path, url_params = {}, form_data = {})
101
+ process_response(Request.put(path, request_options(path, url_params, form_data.to_json)))
102
+ end
103
+
104
+ def process_response(response)
105
+ @auth.process_response_headers(response) if response.headers['Set-Cookie']
106
+ response_body = JSON.parse(response.body)
107
+ if response.code != 200
108
+ error = case response.code
109
+ when 401
110
+ UnauthorizedError.new(response.message)
111
+ else
112
+ if response.code == 500 && response_body["type"] == "UnbalanceCashGivingBalanceException"
113
+ BalanceError.new("Insufficient funds")
114
+ else
115
+ Error.new(response.message)
116
+ end
117
+ end
118
+ error.response = response
119
+ error.response_body = response_body
120
+ raise error
121
+ end
122
+ response_body
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,7 @@
1
+ module Motivosity
2
+ class Error < ::StandardError
3
+ attr_accessor :response, :response_body
4
+ end
5
+ class UnauthorizedError < Error; end
6
+ class BalanceError < Error; end
7
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'mvclient'
3
+ s.version = '0.0.1'
4
+ s.date = '2015-04-08'
5
+ s.summary = "Motivosity API Client"
6
+ s.description = "A minimal Motivosity API v1 wrapper for Ruby, plus a command-line tool"
7
+ s.authors = ["Jeremy Stanley"]
8
+ s.email = 'jstanley0@gmail.com'
9
+ s.files = `git ls-files`.split("\n")
10
+ s.homepage = 'http://github.com/jstanley0/mvclient'
11
+ s.license = 'Apache'
12
+ s.bindir = 'bin'
13
+ s.executables << 'mvclient'
14
+ s.required_ruby_version = '>= 1.9.3'
15
+ s.add_dependency 'httparty', '~> 0'
16
+ s.add_dependency 'http-cookie', '~> 1'
17
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mvclient
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Stanley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http-cookie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ description: A minimal Motivosity API v1 wrapper for Ruby, plus a command-line tool
42
+ email: jstanley0@gmail.com
43
+ executables:
44
+ - mvclient
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - bin/mvclient
53
+ - lib/mvclient.rb
54
+ - lib/mvclient/auth.rb
55
+ - lib/mvclient/client.rb
56
+ - lib/mvclient/error.rb
57
+ - mvclient.gemspec
58
+ homepage: http://github.com/jstanley0/mvclient
59
+ licenses:
60
+ - Apache
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.9.3
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Motivosity API Client
82
+ test_files: []
83
+ has_rdoc: