prest 0.1.0 → 0.1.3
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile +2 -1
- data/Gemfile.lock +9 -1
- data/README.md +62 -8
- data/lib/prest/response.rb +22 -0
- data/lib/prest/version.rb +1 -1
- data/lib/prest.rb +40 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70f03f1a6b0308bf6289c7b7c1158c17622999fd54fe788cf7f5c970d8100ed4
|
4
|
+
data.tar.gz: dabc32ac8bbaf16b4a44519cfa6206ee93121a5c9c666dd6e760b4bd720cf06b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61695f52e98eb93f4c9a321771ca0061ba203640a1bdf971c390767d78b08a0e2f11e3ad734213d04778bf2ae303427e183d1e957be6d4864927a92906c97390
|
7
|
+
data.tar.gz: 8121063ee97d2cfad9a68e68ae16b8ad12248423cd17286feae94c826fd6e6f05e2edfd88a3890c4bfecf7f2eafdf55ae3189dac2d1e097886253877096d96ed
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
prest (0.1.
|
4
|
+
prest (0.1.3)
|
5
5
|
httparty (~> 0.20.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,6 +9,7 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
ast (2.4.2)
|
11
11
|
diff-lcs (1.5.0)
|
12
|
+
docile (1.4.0)
|
12
13
|
httparty (0.20.0)
|
13
14
|
mime-types (~> 3.0)
|
14
15
|
multi_xml (>= 0.5.2)
|
@@ -50,6 +51,12 @@ GEM
|
|
50
51
|
rubocop-ast (1.19.1)
|
51
52
|
parser (>= 3.1.1.0)
|
52
53
|
ruby-progressbar (1.11.0)
|
54
|
+
simplecov (0.21.2)
|
55
|
+
docile (~> 1.1)
|
56
|
+
simplecov-html (~> 0.11)
|
57
|
+
simplecov_json_formatter (~> 0.1)
|
58
|
+
simplecov-html (0.12.3)
|
59
|
+
simplecov_json_formatter (0.1.4)
|
53
60
|
unicode-display_width (2.2.0)
|
54
61
|
|
55
62
|
PLATFORMS
|
@@ -61,6 +68,7 @@ DEPENDENCIES
|
|
61
68
|
rake (~> 13.0)
|
62
69
|
rspec (~> 3.0)
|
63
70
|
rubocop (~> 1.21)
|
71
|
+
simplecov (~> 0.21.2)
|
64
72
|
|
65
73
|
BUNDLED WITH
|
66
74
|
2.3.7
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# Prest
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/prest)
|
4
|
+
[](https://github.com/gogrow-dev/prest/actions/workflows/main.yml)
|
5
|
+
[](https://codeclimate.com/github/gogrow-dev/prest/maintainability)
|
6
|
+
[](https://codeclimate.com/github/gogrow-dev/prest/test_coverage)
|
7
|
+
|
8
|
+
Programmatically communicate with any REST API.
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
@@ -34,6 +39,10 @@ Prest::Client.new('https://example.com/api').users(name: 'Juan', created_at: '20
|
|
34
39
|
Prest::Client.new('https://example.com/api').users(2).pulls(1).comments.get
|
35
40
|
# This translates to making a GET https://example.com/api/users/2/pulls/1/comments
|
36
41
|
|
42
|
+
# To make requests to url which have a dash in it, use a double __
|
43
|
+
Prest::Client.new('https://example.com/api').job__posts(1).get
|
44
|
+
# This translates to making a GET https://example.com/api/job-posts/1
|
45
|
+
|
37
46
|
# To pass headers to the request, pass them to the client constructor
|
38
47
|
Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Bearer Token xxxyyyzzz' } })
|
39
48
|
.users
|
@@ -45,29 +54,74 @@ Prest::Client.new('https://example.com/api', { headers: { 'Authorization' => 'Be
|
|
45
54
|
.post(body: { username: 'juan-apa' })
|
46
55
|
```
|
47
56
|
|
57
|
+
### Accessing the response
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
response = Prest::Client.new('https://example.com/api').users.get
|
61
|
+
|
62
|
+
response[:users] # is equivalent to response.body[:users]
|
63
|
+
# You can access the body directly from the response object
|
64
|
+
|
65
|
+
response.successful? # is equivalent to response.status is between 200-299
|
66
|
+
|
67
|
+
response.status # returns the status code of the response
|
68
|
+
response.headers # returns the headers of the response
|
69
|
+
response.body # returns the body of the response
|
70
|
+
```
|
71
|
+
|
48
72
|
### Rails service-like approach
|
49
73
|
|
50
74
|
```ruby
|
51
75
|
# app/services/github.rb
|
52
|
-
class Github
|
53
|
-
|
76
|
+
class Github < Prest::Service
|
77
|
+
private
|
78
|
+
|
79
|
+
def base_uri
|
80
|
+
'https://api.github.com'
|
81
|
+
end
|
82
|
+
|
83
|
+
def options
|
84
|
+
{
|
85
|
+
headers: {
|
86
|
+
'access_token' => 'xxxyyyzzz'
|
87
|
+
}
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
54
91
|
|
55
|
-
|
56
|
-
|
92
|
+
# Then, you can use it like this anywhere in your app:
|
93
|
+
|
94
|
+
Github.users('juan-apa').pulls.get
|
95
|
+
```
|
96
|
+
|
97
|
+
You can also define an initializer to pass values in runtime to your service:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
# app/services/github.rb
|
101
|
+
class Github < Prest::Service
|
102
|
+
def initialize(organization)
|
103
|
+
@organization = organization
|
57
104
|
end
|
58
105
|
|
59
106
|
private
|
60
107
|
|
61
|
-
def
|
108
|
+
def base_uri
|
109
|
+
'https://api.github.com'
|
110
|
+
end
|
111
|
+
|
112
|
+
def options
|
62
113
|
{
|
63
|
-
|
114
|
+
headers: {
|
115
|
+
'access_token' => 'xxxyyyzzz',
|
116
|
+
'org' => @organization
|
117
|
+
}
|
64
118
|
}
|
65
119
|
end
|
66
120
|
end
|
67
121
|
|
68
122
|
# Then, you can use it like this anywhere in your app:
|
69
123
|
|
70
|
-
Github.new
|
124
|
+
Github.new('gogrow-dev').users.get
|
71
125
|
```
|
72
126
|
|
73
127
|
## Development
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Prest
|
4
|
+
# Main wrapper class for Prest Response.
|
5
|
+
class Response
|
6
|
+
extend ::Forwardable
|
7
|
+
|
8
|
+
def_delegator :@body, :[]
|
9
|
+
|
10
|
+
attr_reader :status, :body, :headers
|
11
|
+
|
12
|
+
def initialize(status, body, headers)
|
13
|
+
@status = status
|
14
|
+
@body = body
|
15
|
+
@headers = headers
|
16
|
+
end
|
17
|
+
|
18
|
+
def successful?
|
19
|
+
@status.between?(100, 399)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/prest/version.rb
CHANGED
data/lib/prest.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'httparty'
|
4
4
|
require_relative 'prest/version'
|
5
|
+
require_relative 'prest/response'
|
5
6
|
|
6
7
|
module Prest
|
7
8
|
class Error < StandardError; end
|
@@ -27,21 +28,24 @@ module Prest
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
def respond_to_missing?(_)
|
31
|
+
def respond_to_missing?(_, _)
|
31
32
|
true
|
32
33
|
end
|
33
34
|
|
34
35
|
private
|
35
36
|
|
36
37
|
def execute_query(http_method, body: {})
|
37
|
-
::HTTParty.send(http_method, build_url, headers: headers, body: body)
|
38
|
+
res = ::HTTParty.send(http_method, build_url, headers: headers, body: body)
|
39
|
+
::Prest::Response.new(res.code, res.parsed_response, res.headers)
|
40
|
+
rescue ::HTTParty::ResponseError => e
|
41
|
+
raise Error, e.message
|
38
42
|
end
|
39
43
|
|
40
44
|
def chain_fragment(fragment_name, *args, **kwargs)
|
41
45
|
arguments = args.join('/')
|
42
46
|
parsed_args = arguments.empty? ? '' : "#{arguments}/"
|
43
47
|
@query_params.merge!(kwargs)
|
44
|
-
@fragments << "#{fragment_name.
|
48
|
+
@fragments << "#{fragment_name.gsub("__", "-")}/#{parsed_args}"
|
45
49
|
end
|
46
50
|
|
47
51
|
def headers
|
@@ -60,4 +64,37 @@ module Prest
|
|
60
64
|
"#{@base_uri}/#{path}#{stringified_params}"
|
61
65
|
end
|
62
66
|
end
|
67
|
+
|
68
|
+
# Base Service Object from which to extend to integrate with api's using the Prest gem.
|
69
|
+
class Service < BasicObject
|
70
|
+
def method_missing(method, *args, **kwargs)
|
71
|
+
client.__send__(method, *args, **kwargs)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.method_missing(method, *args, **kwargs)
|
75
|
+
new.__send__(:client).__send__(method, *args, **kwargs)
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.respond_to_missing?(_, _)
|
79
|
+
true
|
80
|
+
end
|
81
|
+
|
82
|
+
def respond_to_missing?(_, _)
|
83
|
+
true
|
84
|
+
end
|
85
|
+
|
86
|
+
protected
|
87
|
+
|
88
|
+
def client
|
89
|
+
@client ||= ::Prest::Client.new(base_uri, options)
|
90
|
+
end
|
91
|
+
|
92
|
+
def base_uri
|
93
|
+
::Kernel.raise(Error, 'Implement in subclass')
|
94
|
+
end
|
95
|
+
|
96
|
+
def options
|
97
|
+
{}
|
98
|
+
end
|
99
|
+
end
|
63
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Aparicio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
43
|
- lib/prest.rb
|
44
|
+
- lib/prest/response.rb
|
44
45
|
- lib/prest/version.rb
|
45
46
|
- prest.gemspec
|
46
47
|
homepage: https://gogrow.dev
|