svelte 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/.yardopts +1 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +229 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/svelte.rb +37 -0
- data/lib/svelte/configuration.rb +20 -0
- data/lib/svelte/errors.rb +4 -0
- data/lib/svelte/errors/http_error.rb +17 -0
- data/lib/svelte/errors/json_error.rb +4 -0
- data/lib/svelte/errors/parameter_error.rb +5 -0
- data/lib/svelte/errors/version_error.rb +9 -0
- data/lib/svelte/generic_operation.rb +63 -0
- data/lib/svelte/model_factory.rb +171 -0
- data/lib/svelte/model_factory/parameter.rb +154 -0
- data/lib/svelte/operation.rb +31 -0
- data/lib/svelte/operation_builder.rb +47 -0
- data/lib/svelte/path.rb +41 -0
- data/lib/svelte/path_builder.rb +35 -0
- data/lib/svelte/rest_client.rb +44 -0
- data/lib/svelte/service.rb +38 -0
- data/lib/svelte/string_manipulator.rb +66 -0
- data/lib/svelte/swagger_builder.rb +94 -0
- data/lib/svelte/version.rb +4 -0
- data/svelte.gemspec +33 -0
- metadata +219 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 06bce912bdd4e0dcfa6b3eeca2633bd106a15e3b
|
4
|
+
data.tar.gz: c597344e514d2ac1bf1c4572eb0e154cf84d5c35
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a79f95dee772aec6ff511ef4ba14494b13e2e2fab0a0bf051e5f7d7443c4d6936df9385216e6ddb9156aa5afaa6ed03832eda189cb5287ec2821dfd7cc02700e
|
7
|
+
data.tar.gz: b40b5c6d145cef37558b1760e2c367a738f760aba323e70cf32ca57a1a44f81187ce04f60312209c5ca275debf4028707312741d4bd1a04e9136d345e6daedfa
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.4
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup=markdown
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Notonthehighstreet Enterprises Ltd
|
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,229 @@
|
|
1
|
+
# Svelte
|
2
|
+
|
3
|
+
Svelte is a Swagger-to-Ruby object mapper.
|
4
|
+
|
5
|
+
It reads a Swagger specification file in JSON, and automatically generates Resource Classes with static methods to represent the various HTTP endpoints.
|
6
|
+
|
7
|
+
[![Build Status](https://secure.travis-ci.org/savonrb/savon.png?branch=master)](http://travis-ci.org/savonrb/savon)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/notonthehighstreet/svelte/badges/gpa.svg)](https://codeclimate.com/github/notonthehighstreet/svelte)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "svelte"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install svelte
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Point a service at an actual API spec.
|
29
|
+
|
30
|
+
You may pass in a URL pointing to a Swagger spec or the JSON directly:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
service = Svelte::Service.create(url: "http://path/to/swagger/spec/resource.json", module_name: 'PetStore')
|
34
|
+
service = Svelte::Service.create(json: "{ <JSON here> }", module_name: 'PetStore')
|
35
|
+
```
|
36
|
+
|
37
|
+
This will build a dynamically generated client on top of `Svelte::Service::PetStore`.
|
38
|
+
|
39
|
+
The structure of the new module will be based on the API paths and their respective operations.
|
40
|
+
Let's look at an example. Using the complete PetStore spec, we can find the following path:
|
41
|
+
|
42
|
+
```json
|
43
|
+
"/pet/findByStatus": {
|
44
|
+
"get": {
|
45
|
+
"tags": [
|
46
|
+
"pet"
|
47
|
+
],
|
48
|
+
"summary": "Finds Pets by status",
|
49
|
+
"description": "Multiple status values can be provided with comma separated strings",
|
50
|
+
"operationId": "findPetsByStatus",
|
51
|
+
"produces": [
|
52
|
+
"application/xml",
|
53
|
+
"application/json"
|
54
|
+
],
|
55
|
+
"parameters": [
|
56
|
+
{
|
57
|
+
"name": "status",
|
58
|
+
"in": "query",
|
59
|
+
"description": "Status values that need to be considered for filter",
|
60
|
+
"required": true,
|
61
|
+
"type": "array",
|
62
|
+
"items": {
|
63
|
+
"type": "string",
|
64
|
+
"enum": [
|
65
|
+
"available",
|
66
|
+
"pending",
|
67
|
+
"sold"
|
68
|
+
],
|
69
|
+
"default": "available"
|
70
|
+
},
|
71
|
+
"collectionFormat": "multi"
|
72
|
+
}
|
73
|
+
],
|
74
|
+
"responses": {
|
75
|
+
"200": {
|
76
|
+
"description": "successful operation",
|
77
|
+
"schema": {
|
78
|
+
"type": "array",
|
79
|
+
"items": {
|
80
|
+
"$ref": "#/definitions/Pet"
|
81
|
+
}
|
82
|
+
}
|
83
|
+
},
|
84
|
+
"400": {
|
85
|
+
"description": "Invalid status value"
|
86
|
+
}
|
87
|
+
},
|
88
|
+
"security": [
|
89
|
+
{
|
90
|
+
"petstore_auth": [
|
91
|
+
"write:pets",
|
92
|
+
"read:pets"
|
93
|
+
]
|
94
|
+
}
|
95
|
+
]
|
96
|
+
}
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
The path contains two parts: `pet` and `findByStatus`. This will generate
|
101
|
+
the following hierarchy in the new module:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
Svelte::Service::PetStore::Pet::FindByStatus
|
105
|
+
```
|
106
|
+
|
107
|
+
We can see the path has one `get` operation. A method will be generated in the
|
108
|
+
`FindByStatus` module based on the `operationId` Swagger attribute, which will
|
109
|
+
have the following signature:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
Svelte::Service::PetStore::Pet::FindByStatus.find_pets_by_status(
|
113
|
+
request_payload,
|
114
|
+
request_options = {}
|
115
|
+
)
|
116
|
+
```
|
117
|
+
|
118
|
+
Where `request_payload` is a `Hash` representing the parameters of the operation
|
119
|
+
and `request_options`, defaulting to an empty `Hash`, will be a `Hash` of
|
120
|
+
options to pass to the request.
|
121
|
+
|
122
|
+
In our case, the parameters would look like this:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
request_parameters = {
|
126
|
+
status: ['available', 'pending']
|
127
|
+
}
|
128
|
+
```
|
129
|
+
|
130
|
+
### Responses
|
131
|
+
|
132
|
+
Svelte will return a [`Faraday::Request`](http://www.rubydoc.info/gems/faraday/0.9.1/Faraday/Response) object as a response to a call.
|
133
|
+
|
134
|
+
### Models
|
135
|
+
|
136
|
+
Svelte also provides generators for Swagger models. They allow an easy way
|
137
|
+
to programmatically create and validate requests.
|
138
|
+
They also provide an `as_json` that will generate a valid json body for
|
139
|
+
a given request.
|
140
|
+
|
141
|
+
Consider the definitions key of this Swagger model:
|
142
|
+
|
143
|
+
```json
|
144
|
+
{
|
145
|
+
"definitions": {
|
146
|
+
"MoneyView": {
|
147
|
+
"id": "MoneyView",
|
148
|
+
"description": "",
|
149
|
+
"required": [
|
150
|
+
"amount",
|
151
|
+
"currencyCode"
|
152
|
+
],
|
153
|
+
"extends": "",
|
154
|
+
"properties": {
|
155
|
+
"amount": {
|
156
|
+
"type": "number",
|
157
|
+
"format": "double",
|
158
|
+
"description": "Decimal amount"
|
159
|
+
},
|
160
|
+
"currencyCode": {
|
161
|
+
"type": "string",
|
162
|
+
"description": "ISO 3 letter currency code"
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
```
|
169
|
+
|
170
|
+
In Svelte you can generate the ruby mapper like this:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
class MoneyRequest
|
174
|
+
extend Svelte::ModelFactory
|
175
|
+
define_models_from_file(path_to_models_json_file)
|
176
|
+
end
|
177
|
+
|
178
|
+
view = MoneyRequest::MoneyView.new
|
179
|
+
view.valid? # false
|
180
|
+
view.validate # {"currencyCode"=>"Invalid parameter: Missing required parameter", "amount"=>"Invalid parameter: Missing required parameter"}
|
181
|
+
view.currencyCode = "GBP"
|
182
|
+
view.amount = 40.00
|
183
|
+
view.valid? # true
|
184
|
+
view.as_json # {:currencyCode=>"GBP", :amount=>40.0}
|
185
|
+
```
|
186
|
+
|
187
|
+
### Options
|
188
|
+
|
189
|
+
You can specify a timeout option on a per request basis. If the request times out a `Svelte::TimeoutError` exception
|
190
|
+
will be raised.
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
begin
|
194
|
+
Svelte::Service::PetStore::Pet::FindByStatus.find_pets_by_status(request.as_json, { timeout: 10 })
|
195
|
+
rescue Svelte::TimeoutError => e
|
196
|
+
handle_timeout_error(e)
|
197
|
+
end
|
198
|
+
```
|
199
|
+
|
200
|
+
## Limitations
|
201
|
+
|
202
|
+
Svelte is still a work in progress gem and it lacks some features that will be
|
203
|
+
implemented in the future. Feel free to request or comment on what you'd like
|
204
|
+
to see supported. Here is a non exhaustive list of the pitfalls we've identified
|
205
|
+
so far:
|
206
|
+
|
207
|
+
* Supports `application/json` request and response types only
|
208
|
+
* API calls return a raw [Faraday::Response] objects. We'll support returning
|
209
|
+
dynamically generated model responses based on the Swagger spec response
|
210
|
+
schema
|
211
|
+
* Request parameter validation is only done for url based parameters.
|
212
|
+
It'd be possible to add validations to all parameters of the request.
|
213
|
+
In fact the `ModelFactory` already provides that functionality, but it
|
214
|
+
requires the client to call `valid?` on the requests to perform the
|
215
|
+
validation. This should happen automatically
|
216
|
+
|
217
|
+
## Development
|
218
|
+
|
219
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
220
|
+
|
221
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
222
|
+
|
223
|
+
## Contributing
|
224
|
+
|
225
|
+
1. Fork it ( https://github.com/notonthehighstreet/svelte/fork )
|
226
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
227
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
228
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
229
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'svelte'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/svelte.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'svelte/version'
|
3
|
+
require 'svelte/string_manipulator'
|
4
|
+
require 'svelte/configuration'
|
5
|
+
require 'svelte/errors'
|
6
|
+
require 'svelte/model_factory'
|
7
|
+
require 'svelte/rest_client'
|
8
|
+
require 'svelte/service'
|
9
|
+
require 'svelte/swagger_builder'
|
10
|
+
require 'svelte/path'
|
11
|
+
require 'svelte/operation'
|
12
|
+
require 'svelte/path_builder'
|
13
|
+
require 'svelte/operation_builder'
|
14
|
+
require 'svelte/generic_operation'
|
15
|
+
|
16
|
+
# Svelte is a sleek Ruby API Client generated from a Swagger spec.
|
17
|
+
#
|
18
|
+
# You can hand it a spec which defines an path like `/api/comments/{id}` that
|
19
|
+
# supports a series of operations like `get`, `post`, `delete`, and a module
|
20
|
+
# name you want built (i.e. `Comments`), and it will hand you a
|
21
|
+
# `Svelte::Service::Comments` object that can be used like so:
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# Svelte::Service::Comments::Api::Comments.get_comment_by_id(id: 10)
|
25
|
+
# Svelte::Service::Comments::Api::Comments.create_comment(contents: 'nice post!')
|
26
|
+
# Svelte::Service::Comments::Api::Comments.delete_comment_by_id(id: 10)
|
27
|
+
module Svelte
|
28
|
+
# @param url [String] url pointing to a Swagger spec
|
29
|
+
# @param json [String] the entire Swagger spec as a String
|
30
|
+
# @param module_name [String] constant name where you want Svelte to build
|
31
|
+
# the new functionality on top of
|
32
|
+
# @note Either `url` or `json` need to be provided. `url` will take
|
33
|
+
# precedence over `json`
|
34
|
+
def self.create(url: nil, json: nil, module_name:, options: {})
|
35
|
+
Service.create(url: url, json: json, module_name: module_name, options: options)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Svelte
|
2
|
+
# Holds miscelanious configuration options for the current
|
3
|
+
# Swagger API specification
|
4
|
+
class Configuration
|
5
|
+
attr_reader :host, :base_path, :protocol
|
6
|
+
# Creates a new Configuration instance
|
7
|
+
# @param options [Hash] configuration options
|
8
|
+
def initialize(options:)
|
9
|
+
@host = options[:host]
|
10
|
+
@base_path = options[:base_path]
|
11
|
+
@protocol = options[:protocol] || default_protocol
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def default_protocol
|
17
|
+
'http'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Svelte
|
2
|
+
# Svelte error class to represent networking errors
|
3
|
+
# It can be customized by passing a specific errror message and
|
4
|
+
# a parent exception, which will contain the http driver specific
|
5
|
+
# error that generated it
|
6
|
+
class HTTPError < StandardError
|
7
|
+
attr_reader :parent
|
8
|
+
|
9
|
+
# Creates a new HTTPError with a message and a parent error
|
10
|
+
# @param message [String] exception message
|
11
|
+
# @param parent the parent exception
|
12
|
+
def initialize(message: nil, parent: nil)
|
13
|
+
@parent = parent
|
14
|
+
super(message || (parent && parent.message))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|