wor-requests 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +326 -0
- data/.rubocop.yml +12 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.md +21 -0
- data/README.md +102 -0
- data/Rakefile +6 -0
- data/lib/generators/templates/base_service.rb.erb +17 -0
- data/lib/generators/templates/wor_requests.rb +3 -0
- data/lib/generators/wor/requests/install_generator.rb +14 -0
- data/lib/generators/wor/requests/service_generator.rb +14 -0
- data/lib/wor/requests.rb +40 -0
- data/lib/wor/requests/base.rb +160 -0
- data/lib/wor/requests/invalid_options_error.rb +13 -0
- data/lib/wor/requests/request_error.rb +11 -0
- data/lib/wor/requests/version.rb +5 -0
- data/wor-requests.gemspec +37 -0
- metadata +240 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5ed50703665ba94d25a38f3c8ef8a724ca7d4a0d
|
4
|
+
data.tar.gz: 2b894c7322b3ab4e87735bcb9579704a90693512
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b25426b8bc4ad40adbcbe342bb82cc174e7436b45622b418f4eb2e2e4100e379b01f836152f3f6eb9b534018d1dfd2233ccc32b18b3d818d432dca75e536893b
|
7
|
+
data.tar.gz: aa81d67f150c70e1ffd44d56888915ef2868dcaf199bff36b5b207e9b7862a2323d3b81f25f8924bca48b78861cecbafae3d6b632dc893fa3a33ec014af4c881
|
data/.gitignore
ADDED
@@ -0,0 +1,326 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.gem
|
11
|
+
|
12
|
+
# Created by https://www.gitignore.io/api/ruby,rubymine,rails,emacs,vim,sublimetext,osx,macos,linux,windows
|
13
|
+
|
14
|
+
### Emacs ###
|
15
|
+
# -*- mode: gitignore; -*-
|
16
|
+
*~
|
17
|
+
\#*\#
|
18
|
+
/.emacs.desktop
|
19
|
+
/.emacs.desktop.lock
|
20
|
+
*.elc
|
21
|
+
auto-save-list
|
22
|
+
tramp
|
23
|
+
.\#*
|
24
|
+
|
25
|
+
# Org-mode
|
26
|
+
.org-id-locations
|
27
|
+
*_archive
|
28
|
+
|
29
|
+
# flymake-mode
|
30
|
+
*_flymake.*
|
31
|
+
|
32
|
+
# eshell files
|
33
|
+
/eshell/history
|
34
|
+
/eshell/lastdir
|
35
|
+
|
36
|
+
# elpa packages
|
37
|
+
/elpa/
|
38
|
+
|
39
|
+
# reftex files
|
40
|
+
*.rel
|
41
|
+
|
42
|
+
# AUCTeX auto folder
|
43
|
+
/auto/
|
44
|
+
|
45
|
+
# cask packages
|
46
|
+
.cask/
|
47
|
+
dist/
|
48
|
+
|
49
|
+
# Flycheck
|
50
|
+
flycheck_*.el
|
51
|
+
|
52
|
+
# server auth directory
|
53
|
+
/server/
|
54
|
+
|
55
|
+
# projectiles files
|
56
|
+
.projectile
|
57
|
+
|
58
|
+
# directory configuration
|
59
|
+
.dir-locals.el
|
60
|
+
|
61
|
+
### Linux ###
|
62
|
+
|
63
|
+
# temporary files which can be created if a process still has a handle open of a deleted file
|
64
|
+
.fuse_hidden*
|
65
|
+
|
66
|
+
# KDE directory preferences
|
67
|
+
.directory
|
68
|
+
|
69
|
+
# Linux trash folder which might appear on any partition or disk
|
70
|
+
.Trash-*
|
71
|
+
|
72
|
+
# .nfs files are created when an open file is removed but is still being accessed
|
73
|
+
.nfs*
|
74
|
+
|
75
|
+
### macOS ###
|
76
|
+
*.DS_Store
|
77
|
+
.AppleDouble
|
78
|
+
.LSOverride
|
79
|
+
|
80
|
+
# Icon must end with two \r
|
81
|
+
Icon
|
82
|
+
|
83
|
+
|
84
|
+
# Thumbnails
|
85
|
+
._*
|
86
|
+
|
87
|
+
# Files that might appear in the root of a volume
|
88
|
+
.DocumentRevisions-V100
|
89
|
+
.fseventsd
|
90
|
+
.Spotlight-V100
|
91
|
+
.TemporaryItems
|
92
|
+
.Trashes
|
93
|
+
.VolumeIcon.icns
|
94
|
+
.com.apple.timemachine.donotpresent
|
95
|
+
|
96
|
+
# Directories potentially created on remote AFP share
|
97
|
+
.AppleDB
|
98
|
+
.AppleDesktop
|
99
|
+
Network Trash Folder
|
100
|
+
Temporary Items
|
101
|
+
.apdisk
|
102
|
+
|
103
|
+
### OSX ###
|
104
|
+
|
105
|
+
# Icon must end with two \r
|
106
|
+
|
107
|
+
|
108
|
+
# Thumbnails
|
109
|
+
|
110
|
+
# Files that might appear in the root of a volume
|
111
|
+
|
112
|
+
# Directories potentially created on remote AFP share
|
113
|
+
|
114
|
+
### Rails ###
|
115
|
+
*.rbc
|
116
|
+
capybara-*.html
|
117
|
+
.rspec
|
118
|
+
/log
|
119
|
+
/tmp
|
120
|
+
/db/*.sqlite3
|
121
|
+
/db/*.sqlite3-journal
|
122
|
+
/public/system
|
123
|
+
/coverage/
|
124
|
+
/spec/tmp
|
125
|
+
**.orig
|
126
|
+
rerun.txt
|
127
|
+
pickle-email-*.html
|
128
|
+
|
129
|
+
# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
|
130
|
+
config/initializers/secret_token.rb
|
131
|
+
|
132
|
+
# Only include if you have production secrets in this file, which is no longer a Rails default
|
133
|
+
# config/secrets.yml
|
134
|
+
|
135
|
+
# dotenv
|
136
|
+
# TODO Comment out this rule if environment variables can be committed
|
137
|
+
.env
|
138
|
+
|
139
|
+
## Environment normalization:
|
140
|
+
/.bundle
|
141
|
+
/vendor/bundle
|
142
|
+
|
143
|
+
# these should all be checked in to normalize the environment:
|
144
|
+
# Gemfile.lock, .ruby-version, .ruby-gemset
|
145
|
+
|
146
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
147
|
+
.rvmrc
|
148
|
+
|
149
|
+
# if using bower-rails ignore default bower_components path bower.json files
|
150
|
+
/vendor/assets/bower_components
|
151
|
+
*.bowerrc
|
152
|
+
bower.json
|
153
|
+
|
154
|
+
# Ignore pow environment settings
|
155
|
+
.powenv
|
156
|
+
|
157
|
+
# Ignore Byebug command history file.
|
158
|
+
.byebug_history
|
159
|
+
|
160
|
+
### Ruby ###
|
161
|
+
*.gem
|
162
|
+
/.config
|
163
|
+
/InstalledFiles
|
164
|
+
/pkg/
|
165
|
+
/spec/reports/
|
166
|
+
/spec/examples.txt
|
167
|
+
/test/tmp/
|
168
|
+
/test/version_tmp/
|
169
|
+
/tmp/
|
170
|
+
|
171
|
+
# Used by dotenv library to load environment variables.
|
172
|
+
# .env
|
173
|
+
|
174
|
+
## Specific to RubyMotion:
|
175
|
+
.dat*
|
176
|
+
.repl_history
|
177
|
+
build/
|
178
|
+
*.bridgesupport
|
179
|
+
build-iPhoneOS/
|
180
|
+
build-iPhoneSimulator/
|
181
|
+
|
182
|
+
## Specific to RubyMotion (use of CocoaPods):
|
183
|
+
#
|
184
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
185
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
186
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
187
|
+
#
|
188
|
+
# vendor/Pods/
|
189
|
+
|
190
|
+
## Documentation cache and generated files:
|
191
|
+
/.yardoc/
|
192
|
+
/_yardoc/
|
193
|
+
/doc/
|
194
|
+
/rdoc/
|
195
|
+
|
196
|
+
## Environment normalization:
|
197
|
+
/.bundle/
|
198
|
+
/lib/bundler/man/
|
199
|
+
|
200
|
+
# for a library or gem, you might want to ignore these files since the code is
|
201
|
+
# intended to run in multiple environments; otherwise, check them in:
|
202
|
+
# Gemfile.lock
|
203
|
+
# .ruby-version
|
204
|
+
# .ruby-gemset
|
205
|
+
|
206
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
207
|
+
|
208
|
+
### RubyMine ###
|
209
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
210
|
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
211
|
+
|
212
|
+
# User-specific stuff:
|
213
|
+
.idea/**/workspace.xml
|
214
|
+
.idea/**/tasks.xml
|
215
|
+
|
216
|
+
# Sensitive or high-churn files:
|
217
|
+
.idea/**/dataSources/
|
218
|
+
.idea/**/dataSources.ids
|
219
|
+
.idea/**/dataSources.xml
|
220
|
+
.idea/**/dataSources.local.xml
|
221
|
+
.idea/**/sqlDataSources.xml
|
222
|
+
.idea/**/dynamic.xml
|
223
|
+
.idea/**/uiDesigner.xml
|
224
|
+
|
225
|
+
# Gradle:
|
226
|
+
.idea/**/gradle.xml
|
227
|
+
.idea/**/libraries
|
228
|
+
|
229
|
+
# Mongo Explorer plugin:
|
230
|
+
.idea/**/mongoSettings.xml
|
231
|
+
|
232
|
+
## File-based project format:
|
233
|
+
*.iws
|
234
|
+
|
235
|
+
## Plugin-specific files:
|
236
|
+
|
237
|
+
# IntelliJ
|
238
|
+
/out/
|
239
|
+
|
240
|
+
# mpeltonen/sbt-idea plugin
|
241
|
+
.idea_modules/
|
242
|
+
|
243
|
+
# JIRA plugin
|
244
|
+
atlassian-ide-plugin.xml
|
245
|
+
|
246
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
247
|
+
com_crashlytics_export_strings.xml
|
248
|
+
crashlytics.properties
|
249
|
+
crashlytics-build.properties
|
250
|
+
fabric.properties
|
251
|
+
|
252
|
+
### RubyMine Patch ###
|
253
|
+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
254
|
+
|
255
|
+
# *.iml
|
256
|
+
# modules.xml
|
257
|
+
# .idea/misc.xml
|
258
|
+
# *.ipr
|
259
|
+
|
260
|
+
### SublimeText ###
|
261
|
+
# cache files for sublime text
|
262
|
+
*.tmlanguage.cache
|
263
|
+
*.tmPreferences.cache
|
264
|
+
*.stTheme.cache
|
265
|
+
|
266
|
+
# workspace files are user-specific
|
267
|
+
*.sublime-workspace
|
268
|
+
|
269
|
+
# project files should be checked into the repository, unless a significant
|
270
|
+
# proportion of contributors will probably not be using SublimeText
|
271
|
+
# *.sublime-project
|
272
|
+
|
273
|
+
# sftp configuration file
|
274
|
+
sftp-config.json
|
275
|
+
|
276
|
+
# Package control specific files
|
277
|
+
Package Control.last-run
|
278
|
+
Package Control.ca-list
|
279
|
+
Package Control.ca-bundle
|
280
|
+
Package Control.system-ca-bundle
|
281
|
+
Package Control.cache/
|
282
|
+
Package Control.ca-certs/
|
283
|
+
Package Control.merged-ca-bundle
|
284
|
+
Package Control.user-ca-bundle
|
285
|
+
oscrypto-ca-bundle.crt
|
286
|
+
bh_unicode_properties.cache
|
287
|
+
|
288
|
+
# Sublime-github package stores a github token in this file
|
289
|
+
# https://packagecontrol.io/packages/sublime-github
|
290
|
+
GitHub.sublime-settings
|
291
|
+
|
292
|
+
### Vim ###
|
293
|
+
# swap
|
294
|
+
[._]*.s[a-v][a-z]
|
295
|
+
[._]*.sw[a-p]
|
296
|
+
[._]s[a-v][a-z]
|
297
|
+
[._]sw[a-p]
|
298
|
+
# session
|
299
|
+
Session.vim
|
300
|
+
# temporary
|
301
|
+
.netrwhist
|
302
|
+
# auto-generated tag files
|
303
|
+
tags
|
304
|
+
|
305
|
+
### Windows ###
|
306
|
+
# Windows thumbnail cache files
|
307
|
+
Thumbs.db
|
308
|
+
ehthumbs.db
|
309
|
+
ehthumbs_vista.db
|
310
|
+
|
311
|
+
# Folder config file
|
312
|
+
Desktop.ini
|
313
|
+
|
314
|
+
# Recycle Bin used on file shares
|
315
|
+
$RECYCLE.BIN/
|
316
|
+
|
317
|
+
# Windows Installer files
|
318
|
+
*.cab
|
319
|
+
*.msi
|
320
|
+
*.msm
|
321
|
+
*.msp
|
322
|
+
|
323
|
+
# Windows shortcuts
|
324
|
+
*.lnk
|
325
|
+
|
326
|
+
# End of https://www.gitignore.io/api/ruby,rubymine,rails,emacs,vim,sublimetext,osx,macos,linux,windows
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Wolox
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Wolox on Rails - Requests
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/wor-requests.svg)](https://badge.fury.io/rb/wor-requests)
|
3
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/Wolox/wor-requests.svg)](https://gemnasium.com/github.com/Wolox/wor-requests)
|
4
|
+
[![Build Status](https://travis-ci.org/Wolox/wor-requests.svg)](https://travis-ci.org/Wolox/wor-requests)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/Wolox/wor-requests/badges/gpa.svg)](https://codeclimate.com/github/Wolox/wor-requests)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/Wolox/wor-requests/badges/coverage.svg)](https://codeclimate.com/github/Wolox/wor-requests/coverage)
|
7
|
+
|
8
|
+
Make external requests in you service objects, with easy logging and error handling!
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'wor-requests'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install wor-requests
|
25
|
+
|
26
|
+
Then you can run `rails generate wor:requests:install` to create the initializer:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
# config/initializers/wor_requests.rb
|
30
|
+
|
31
|
+
Wor::Requests.configure do |config|
|
32
|
+
config.logger = Rails.logger
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
To write your first Service using Wor-requests you can write something like this:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'wor/requests'
|
42
|
+
|
43
|
+
class GithubService < Wor::Requests::Base
|
44
|
+
def repositories(username)
|
45
|
+
get(
|
46
|
+
attempting_to: "get repositories of #{username}",
|
47
|
+
path: "/users/#{username}/repos"
|
48
|
+
)
|
49
|
+
rescue Wor::Requests::RequestError => e
|
50
|
+
puts e.message
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
def base_url
|
56
|
+
'https://api.github.com'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
puts GithubService.new.repositories('alebian')
|
61
|
+
```
|
62
|
+
|
63
|
+
Or, even easier, run `rails generate wor:requests:service NAME` in your Rails root
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
1. Fork it
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
69
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
70
|
+
4. Run rubocop lint (`rubocop -R --format simple`)
|
71
|
+
5. Run rspec tests (`bundle exec rspec`)
|
72
|
+
6. Push your branch (`git push origin my-new-feature`)
|
73
|
+
7. Create a new Pull Request
|
74
|
+
|
75
|
+
## About ##
|
76
|
+
|
77
|
+
This project is maintained by [Diego Raffo](https://github.com/enanodr) along with [Michel Agopian](https://github.com/mishuagopian) and it was written by [Wolox](http://www.wolox.com.ar).
|
78
|
+
![Wolox](https://raw.githubusercontent.com/Wolox/press-kit/master/logos/logo_banner.png)
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
**wor-requests** is available under the MIT [license](https://raw.githubusercontent.com/Wolox/wor-requests/master/LICENSE.md).
|
83
|
+
|
84
|
+
Copyright (c) 2017 Wolox
|
85
|
+
|
86
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
87
|
+
of this software and associated documentation files (the "Software"), to deal
|
88
|
+
in the Software without restriction, including without limitation the rights
|
89
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
90
|
+
copies of the Software, and to permit persons to whom the Software is
|
91
|
+
furnished to do so, subject to the following conditions:
|
92
|
+
|
93
|
+
The above copyright notice and this permission notice shall be included in
|
94
|
+
all copies or substantial portions of the Software.
|
95
|
+
|
96
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
97
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
98
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
99
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
100
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
101
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
102
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= class_name %>Service < Wor::Requests::Base
|
2
|
+
# An example of how to get all repositories for the specified user in github.com
|
3
|
+
# def repositories(username)
|
4
|
+
# get(
|
5
|
+
# attempting_to: "get repositories of #{username}",
|
6
|
+
# path: "/users/#{username}/repos"
|
7
|
+
# )
|
8
|
+
# rescue Wor::Requests::RequestError => e
|
9
|
+
# puts e.message
|
10
|
+
# end
|
11
|
+
|
12
|
+
# protected
|
13
|
+
|
14
|
+
# def base_url
|
15
|
+
# 'https://api.github.com'
|
16
|
+
# end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Wor
|
2
|
+
module Requests
|
3
|
+
module Generators
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../../../templates', __FILE__)
|
6
|
+
desc 'Creates Wor-requests initializer for your application'
|
7
|
+
|
8
|
+
def copy_initializer
|
9
|
+
template 'wor_requests.rb', 'config/initializers/wor_requests.rb'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Wor
|
2
|
+
module Requests
|
3
|
+
module Generators
|
4
|
+
class ServiceGenerator < Rails::Generators::NamedBase
|
5
|
+
source_root File.expand_path('../../../templates', __FILE__)
|
6
|
+
desc 'Creates Wor-requests service for your application'
|
7
|
+
|
8
|
+
def create_service
|
9
|
+
template 'base_service.rb.erb', "app/services/#{plural_name}_service.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/wor/requests.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'requests/base'
|
2
|
+
require_relative 'requests/request_error'
|
3
|
+
require_relative 'requests/version'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module Wor
|
7
|
+
module Requests
|
8
|
+
VALID_RESPONSE_TYPES = [:json].freeze
|
9
|
+
|
10
|
+
@config = {
|
11
|
+
logger: Logger.new(STDOUT),
|
12
|
+
default_response_type: :json
|
13
|
+
}
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield self
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.logger=(logger)
|
20
|
+
@config[:logger] = logger
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.default_response_type=(type)
|
24
|
+
return unless VALID_RESPONSE_TYPES.include?(type)
|
25
|
+
@config[:default_response_type] = type
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.config
|
29
|
+
@config
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.logger
|
33
|
+
@config[:logger]
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.default_response_type
|
37
|
+
@config[:default_response_type]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httparty'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
# rubocop:disable ClassLength
|
6
|
+
module Wor
|
7
|
+
module Requests
|
8
|
+
class Base
|
9
|
+
VALID_HTTP_VERBS = %i(get post patch put delete).freeze
|
10
|
+
|
11
|
+
# According to RFC 7231
|
12
|
+
COMMON_ATTRIBUTES = %i(path headers attempting_to response_type).freeze
|
13
|
+
HAS_QUERY = [:query].freeze
|
14
|
+
HAS_BODY = [:body].freeze
|
15
|
+
HTTP_COMPLETE = (COMMON_ATTRIBUTES + HAS_QUERY + HAS_BODY).freeze
|
16
|
+
HTTP_QUERY_ONLY = (COMMON_ATTRIBUTES + HAS_QUERY).freeze
|
17
|
+
GET_ATTRIBUTES = HTTP_QUERY_ONLY
|
18
|
+
POST_ATTRIBUTES = HTTP_COMPLETE
|
19
|
+
PATCH_ATTRIBUTES = HTTP_COMPLETE
|
20
|
+
PUT_ATTRIBUTES = HTTP_COMPLETE
|
21
|
+
DELETE_ATTRIBUTES = HTTP_QUERY_ONLY
|
22
|
+
|
23
|
+
# Define the methods:
|
24
|
+
# - get(opts = {}, &block)
|
25
|
+
# - post(opts = {}, &block)
|
26
|
+
# - patch(opts = {}, &block)
|
27
|
+
# - put(opts = {}, &block)
|
28
|
+
# - delete(opts = {}, &block)
|
29
|
+
VALID_HTTP_VERBS.each do |method|
|
30
|
+
define_method(method) do |opts = {}, &block|
|
31
|
+
method_attributes = constantize("#{method.upcase}_ATTRIBUTES")
|
32
|
+
unpermitted_attributes = opts.keys - method_attributes
|
33
|
+
unless unpermitted_attributes.empty?
|
34
|
+
raise Wor::Requests::InvalidOptionsError.new(method_attributes, unpermitted_attributes)
|
35
|
+
end
|
36
|
+
|
37
|
+
request(opts.merge(method: method), &block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def request(options = {}, &block)
|
42
|
+
validate_method!(options[:method])
|
43
|
+
|
44
|
+
log_attempt(options[:attempting_to])
|
45
|
+
resp = HTTParty.send(options[:method], uri(options[:path]), request_parameters(options))
|
46
|
+
|
47
|
+
return after_success(resp, options, &block) if resp.success?
|
48
|
+
after_error(resp, options)
|
49
|
+
end
|
50
|
+
|
51
|
+
def logger
|
52
|
+
Wor::Requests.logger
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
def base_url
|
58
|
+
raise NoMethodError, "Subclass must implement method: '#{__method__}'"
|
59
|
+
end
|
60
|
+
|
61
|
+
def external_api_name
|
62
|
+
self.class.name
|
63
|
+
end
|
64
|
+
|
65
|
+
def default_response_type
|
66
|
+
Wor::Requests.default_response_type
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def after_success(response, options)
|
72
|
+
log_success(options[:attempting_to])
|
73
|
+
|
74
|
+
return yield(response) if block_given?
|
75
|
+
handle_response(response, options[:response_type])
|
76
|
+
end
|
77
|
+
|
78
|
+
def after_error(response, options)
|
79
|
+
log_error(response, options[:attempting_to])
|
80
|
+
raise Wor::Requests::RequestError.new(response), exception_message
|
81
|
+
end
|
82
|
+
|
83
|
+
def uri(path)
|
84
|
+
URI.join(formatted_base_url, formatted_path(path))
|
85
|
+
end
|
86
|
+
|
87
|
+
def formatted_base_url
|
88
|
+
base_url.last != '/' ? "#{base_url}/" : base_url
|
89
|
+
end
|
90
|
+
|
91
|
+
def formatted_path(path)
|
92
|
+
return '' if path.nil?
|
93
|
+
return path if path.first != '/'
|
94
|
+
path.slice!(0)
|
95
|
+
path
|
96
|
+
end
|
97
|
+
|
98
|
+
def log_success(attempt)
|
99
|
+
return unless present?(attempt)
|
100
|
+
logger.info "SUCCESS: #{attempt}"
|
101
|
+
end
|
102
|
+
|
103
|
+
def log_attempt(attempt)
|
104
|
+
return unless present?(attempt)
|
105
|
+
logger.info "ATTEMPTING TO: #{attempt}"
|
106
|
+
end
|
107
|
+
|
108
|
+
def log_error(response, attempting_to)
|
109
|
+
return unless present?(attempting_to)
|
110
|
+
response_error = "ERROR when trying to #{attempting_to}. "
|
111
|
+
response_error << "Got status code: #{response.code}. "
|
112
|
+
if present?(response.body)
|
113
|
+
response_error << "Response error: #{JSON.parse(response.body)}"
|
114
|
+
end
|
115
|
+
logger.error response_error
|
116
|
+
rescue => e
|
117
|
+
logger.error("#{response_error} ERROR while parsing response body: #{e.message}.")
|
118
|
+
end
|
119
|
+
|
120
|
+
def exception_message
|
121
|
+
"#{external_api_name} got an error. See logs for more information."
|
122
|
+
end
|
123
|
+
|
124
|
+
def request_parameters(options_hash)
|
125
|
+
answer = {}
|
126
|
+
answer[:body] = options_hash[:body] if present?(options_hash[:body])
|
127
|
+
answer[:query] = options_hash[:query] if present?(options_hash[:query])
|
128
|
+
answer[:headers] = options_hash[:headers] if present?(options_hash[:headers])
|
129
|
+
answer
|
130
|
+
end
|
131
|
+
|
132
|
+
def present?(object)
|
133
|
+
!object.nil?
|
134
|
+
end
|
135
|
+
|
136
|
+
def constantize(string)
|
137
|
+
self.class.class_eval(string)
|
138
|
+
end
|
139
|
+
|
140
|
+
def validate_method!(method)
|
141
|
+
return true if VALID_HTTP_VERBS.include?(method)
|
142
|
+
raise ArgumentError, "#{method} is not a valid method."
|
143
|
+
end
|
144
|
+
|
145
|
+
def response_type(type)
|
146
|
+
return type if Wor::Requests::VALID_RESPONSE_TYPES.include?(type)
|
147
|
+
default_response_type
|
148
|
+
end
|
149
|
+
|
150
|
+
def handle_response(response, response_type)
|
151
|
+
case response_type(response_type)
|
152
|
+
when :json
|
153
|
+
JSON.parse(response.body)
|
154
|
+
else
|
155
|
+
response.body
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wor/requests/version'
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'wor-requests'
|
9
|
+
spec.version = Wor::Requests::VERSION
|
10
|
+
spec.platform = Gem::Platform::RUBY
|
11
|
+
spec.date = Date.today
|
12
|
+
spec.authors = ['draffo', 'mishuagopian']
|
13
|
+
spec.email = ['diego.raffo@wolox.com.ar', 'michel.agopian@wolox.com.ar']
|
14
|
+
|
15
|
+
spec.summary = 'Make external requests in you service objects, with easy logging and error handling!'
|
16
|
+
spec.description = 'Make external requests in you service objects, with easy logging and error handling!'
|
17
|
+
spec.homepage = 'https://github.com/Wolox/wor-requests'
|
18
|
+
spec.license = 'MIT'
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec)/}) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec)/})
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'httparty', '~> 0.13'
|
25
|
+
spec.add_dependency 'railties', '>= 4.1.0', '< 5.1'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'faker'
|
28
|
+
spec.add_development_dependency 'webmock'
|
29
|
+
spec.add_development_dependency 'byebug', '~> 9.0'
|
30
|
+
spec.add_development_dependency 'rubocop', '~> 0.47.0'
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
34
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
|
35
|
+
spec.add_development_dependency 'generator_spec'
|
36
|
+
spec.add_development_dependency 'simplecov'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,240 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wor-requests
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- draffo
|
8
|
+
- mishuagopian
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0.13'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0.13'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: railties
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 4.1.0
|
35
|
+
- - "<"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '5.1'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 4.1.0
|
45
|
+
- - "<"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.1'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: faker
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: webmock
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: byebug
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '9.0'
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '9.0'
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: rubocop
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.47.0
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.47.0
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: bundler
|
106
|
+
requirement: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.13'
|
111
|
+
type: :development
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.13'
|
118
|
+
- !ruby/object:Gem::Dependency
|
119
|
+
name: rake
|
120
|
+
requirement: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '10.0'
|
125
|
+
type: :development
|
126
|
+
prerelease: false
|
127
|
+
version_requirements: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '10.0'
|
132
|
+
- !ruby/object:Gem::Dependency
|
133
|
+
name: rspec
|
134
|
+
requirement: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '3.0'
|
139
|
+
type: :development
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '3.0'
|
146
|
+
- !ruby/object:Gem::Dependency
|
147
|
+
name: codeclimate-test-reporter
|
148
|
+
requirement: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.0.0
|
153
|
+
type: :development
|
154
|
+
prerelease: false
|
155
|
+
version_requirements: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 1.0.0
|
160
|
+
- !ruby/object:Gem::Dependency
|
161
|
+
name: generator_spec
|
162
|
+
requirement: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
type: :development
|
168
|
+
prerelease: false
|
169
|
+
version_requirements: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
- !ruby/object:Gem::Dependency
|
175
|
+
name: simplecov
|
176
|
+
requirement: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
type: :development
|
182
|
+
prerelease: false
|
183
|
+
version_requirements: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
description: Make external requests in you service objects, with easy logging and
|
189
|
+
error handling!
|
190
|
+
email:
|
191
|
+
- diego.raffo@wolox.com.ar
|
192
|
+
- michel.agopian@wolox.com.ar
|
193
|
+
executables: []
|
194
|
+
extensions: []
|
195
|
+
extra_rdoc_files: []
|
196
|
+
files:
|
197
|
+
- ".gitignore"
|
198
|
+
- ".rspec"
|
199
|
+
- ".rubocop.yml"
|
200
|
+
- ".travis.yml"
|
201
|
+
- Gemfile
|
202
|
+
- LICENSE.md
|
203
|
+
- README.md
|
204
|
+
- Rakefile
|
205
|
+
- lib/generators/templates/base_service.rb.erb
|
206
|
+
- lib/generators/templates/wor_requests.rb
|
207
|
+
- lib/generators/wor/requests/install_generator.rb
|
208
|
+
- lib/generators/wor/requests/service_generator.rb
|
209
|
+
- lib/wor/requests.rb
|
210
|
+
- lib/wor/requests/base.rb
|
211
|
+
- lib/wor/requests/invalid_options_error.rb
|
212
|
+
- lib/wor/requests/request_error.rb
|
213
|
+
- lib/wor/requests/version.rb
|
214
|
+
- wor-requests.gemspec
|
215
|
+
homepage: https://github.com/Wolox/wor-requests
|
216
|
+
licenses:
|
217
|
+
- MIT
|
218
|
+
metadata: {}
|
219
|
+
post_install_message:
|
220
|
+
rdoc_options: []
|
221
|
+
require_paths:
|
222
|
+
- lib
|
223
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
224
|
+
requirements:
|
225
|
+
- - ">="
|
226
|
+
- !ruby/object:Gem::Version
|
227
|
+
version: '0'
|
228
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
229
|
+
requirements:
|
230
|
+
- - ">="
|
231
|
+
- !ruby/object:Gem::Version
|
232
|
+
version: '0'
|
233
|
+
requirements: []
|
234
|
+
rubyforge_project:
|
235
|
+
rubygems_version: 2.6.7
|
236
|
+
signing_key:
|
237
|
+
specification_version: 4
|
238
|
+
summary: Make external requests in you service objects, with easy logging and error
|
239
|
+
handling!
|
240
|
+
test_files: []
|