scaleapi-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/CONTRIBUTING.md +29 -0
  4. data/Gemfile +19 -0
  5. data/Gemfile.lock +121 -0
  6. data/LICENSE.md +25 -0
  7. data/README.md +237 -0
  8. data/Rakefile +52 -0
  9. data/VERSION +1 -0
  10. data/lib/scale.rb +42 -0
  11. data/lib/scale/api.rb +104 -0
  12. data/lib/scale/callbacks/base.rb +25 -0
  13. data/lib/scale/callbacks/task.rb +23 -0
  14. data/lib/scale/endpoints/endpoint.rb +31 -0
  15. data/lib/scale/endpoints/tasks/cancel_task.rb +23 -0
  16. data/lib/scale/endpoints/tasks/create_annotation_task.rb +16 -0
  17. data/lib/scale/endpoints/tasks/create_categorization_task.rb +16 -0
  18. data/lib/scale/endpoints/tasks/create_comparison_task.rb +16 -0
  19. data/lib/scale/endpoints/tasks/create_phonecall_task.rb +16 -0
  20. data/lib/scale/endpoints/tasks/create_transcription_task.rb +16 -0
  21. data/lib/scale/endpoints/tasks/list_tasks.rb +27 -0
  22. data/lib/scale/endpoints/tasks/retrieve_task.rb +23 -0
  23. data/lib/scale/endpoints/tasks/task_endpoint.rb +22 -0
  24. data/lib/scale/generic_error.rb +4 -0
  25. data/lib/scale/http_error.rb +16 -0
  26. data/lib/scale/resources/base.rb +6 -0
  27. data/lib/scale/resources/task.rb +26 -0
  28. data/test/callbacks/test_task_callback.rb +35 -0
  29. data/test/fixtures/callback.json +29 -0
  30. data/test/fixtures/vcr_cassettes/tasks.yml +2629 -0
  31. data/test/helper.rb +42 -0
  32. data/test/tasks/test_cancel_task.rb +21 -0
  33. data/test/tasks/test_create_annotation_task.rb +40 -0
  34. data/test/tasks/test_create_categorization_task.rb +40 -0
  35. data/test/tasks/test_create_comparison_task.rb +43 -0
  36. data/test/tasks/test_create_phonecall_task.rb +41 -0
  37. data/test/tasks/test_create_transcription_task.rb +41 -0
  38. data/test/tasks/test_list_tasks.rb +14 -0
  39. data/test/tasks/test_retrieve_task.rb +37 -0
  40. data/test/test_api.rb +27 -0
  41. metadata +182 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd1682c683487bf57533d8f7e15262377c34a08c
