prest 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8a009d1db76e802ed1e416845134d08d466f21b26c90aea89929a8efbf875ea
4
- data.tar.gz: b5a860ff089a4b2652fce692e0d9b441333384b6545cca661cf8f143bef67ff8
3
+ metadata.gz: 70f03f1a6b0308bf6289c7b7c1158c17622999fd54fe788cf7f5c970d8100ed4
4
+ data.tar.gz: dabc32ac8bbaf16b4a44519cfa6206ee93121a5c9c666dd6e760b4bd720cf06b
5
5
  SHA512:
6
- metadata.gz: d1522ea4ef50f8c66e5c21510f87e23a4a5fa1191f2cc5b585c12de698dcd6f1889b427119773fdd8e5af4d55cd412890b1b94e1c3194f093232ec2c8238242b
7
- data.tar.gz: a0922e5d6b4577481cee58bcc0ea37a26c5ad5710499709d49b1af2371b9966dcb1e8135dc06f4fb43a764d2c94703a9f5aefb4a5024d7c7c28c2dd708e6141e
6
+ metadata.gz: 61695f52e98eb93f4c9a321771ca0061ba203640a1bdf971c390767d78b08a0e2f11e3ad734213d04778bf2ae303427e183d1e957be6d4864927a92906c97390
7
+ data.tar.gz: 8121063ee97d2cfad9a68e68ae16b8ad12248423cd17286feae94c826fd6e6f05e2edfd88a3890c4bfecf7f2eafdf55ae3189dac2d1e097886253877096d96ed
data/.rubocop.yml CHANGED
@@ -1,10 +1,11 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.7
2
+ TargetRubyVersion: 2.7.0
3
3
  NewCops: disable
4
4
 
5
5
  Metrics/BlockLength:
6
6
  Exclude:
7
7
  - "spec/**/*"
8
+
8
9
  Style/StringLiterals:
9
10
  Enabled: true
10
11
  EnforcedStyle: single_quotes
data/CHANGELOG.md CHANGED
@@ -3,3 +3,7 @@
3
3
  ## [0.1.0] - 2022-07-20
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2022-07-22
8
+
9
+ - Wraps endpoints response in a `Prest::Response` object for easier access to the response data
data/Gemfile CHANGED
@@ -7,4 +7,5 @@ gemspec
7
7
 
8
8
  gem 'rake', '~> 13.0'
9
9
  gem 'rspec', '~> 3.0'
10
- gem 'rubocop', '~> 1.21'
10
+ gem 'rubocop', '~> 1.21', require: false
11
+ gem 'simplecov', '~> 0.21.2', require: false, group: :test
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prest (0.1.0)
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
- Programmaticcally communicate with any REST API.
3
+ [![Gem Version](https://badge.fury.io/rb/prest.svg)](https://badge.fury.io/rb/prest)
4
+ [![Ruby](https://github.com/gogrow-dev/prest/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/gogrow-dev/prest/actions/workflows/main.yml)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/f81b2e00be4d8eaa5e81/maintainability)](https://codeclimate.com/github/gogrow-dev/prest/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/f81b2e00be4d8eaa5e81/test_coverage)](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
- BASE_URL = 'https://api.github.com'
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
- def new
56
- Prest::Client.new(BASE_URL, { headers: headers })
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 headers
108
+ def base_uri
109
+ 'https://api.github.com'
110
+ end
111
+
112
+ def options
62
113
  {
63
- 'access_token' => 'xxxyyyzzz'
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.users('juan-apa').pulls.get
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prest
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.3'
5
5
  end
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.tr("_", "-")}/#{parsed_args}"
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.0
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-07-20 00:00:00.000000000 Z
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