wrappi 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +83 -2
- data/lib/wrappi.rb +14 -0
- data/lib/wrappi/endpoint.rb +29 -1
- data/lib/wrappi/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61be13fabeb0542feedb58078fe1308cf42286994dcafa91a7fccdf0de08200c
|
4
|
+
data.tar.gz: e0e8fe4b69ad694edc0ec1a0c2bb3f3a512f0415c8ceab94c7ed82065b721b37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44cf1ab2fb72aadbddc0a191c1fccb6d3dee981189f28b08722947ae6295969dd05711f090524519ef832af938eec569d358649a1859a294e286fa1922096f27
|
7
|
+
data.tar.gz: 70ad84300904ab70b2758f306e925462f439b23d743b82fe2fc085dedaef06a2081541abb0c3aca8f0f7a19145fe818808c99257ae0b9d391d4f127ce31bad98
|
data/README.md
CHANGED
@@ -30,8 +30,10 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
__Github example:__
|
32
32
|
|
33
|
+
You can test this examples running `bin/console`
|
34
|
+
|
33
35
|
```ruby
|
34
|
-
module
|
36
|
+
module GithubCLI
|
35
37
|
class Client < Wrappi::Client
|
36
38
|
setup do |config|
|
37
39
|
config.domain = 'https://api.github.com'
|
@@ -51,13 +53,92 @@ end
|
|
51
53
|
```
|
52
54
|
|
53
55
|
```ruby
|
54
|
-
user =
|
56
|
+
user = GithubCLI::User.new(username: 'arturictus')
|
55
57
|
user.success? # => true
|
56
58
|
user.error? # => false
|
57
59
|
user.status_code # => 200
|
58
60
|
user.body # => {"login"=>"arturictus", "id"=>1930175, ...}
|
59
61
|
```
|
60
62
|
|
63
|
+
### #success?
|
64
|
+
|
65
|
+
The next behaviours are using `#success?` method. You can override by redefining your own success?
|
66
|
+
|
67
|
+
The current `#success?` is defined like this:
|
68
|
+
|
69
|
+
_wrappi/response.rb_
|
70
|
+
```ruby
|
71
|
+
def success?
|
72
|
+
@success ||= request.code < 300 && request.code >= 200
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Overrride your own in Endpoint
|
77
|
+
```ruby
|
78
|
+
def success?
|
79
|
+
response.status == 201
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
##### #on_success | #on_error
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
GithubCLI::User.new(username: 'arturictus')
|
87
|
+
.on_success do |inst|
|
88
|
+
inst.status_code # => 200
|
89
|
+
inst.body # => {"login"=>"arturictus", "id"=>1930175, ...}
|
90
|
+
# do something useful
|
91
|
+
end.on_error do |inst|
|
92
|
+
puts "Error retrieving use"
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
##### ::body
|
97
|
+
If you just need to retrieve the body and handle the error response on your side
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
GithubCLI::User.body(username: 'arturictus') # => {"login"=>"arturictus", "id"=>1930175, ...}
|
101
|
+
```
|
102
|
+
```ruby
|
103
|
+
GithubCLI::User.body(username: 'sdfsdfasdjfojaspdjfpsajdpfoijsapdofijsadf')
|
104
|
+
# => {"message"=>"Not Found", "documentation_url"=>"https://developer.github.com/v3/users/#get-a-single-user"}
|
105
|
+
```
|
106
|
+
|
107
|
+
##### ::call
|
108
|
+
|
109
|
+
returns `false` if unsuccessful and instance if successful
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
if req = GithubCLI::User.call(username: 'arturictus')
|
113
|
+
req.body # => {"login"=>"arturictus", "id"=>1930175, ...}
|
114
|
+
else
|
115
|
+
# Handle error
|
116
|
+
end
|
117
|
+
```
|
118
|
+
##### ::call!
|
119
|
+
|
120
|
+
Raises error if unsuccessful, returns instance if successful
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
begin
|
124
|
+
req = GithubCLI::User.call!(username: 'arturictus')
|
125
|
+
req.body # => {"login"=>"arturictus", "id"=>1930175, ...}
|
126
|
+
rescue => Wrappi::UnsuccessfulResponse
|
127
|
+
# Handle error or not
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
The error:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
GithubCLI::User.call!(username: 'sdfsdfasdjfojaspdjfpsajdpfoijsapdofijsadf')
|
135
|
+
# Wrappi::UnsuccessfulResponse ()
|
136
|
+
# raw_body: {"message":"Not Found","documentation_url":"https://developer.github.com/v3/users/#get-a-single-user"}
|
137
|
+
# code: 404
|
138
|
+
# uri: https://api.github.com/users/sdfsdfasdjfojaspdjfpsajdpfoijsapdofijsadf
|
139
|
+
# success: false
|
140
|
+
```
|
141
|
+
|
61
142
|
#### Async
|
62
143
|
Wrappi comes with a background Job out of the box. If you are in a Rails app the `#async`
|
63
144
|
method will queue a new job (`< ActiveJob::Base`) that will make the request and trigger the async callback
|
data/lib/wrappi.rb
CHANGED
@@ -8,6 +8,20 @@ module Wrappi
|
|
8
8
|
class TimeoutError < StandardError; end
|
9
9
|
class NotAuthorizedAccessError < StandardError; end
|
10
10
|
class JsonParseError < StandardError; end
|
11
|
+
class UnsuccessfulResponse < StandardError
|
12
|
+
def initialize(endpoint)
|
13
|
+
@endpoint = endpoint
|
14
|
+
@output = StringIO.new.tap { |s| s.puts "" }
|
15
|
+
super(_message)
|
16
|
+
end
|
17
|
+
|
18
|
+
def _message
|
19
|
+
@endpoint.response.to_h.each do |k, v|
|
20
|
+
@output.puts " #{k}: #{v}"
|
21
|
+
end
|
22
|
+
@output.string
|
23
|
+
end
|
24
|
+
end
|
11
25
|
end
|
12
26
|
|
13
27
|
require 'wrappi/async_job'
|
data/lib/wrappi/endpoint.rb
CHANGED
@@ -19,6 +19,7 @@ module Wrappi
|
|
19
19
|
basic_auth: proc { client.basic_auth }
|
20
20
|
}
|
21
21
|
)
|
22
|
+
|
22
23
|
attr_reader :input_params, :options
|
23
24
|
def initialize(input_params = {}, options = {})
|
24
25
|
@input_params = input_params
|
@@ -29,10 +30,37 @@ module Wrappi
|
|
29
30
|
new(*args).call
|
30
31
|
end
|
31
32
|
|
33
|
+
def self.call!(*args)
|
34
|
+
new(*args).call!
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.body(*args)
|
38
|
+
new(*args).body
|
39
|
+
end
|
40
|
+
|
41
|
+
def on_success(&block)
|
42
|
+
block.call(self) if success?
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def on_error(&block)
|
47
|
+
block.call(self) unless success?
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def call
|
52
|
+
return false unless success?
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def call!
|
57
|
+
raise UnsuccessfulResponse.new(self) unless success?
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
32
61
|
def response
|
33
62
|
@response ||= Executer.call(self)
|
34
63
|
end
|
35
|
-
alias_method :call, :response
|
36
64
|
|
37
65
|
def body; response.body end
|
38
66
|
def success?; response.success? end
|
data/lib/wrappi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrappi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Pañach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|