4
+ data.tar.gz: c55fce2c3b0be18af7617511919420c388505e79
5
+ SHA512:
6
+ metadata.gz: 214b011b51c59ee19523f84f10d7d256e580f41e7ee11c87814e8bd2845f80e08446bbafd92387dca29b0b38cef31b5b13f522dc0fc7523dce16a67977b06fb9
7
+ data.tar.gz: 366e928aab8f116be689fe8cd6e14643097c9ed5d76f09e165758cba47a6f2c6ee9649bf6878fd82a9e739edf39cab52fcea80144c413605c1158ede152dc1e5
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,29 @@
1
+ # Contributing
2
+
3
+ 1. Fork this project
4
+ 2. Create your feature branch `git checkout -b my-branch`
5
+ 3. Add yourself as a developer to `README.md`!
6
+ 4. Commit your changes `git commit -am 'Add some feature'`
7
+ 5. Push to the branch `git push origin my-new-feature`
8
+ 6. Create a pull request with the following format:
9
+
10
+ ```markdown
11
+ ## Description
12
+
13
+ This pull request will be used to [fix|implement|...] the following [bugs|features|...].
14
+
15
+ ## Tasks
16
+
17
+ - [ ] Uncompleted task nº1.
18
+ - [x] Completed task nº2.
19
+ - [x] Buy eggs.
20
+ - [ ] Fix that strange bug.
21
+ - [ ] Etc.
22
+
23
+ ## References
24
+
25
+ - [Example reference](http://www.example.com)
26
+ - [Wabit season!](https://www.youtube.com/watch?v=17ocaZb-bGg)
27
+ ```
28
+
29
+ Remember that your help is always appreciated! :smile:
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rest-client'
4
+ gem 'activesupport'
5
+
6
+ group :development do
7
+ gem 'rdoc', '~> 3.12'
8
+ gem 'bundler', '~> 1.0'
9
+ gem 'juwelier', '~> 2.1.0'
10
+ gem 'pry'
11
+ gem 'pry-nav'
12
+ end
13
+
14
+ group :test do
15
+ gem 'test-unit'
16
+ gem 'shoulda', '>= 0'
17
+ gem 'vcr'
18
+ gem 'webmock'
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,121 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activesupport (5.0.0.1)
5
+ concurrent-ruby (~> 1.0, >= 1.0.2)
6
+ i18n (~> 0.7)
7
+ minitest (~> 5.1)
8
+ tzinfo (~> 1.1)
9
+ addressable (2.4.0)
10
+ builder (3.2.2)
11
+ coderay (1.1.1)
12
+ concurrent-ruby (1.0.2)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ descendants_tracker (0.0.4)
16
+ thread_safe (~> 0.3, >= 0.3.1)
17
+ domain_name (0.5.20160826)
18
+ unf (>= 0.0.5, < 1.0.0)
19
+ faraday (0.9.2)
20
+ multipart-post (>= 1.2, < 3)
21
+ git (1.3.0)
22
+ github_api (0.14.5)
23
+ addressable (~> 2.4.0)
24
+ descendants_tracker (~> 0.0.4)
25
+ faraday (~> 0.8, < 0.10)
26
+ hashie (>= 3.4)
27
+ oauth2 (~> 1.0)
28
+ hashdiff (0.3.0)
29
+ hashie (3.4.4)
30
+ highline (1.7.8)
31
+ http-cookie (1.0.2)
32
+ domain_name (~> 0.5)
33
+ i18n (0.7.0)
34
+ json (1.8.3)
35
+ juwelier (2.1.2)
36
+ builder
37
+ bundler (>= 1.0)
38
+ git (>= 1.2.5)
39
+ github_api
40
+ highline (>= 1.6.15)
41
+ nokogiri (>= 1.5.10)
42
+ rake
43
+ rdoc
44
+ semver
45
+ jwt (1.5.4)
46
+ method_source (0.8.2)
47
+ mime-types (3.1)
48
+ mime-types-data (~> 3.2015)
49
+ mime-types-data (3.2016.0521)
50
+ mini_portile2 (2.1.0)
51
+ minitest (5.9.0)
52
+ multi_json (1.12.1)
53
+ multi_xml (0.5.5)
54
+ multipart-post (2.0.0)
55
+ netrc (0.11.0)
56
+ nokogiri (1.6.8)
57
+ mini_portile2 (~> 2.1.0)
58
+ pkg-config (~> 1.1.7)
59
+ oauth2 (1.2.0)
60
+ faraday (>= 0.8, < 0.10)
61
+ jwt (~> 1.0)
62
+ multi_json (~> 1.3)
63
+ multi_xml (~> 0.5)
64
+ rack (>= 1.2, < 3)
65
+ pkg-config (1.1.7)
66
+ power_assert (0.3.0)
67
+ pry (0.10.4)
68
+ coderay (~> 1.1.0)
69
+ method_source (~> 0.8.1)
70
+ slop (~> 3.4)
71
+ pry-nav (0.2.4)
72
+ pry (>= 0.9.10, < 0.11.0)
73
+ rack (1.6.4)
74
+ rake (11.2.2)
75
+ rdoc (3.12.2)
76
+ json (~> 1.4)
77
+ rest-client (2.0.0)
78
+ http-cookie (>= 1.0.2, < 2.0)
79
+ mime-types (>= 1.16, < 4.0)
80
+ netrc (~> 0.8)
81
+ safe_yaml (1.0.4)
82
+ semver (1.0.1)
83
+ shoulda (3.5.0)
84
+ shoulda-context (~> 1.0, >= 1.0.1)
85
+ shoulda-matchers (>= 1.4.1, < 3.0)
86
+ shoulda-context (1.2.1)
87
+ shoulda-matchers (2.8.0)
88
+ activesupport (>= 3.0.0)
89
+ slop (3.6.0)
90
+ test-unit (3.2.1)
91
+ power_assert
92
+ thread_safe (0.3.5)
93
+ tzinfo (1.2.2)
94
+ thread_safe (~> 0.1)
95
+ unf (0.1.4)
96
+ unf_ext
97
+ unf_ext (0.0.7.2)
98
+ vcr (3.0.3)
99
+ webmock (2.1.0)
100
+ addressable (>= 2.3.6)
101
+ crack (>= 0.3.2)
102
+ hashdiff
103
+
104
+ PLATFORMS
105
+ ruby
106
+
107
+ DEPENDENCIES
108
+ activesupport
109
+ bundler (~> 1.0)
110
+ juwelier (~> 2.1.0)
111
+ pry
112
+ pry-nav
113
+ rdoc (~> 3.12)
114
+ rest-client
115
+ shoulda
116
+ test-unit
117
+ vcr
118
+ webmock
119
+
120
+ BUNDLED WITH
121
+ 1.12.5
data/LICENSE.md ADDED
@@ -0,0 +1,25 @@
1
+ The MIT License (MIT)
2
+ =====================
3
+
4
+ Copyright © `2016` `Daniel Herzog`
5
+
6
+ Permission is hereby granted, free of charge, to any person
7
+ obtaining a copy of this software and associated documentation
8
+ files (the “Software”), to deal in the Software without
9
+ restriction, including without limitation the rights to use,
10
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the
12
+ Software is furnished to do so, subject to the following
13
+ conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,237 @@
1
+ # ScaleAPI for Ruby
2
+
3
+ A simple ruby wrapper for the [Scale](http://www.scaleapi.com) HTTP API. Documentation for this API is available [here](https://docs.scaleapi.com/).
4
+
5
+ This project uses [`juwelier`](https://github.com/flajann2/juwelier) for managing and releasing this gem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'scaleapi-ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```sh
18
+ $ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```sh
24
+ $ gem install scaleapi-ruby
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ First, you need to initialize the Scale client:
30
+
31
+ ```ruby
32
+ require 'scale'
33
+ scale = Scale.setup api_key: 'YOUR_KEY_GOES_HERE', # Required
34
+ callback_key: 'YOUR CALLBACK_KEY_GOES_HERE' # Optional
35
+ ```
36
+
37
+ Check [this]() for further information.
38
+
39
+ ### Tasks
40
+
41
+ Most of these methods will return a `Scale::Resources::Task` object, which will contain information
42
+ about the json response (task_id, status...).
43
+
44
+ Any parameter available in the [documentation](https://docs.scaleapi.com) can be passed as an argument
45
+ option with the corresponding type.
46
+
47
+ The following endpoints for tasks are available:
48
+
49
+ #### Create categorization task
50
+
51
+ Check [this](https://docs.scaleapi.com/#create-categorization-task) for further information.
52
+
53
+ ```ruby
54
+ task = scale.create_categorization_task(
55
+ callback_url: 'http://www.example.com/callback',
56
+ instruction: 'Is this company public or private?',
57
+ attachment_type: 'website',
58
+ attachment: 'http://www.google.com/',
59
+ categories: ['public', 'private']
60
+ )
61
+ ```
62
+
63
+ #### Create transcription task
64
+
65
+ Check [this](https://docs.scaleapi.com/#create-transcription-task) for further information.
66
+
67
+ ```ruby
68
+ task = scale.create_transcription_task(
69
+ callback_url: 'http://www.example.com/callback',
70
+ instruction: 'Transcribe the given fields. Then for each news item on the page, transcribe the information for the row.',
71
+ attachment_type: 'website',
72
+ attachment: 'http://www.google.com/',
73
+ fields: { title: 'Title of Webpage', top_result: 'Title of the top result' },
74
+ row_fields: { username: 'Username of submitter', comment_count: 'Number of comments' }
75
+ )
76
+ ```
77
+
78
+ #### Create phone call task
79
+
80
+ Check [this](https://docs.scaleapi.com/#create-phone-call-task) for further information.
81
+
82
+ ```ruby
83
+ scale.create_phonecall_task(
84
+ callback_url: 'http://www.example.com/callback',
85
+ instruction: "Call this person and tell me his email address. Ask if he's happy too.",
86
+ phone_number: '5055006865',
87
+ entity_name: 'Alexandr Wang',
88
+ fields: { email: 'Email Address' },
89
+ choices: ['He is happy', 'He is not happy']
90
+ )
91
+ ```
92
+
93
+ #### Create comparison task
94
+
95
+ Check [this](https://docs.scaleapi.com/#create-comparison-task) for further information.
96
+
97
+ ```ruby
98
+ scale.create_comparison_task(
99
+ callback_url: 'http://www.example.com/callback',
100
+ instruction: 'Do the objects in these images have the same pattern?',
101
+ attachment_type: 'image',
102
+ choices: ['yes', 'no'],
103
+ attachments: [
104
+ 'http://i.ebayimg.com/00/$T2eC16dHJGwFFZKjy5ZjBRfNyMC4Ig~~_32.JPG',
105
+ 'http://images.wisegeek.com/checkered-tablecloth.jpg'
106
+ ]
107
+ )
108
+ ```
109
+
110
+ #### Create annotation task
111
+
112
+ Check [this](https://docs.scaleapi.com/#create-annotation-task-bounding-box) for further information.
113
+
114
+ ```ruby
115
+ scale.create_comparison_task(
116
+ callback_url: 'http://www.example.com/callback',
117
+ instruction: 'Draw a box around each baby cow and big cow.',
118
+ attachment_type: "image",
119
+ attachment: "http://i.imgur.com/v4cBreD.jpg",
120
+ objects_to_annotate: ["baby cow", "big cow"]
121
+ )
122
+ ```
123
+
124
+ #### Retrieve task
125
+
126
+ Check [this](https://docs.scaleapi.com/#retrieve-a-task) for further information.
127
+
128
+ Retrieve a task given its id.
129
+
130
+ ```ruby
131
+ task = scale.retrieve_task 'asdfasdfasdfasdfasdfasdf'
132
+ task.id == 'asdfasdfasdfasdfasdfasdf' # true
133
+ ```
134
+
135
+ #### Cancel task
136
+
137
+ Check [this](https://docs.scaleapi.com/#cancel-a-task) for further information.
138
+
139
+ Cancel a task given its id, only if it's not completed.
140
+
141
+ ```ruby
142
+ task = scale.cancel_task 'asdfasdfasdfasdfasdfasdf'
143
+ ```
144
+
145
+ #### List tasks
146
+
147
+ Check [this](https://docs.scaleapi.com/#list-all-tasks) for further information.
148
+
149
+ Retrieve a list (`Array`) of all tasks.
150
+
151
+ ```ruby
152
+ tasks = scale.tasks
153
+ tasks.all? { |t| t.is_a? Scale::Resources::Task } # true
154
+ ```
155
+
156
+ ### Callbacks
157
+
158
+ This gem allows you to create and parse callback data, so it can be easily used for web applications:
159
+
160
+ For example, for Ruby on Rails:
161
+
162
+ ```ruby
163
+ # app/controllers/scale_api_controller.rb
164
+ class ScaleApiController < ApplicationController
165
+ # POST /scale_api
166
+ def create
167
+ callback = scale.build_callback params, 'task', callback_key: request.headers['scale-callback-auth']
168
+ callback.response # Response content hash (code and result)
169
+ callback.task # Scale::Resources::Task object
170
+ end
171
+ end
172
+ ```
173
+
174
+ Please note that callback validation is optional; omit it if no `callback_key` was passed to `Scale.build`
175
+ builder.
176
+
177
+ If the validation is enabled, the `build_callback` method will raise a `Scale::GenericError`, explaning that
178
+ the tokens don't match. You can also use `valid_callback_key?` to test it:
179
+
180
+ ```rb
181
+ # `scale.callback_key` is 'TEST'
182
+ scale.valid_callback_key? 'FAKE' # false, because 'TEST' != 'FAKE'
183
+ ```
184
+
185
+ ## Error handling
186
+
187
+ If something went wrong while making API calls, then exceptions will be raised automatically
188
+ as a `Scale::GenericError` (or `Scale::HttpError`) runtime error. For example:
189
+
190
+ ```ruby
191
+ begin
192
+ scale.create_categorization_task instructions: 'Some parameters are missing.'
193
+ rescue Scale::HttpError => e
194
+ puts e.code # 400
195
+ puts e.exception # Missing parameter X
196
+ pust e.original_exception # Catched exception
197
+ rescue Scale::GenericError => e
198
+ puts e.message # Missing parameter X
199
+ end
200
+ ```
201
+
202
+ ## Custom options
203
+
204
+ The api initialization accepts the following options:
205
+
206
+ | Name | Description | Default |
207
+ | ---- | ----------- | ------- |
208
+ | `endpoint` | Endpoint used in the http requests. | https://api.scaleapi.com/v1/ |
209
+ | `api_key` | API key used in the http requests. | |
210
+ | `callback_key` | API key used to validate callback POST requests. | |
211
+ | `default_request_params` | Default parameters (payload) for the API requests | |
212
+
213
+ For example, `default_request_params` can be used to always set the same `callback_url` value:
214
+
215
+ ```rb
216
+ scale = Scale.setup api_key: 'YOUR_KEY_GOES_HERE', # Required
217
+ callback_key: 'YOUR CALLBACK_KEY_GOES_HERE' # Optional
218
+ default_request_params: {
219
+ callback_url: 'http://www.example.com/callback'
220
+ }
221
+
222
+ # All callback requests will posted be made to http://www.example.com/callback
223
+ scale.create_comparison_task(
224
+ instruction: 'Draw a box around each baby cow and big cow.',
225
+ attachment_type: "image",
226
+ attachment: "http://i.imgur.com/v4cBreD.jpg",
227
+ objects_to_annotate: ["baby cow", "big cow"]
228
+ )
229
+ ```
230
+
231
+ ## Authors
232
+
233
+ This project has been developed by:
234
+
235
+ | Avatar | Name | Nickname | Email |
236
+ | ------- | ------------- | --------- | ------------------ |
237
+ | ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [wikiti.doghound@gmail.com](mailto:wikiti.doghound@gmail.com) |