wrappi 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 +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +169 -0
- data/Rakefile +6 -0
- data/bin/console +34 -0
- data/bin/dev_server +3 -0
- data/bin/setup +8 -0
- data/lib/wrappi.rb +13 -0
- data/lib/wrappi/client.rb +68 -0
- data/lib/wrappi/endpoint.rb +76 -0
- data/lib/wrappi/path_gen.rb +56 -0
- data/lib/wrappi/request.rb +30 -0
- data/lib/wrappi/request/get.rb +9 -0
- data/lib/wrappi/request/template.rb +38 -0
- data/lib/wrappi/request/with_body.rb +24 -0
- data/lib/wrappi/response.rb +56 -0
- data/lib/wrappi/version.rb +3 -0
- data/wrappi.gemspec +29 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 23d8f581f5cc19d822e8f54210498ea68ae92ffdecddc7598c62d1d07b011fc8
|
4
|
+
data.tar.gz: 330cae7ff0e5112396773044588cbe0da41e4bb861673d6be2f750ecffbe3eff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cc0503810349bb2ab62054863cfd4364bd5e88d96201a2e52345a4057cbeefa234efb2385b2073caeaae49f14bb77d8619900f03ba5c166d301334ed829a0f02
|
7
|
+
data.tar.gz: 03dae8c1b94cddfc3eb4118cee9c1b7a9794ba5550ecf52651640039f77efef3c3d7e8de605d00cd1c498e8ee7b402a9c40ce0d9dd18e49cc062623fdc0d752b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at arturictus@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Artur Pañach
|
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,169 @@
|
|
1
|
+
# Wrappi
|
2
|
+
|
3
|
+
Framework to create API clients.
|
4
|
+
The intention is to bring the best practices and standarize the mess it's currently happening with the API clients.
|
5
|
+
It allows to create API clients in a declarative way improving readability and unifying the behavior.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'wrappi'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install wrappi
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
__Github example:__
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
module Github
|
29
|
+
class Client < Wrappi::Client
|
30
|
+
setup do |config|
|
31
|
+
config.domain = 'https://api.github.com'
|
32
|
+
config.headers = {
|
33
|
+
'Content-Type' => 'application/json',
|
34
|
+
'Accept' => 'application/vnd.github.v3+json',
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class User < Wrappi::Endpoint
|
40
|
+
client Client
|
41
|
+
verb :get
|
42
|
+
path "users/:username"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
user = Github::User.new(username: 'arturictus')
|
49
|
+
user.success?
|
50
|
+
# => true
|
51
|
+
user.code
|
52
|
+
# => 200
|
53
|
+
user.body
|
54
|
+
# => {"login"=>"arturictus", "id"=>1930175, ...}
|
55
|
+
```
|
56
|
+
|
57
|
+
### Client
|
58
|
+
|
59
|
+
Is the main configuration for your service.
|
60
|
+
|
61
|
+
It holds the common configuration for all the endpoints (`Wrappi::Endpoint`).
|
62
|
+
|
63
|
+
Required:
|
64
|
+
|
65
|
+
- `domain`: Yep, you know.
|
66
|
+
```ruby
|
67
|
+
config.domain = 'https://api.github.com'
|
68
|
+
```
|
69
|
+
|
70
|
+
Optionals:
|
71
|
+
|
72
|
+
- `params`: Set global params for all the `Endpoints`.
|
73
|
+
This is a great place to put the `api_key`.
|
74
|
+
```ruby
|
75
|
+
config.params = { "api_key" => "asdfasdfoerkwlejrwer" }
|
76
|
+
```
|
77
|
+
default: `{}`
|
78
|
+
|
79
|
+
- `logger`: Set your logger.
|
80
|
+
```ruby
|
81
|
+
config.logger = Rails.logger
|
82
|
+
```
|
83
|
+
default: `Logger.new(STDOUT)`
|
84
|
+
|
85
|
+
- `headers`: Headers for all the endpoints. Format, Authentication.
|
86
|
+
```ruby
|
87
|
+
config.headers = {
|
88
|
+
"Content-Type" => "application/json",
|
89
|
+
"Accept' => 'application/json",
|
90
|
+
"Auth-Token" => "verysecret"
|
91
|
+
}
|
92
|
+
```
|
93
|
+
default:
|
94
|
+
```ruby
|
95
|
+
{ 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
96
|
+
```
|
97
|
+
|
98
|
+
- `ssl_context`: If you need to set an ssl_context.
|
99
|
+
```ruby
|
100
|
+
config.ssl_context = OpenSSL::SSL::SSLContext.new.tap do |ctx|
|
101
|
+
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
102
|
+
end
|
103
|
+
```
|
104
|
+
default: `nil`
|
105
|
+
|
106
|
+
- `use_ssl_context`: It has to be set to `true` for using the `ssl_context`
|
107
|
+
|
108
|
+
default: `false`
|
109
|
+
|
110
|
+
### Endpoint
|
111
|
+
|
112
|
+
Required:
|
113
|
+
- `client`: `Wrappi::Client` `class`
|
114
|
+
- `path`: The path.
|
115
|
+
|
116
|
+
TODO
|
117
|
+
- `verb`:
|
118
|
+
- `:get`
|
119
|
+
- `:post`
|
120
|
+
- `:delete`
|
121
|
+
- `:put`
|
122
|
+
|
123
|
+
default: `:get`
|
124
|
+
|
125
|
+
Optional:
|
126
|
+
|
127
|
+
- `default_params`:
|
128
|
+
|
129
|
+
default: `{}`
|
130
|
+
- `headers`:
|
131
|
+
|
132
|
+
default: `proc { client.headers }`
|
133
|
+
|
134
|
+
- `basic_auth`:
|
135
|
+
|
136
|
+
default: `nil`
|
137
|
+
- `follow_redirects`:
|
138
|
+
|
139
|
+
default: `true`
|
140
|
+
- `body_type`:
|
141
|
+
|
142
|
+
default: `:json`
|
143
|
+
|
144
|
+
## Development
|
145
|
+
|
146
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
147
|
+
|
148
|
+
Run test:
|
149
|
+
```
|
150
|
+
bin/dev_server
|
151
|
+
```
|
152
|
+
This will run a rails server. The test are running agains it.
|
153
|
+
|
154
|
+
```
|
155
|
+
bundle exec rspec
|
156
|
+
```
|
157
|
+
|
158
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
## Contributing
|
163
|
+
|
164
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/arturictus/wrappi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
165
|
+
|
166
|
+
|
167
|
+
## License
|
168
|
+
|
169
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wrappi"
|
5
|
+
|
6
|
+
# Github example:
|
7
|
+
|
8
|
+
module Github
|
9
|
+
class Client < Wrappi::Client
|
10
|
+
setup do |config|
|
11
|
+
config.domain = 'https://api.github.com'
|
12
|
+
config.headers = {
|
13
|
+
'Content-Type' => 'application/json',
|
14
|
+
'Accept' => 'application/vnd.github.v3+json',
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
class User < Wrappi::Endpoint
|
19
|
+
client Client
|
20
|
+
verb :get
|
21
|
+
path "users/:username"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
27
|
+
# with your gem easier. You can also use a different console, if you like.
|
28
|
+
|
29
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
30
|
+
# require "pry"
|
31
|
+
# Pry.start
|
32
|
+
|
33
|
+
require "irb"
|
34
|
+
IRB.start
|
data/bin/dev_server
ADDED
data/bin/setup
ADDED
data/lib/wrappi.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'logger'
|
2
|
+
module Wrappi
|
3
|
+
# This class is expected to handle all the configurations for your main module
|
4
|
+
class Client
|
5
|
+
class TimeoutError < StandardError; end
|
6
|
+
class JsonParseError < StandardError; end
|
7
|
+
class NotAuthorizedAccessError < StandardError; end
|
8
|
+
|
9
|
+
include Fusu::Configurable
|
10
|
+
|
11
|
+
# Not verify example
|
12
|
+
# OpenSSL::SSL::SSLContext.new.tap do |ctx|
|
13
|
+
# ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
14
|
+
# end
|
15
|
+
config_accessor :ssl_context
|
16
|
+
config_accessor(:use_ssl_context) { false }
|
17
|
+
config_accessor(:logger) { Logger.new(STDOUT) }
|
18
|
+
config_accessor(:headers) do
|
19
|
+
{ 'Content-Type' => 'application/json',
|
20
|
+
'Accept' => 'application/json' }
|
21
|
+
end
|
22
|
+
config_accessor(:params) { {} }
|
23
|
+
|
24
|
+
def self.setup
|
25
|
+
yield(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.domain=(domain)
|
29
|
+
@domain = domain
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.timeout=(opts)
|
33
|
+
@timeout = { write: 9, connect: 9, read: 9 }.merge(opts)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.timeout
|
37
|
+
return @timeout if defined?(@timeout)
|
38
|
+
self.timeout = {}
|
39
|
+
@timeout
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.domain
|
43
|
+
fail "[#{self.class}] Bad configuration: you must set the `domain` config" unless @domain
|
44
|
+
@domain
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.errors
|
48
|
+
[
|
49
|
+
TimeoutError,
|
50
|
+
JsonParseError,
|
51
|
+
NotAuthorizedAccessError
|
52
|
+
]
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.root
|
56
|
+
Pathname.new(File.expand_path('../../', __FILE__))
|
57
|
+
end
|
58
|
+
|
59
|
+
# def self.params_with_defaults(params = {})
|
60
|
+
# if self.use_ssl_context
|
61
|
+
# fail "[#{self}] Bad configuration: You set `use_ssl_context` but did not provide `ssl_context`" unless self.ssl_context
|
62
|
+
# params.reverse_merge(ssl_context: self.ssl_context)
|
63
|
+
# else
|
64
|
+
# params
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'wrappi/response'
|
2
|
+
module Wrappi
|
3
|
+
class Endpoint < Miller.base(
|
4
|
+
:verb, :client, :path, :default_params,
|
5
|
+
:headers, :follow_redirects, :basic_auth,
|
6
|
+
:body_type,
|
7
|
+
default_config: {
|
8
|
+
verb: :get,
|
9
|
+
client: proc { raise 'client not set' }, # TODO: add proper error
|
10
|
+
path: proc { raise 'path not defined' }, # TODO: add proper error
|
11
|
+
default_params: {},
|
12
|
+
headers: proc { client.headers },
|
13
|
+
follow_redirects: true,
|
14
|
+
body_type: :json
|
15
|
+
}
|
16
|
+
)
|
17
|
+
attr_reader :input_params, :options
|
18
|
+
def initialize(input_params = {}, options = {})
|
19
|
+
@input_params = input_params
|
20
|
+
@options = options
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.call(*args)
|
24
|
+
new(*args).call
|
25
|
+
end
|
26
|
+
|
27
|
+
def url
|
28
|
+
URI.join("#{client.domain}/", path_gen.path)
|
29
|
+
end
|
30
|
+
|
31
|
+
# overridable
|
32
|
+
def consummated_params
|
33
|
+
params
|
34
|
+
end
|
35
|
+
|
36
|
+
# overridable
|
37
|
+
def before_request
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
# overridable
|
42
|
+
def after_request(response)
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
def response
|
47
|
+
return unless before_request
|
48
|
+
@response ||= Response.new do
|
49
|
+
Request.new(self).call
|
50
|
+
end.tap(&:request)
|
51
|
+
after_request(@response)
|
52
|
+
@response
|
53
|
+
end
|
54
|
+
alias_method :call, :response
|
55
|
+
|
56
|
+
def body; response.body end
|
57
|
+
def success?; response.success? end
|
58
|
+
def status; response.status end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def params
|
63
|
+
path_gen.params
|
64
|
+
end
|
65
|
+
|
66
|
+
def processed_params
|
67
|
+
client.params.merge(default_params.merge(input_params))
|
68
|
+
end
|
69
|
+
|
70
|
+
def path_gen
|
71
|
+
@path_gen ||= PathGen.new(path, processed_params)
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Wrappi
|
2
|
+
class PathGen
|
3
|
+
class MissingParamError < StandardError; end
|
4
|
+
PATTERN = /:\w+/
|
5
|
+
attr_reader :input_path, :input_params
|
6
|
+
def initialize(input_path, input_params)
|
7
|
+
@input_path = input_path
|
8
|
+
@input_params = Fusu::HashWithIndifferentAccess.new(input_params)
|
9
|
+
@interpolable = input_path =~ PATTERN
|
10
|
+
end
|
11
|
+
|
12
|
+
def compiled_path
|
13
|
+
return input_path unless interpolable?
|
14
|
+
@compiled_path ||= URI.escape(new_sections.join('/'))
|
15
|
+
end
|
16
|
+
alias_method :path, :compiled_path
|
17
|
+
|
18
|
+
def processed_params
|
19
|
+
return input_params unless interpolable?
|
20
|
+
@processed_params ||= input_params.reject{ |k, v| keys_in_params.include?(k.to_sym) }
|
21
|
+
end
|
22
|
+
alias_method :params, :processed_params
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def interpolable?
|
27
|
+
@interpolable
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_sections
|
31
|
+
sections.map do |section|
|
32
|
+
if section =~ PATTERN
|
33
|
+
key = section.delete(':')
|
34
|
+
raise MissingParamError.new("path: #{input_path} requires param ':#{key}'") unless input_params.key?(key)
|
35
|
+
input_params[key]
|
36
|
+
else
|
37
|
+
section
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def sections
|
43
|
+
input_path.split("/")
|
44
|
+
end
|
45
|
+
|
46
|
+
def keys_in_params
|
47
|
+
interpolations.map do |k|
|
48
|
+
k.delete(':').to_sym
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def interpolations
|
53
|
+
input_path.scan(PATTERN)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Wrappi
|
2
|
+
class Request
|
3
|
+
attr_reader :endpoint
|
4
|
+
def initialize(endpoint)
|
5
|
+
@endpoint = endpoint
|
6
|
+
end
|
7
|
+
|
8
|
+
def verb
|
9
|
+
endpoint.verb
|
10
|
+
end
|
11
|
+
|
12
|
+
def strategy
|
13
|
+
@strategy ||= case verb
|
14
|
+
when :get
|
15
|
+
Get.new(endpoint)
|
16
|
+
when :post, :delete, :put
|
17
|
+
WithBody.new(endpoint)
|
18
|
+
else
|
19
|
+
raise 'Verb strategy not defined'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def call
|
24
|
+
@call ||= strategy.call
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
require 'wrappi/request/template'
|
29
|
+
require 'wrappi/request/get'
|
30
|
+
require 'wrappi/request/with_body'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Wrappi
|
2
|
+
class Request
|
3
|
+
class Template
|
4
|
+
attr_reader :endpoint
|
5
|
+
def initialize(endpoint)
|
6
|
+
@endpoint = endpoint
|
7
|
+
end
|
8
|
+
|
9
|
+
def client
|
10
|
+
endpoint.client
|
11
|
+
end
|
12
|
+
|
13
|
+
def params
|
14
|
+
endpoint.consummated_params
|
15
|
+
end
|
16
|
+
|
17
|
+
def url
|
18
|
+
endpoint.url
|
19
|
+
end
|
20
|
+
|
21
|
+
def verb
|
22
|
+
endpoint.verb
|
23
|
+
end
|
24
|
+
|
25
|
+
def http
|
26
|
+
h = HTTP.timeout(client.timeout)
|
27
|
+
.headers(endpoint.headers)
|
28
|
+
h = h.follow() if endpoint.follow_redirects # TODO: add strict mode
|
29
|
+
h = h.basic_auth(endpoint.basic_auth) if endpoint.basic_auth
|
30
|
+
h
|
31
|
+
end
|
32
|
+
|
33
|
+
def call
|
34
|
+
raise NotImplementedError
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Wrappi
|
2
|
+
class Request
|
3
|
+
class WithBody < Template
|
4
|
+
def call
|
5
|
+
http.send(verb, url, body)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def body
|
11
|
+
{ body_type.to_sym => params }
|
12
|
+
end
|
13
|
+
|
14
|
+
def body_type
|
15
|
+
case endpoint.body_type
|
16
|
+
when :json, :form, :body
|
17
|
+
endpoint.body_type
|
18
|
+
else
|
19
|
+
raise "body_type not recognized"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Wrappi
|
2
|
+
# Wrapper around HTTP::Response
|
3
|
+
# check documentation at:
|
4
|
+
# https://github.com/httprb/http/wiki/Response-Handling
|
5
|
+
class Response
|
6
|
+
class TimeoutError < StandardError; end
|
7
|
+
class NotAuthorizedAccessError < StandardError; end
|
8
|
+
attr_reader :block
|
9
|
+
|
10
|
+
def initialize(&block)
|
11
|
+
@block = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def request
|
15
|
+
@request ||= block.call
|
16
|
+
# raise controlled errors
|
17
|
+
end
|
18
|
+
alias_method :call, :request
|
19
|
+
|
20
|
+
def body
|
21
|
+
@body ||= request.parse
|
22
|
+
end
|
23
|
+
|
24
|
+
def success?
|
25
|
+
@success ||= request.code < 300 && request.code >= 200
|
26
|
+
end
|
27
|
+
|
28
|
+
def error?
|
29
|
+
!success?
|
30
|
+
end
|
31
|
+
|
32
|
+
def raw_body
|
33
|
+
@raw_body ||= request.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def uri
|
37
|
+
request.uri.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
def status
|
41
|
+
request.code
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing(method_name, *arguments, &block)
|
45
|
+
if request.respond_to?(method_name)
|
46
|
+
request.send(method_name, *arguments, &block)
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def respond_to_missing?(method_name, include_private = false)
|
53
|
+
request.respond_to?(method_name) || super
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/wrappi.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wrappi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wrappi"
|
8
|
+
spec.version = Wrappi::VERSION
|
9
|
+
spec.authors = ["Artur Pañach"]
|
10
|
+
spec.email = ["arturictus@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{framework to create and standarize quickly API wrappers}
|
13
|
+
spec.description = %q{framework to create and standarize quickly API wrappers}
|
14
|
+
spec.homepage = "https://github.com/arturictus/wrappi"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "sinatra"
|
26
|
+
spec.add_dependency 'http', "~> 2.2"
|
27
|
+
spec.add_dependency 'fusu', "~> 0.2.1"
|
28
|
+
spec.add_dependency 'miller'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wrappi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artur Pañach
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sinatra
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: http
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.2'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: fusu
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.2.1
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.2.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: miller
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: framework to create and standarize quickly API wrappers
|
112
|
+
email:
|
113
|
+
- arturictus@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/dev_server
|
128
|
+
- bin/setup
|
129
|
+
- lib/wrappi.rb
|
130
|
+
- lib/wrappi/client.rb
|
131
|
+
- lib/wrappi/endpoint.rb
|
132
|
+
- lib/wrappi/path_gen.rb
|
133
|
+
- lib/wrappi/request.rb
|
134
|
+
- lib/wrappi/request/get.rb
|
135
|
+
- lib/wrappi/request/template.rb
|
136
|
+
- lib/wrappi/request/with_body.rb
|
137
|
+
- lib/wrappi/response.rb
|
138
|
+
- lib/wrappi/version.rb
|
139
|
+
- wrappi.gemspec
|
140
|
+
homepage: https://github.com/arturictus/wrappi
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
metadata: {}
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
require_paths:
|
147
|
+
- lib
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
requirements: []
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 2.7.6
|
161
|
+
signing_key:
|
162
|
+
specification_version: 4
|
163
|
+
summary: framework to create and standarize quickly API wrappers
|
164
|
+
test_files: []
|