api-blueprint 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +79 -0
- data/lib/api-blueprint/blueprint.rb +2 -1
- data/lib/api-blueprint/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d23a188a9076e091b9ce710bad84b2bafd27723
|
4
|
+
data.tar.gz: e3cd015fb4cc6b563113515cb8a00f1387fe4345
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dd0313e92b043c835515e9e970313c79d452167604560de4a988cd7a9aee672629412e990a16fc9c783039eba73bc05e8b50df3e0e735ba41143164a12500dd
|
7
|
+
data.tar.gz: 708aad91e765f722ad8c9fa34b47aa71887d92a3b04fa34a67564bb4d2fb1feecce368283e3e76c3cff47a95c8bd9db15179f70718ad9ddfe375f70f4ff8cb14
|
data/README.md
CHANGED
@@ -60,3 +60,82 @@ The result of using `api.run` on a blueprint is as you'd expect, nice model inst
|
|
60
60
|
<% end %>
|
61
61
|
</ul>
|
62
62
|
```
|
63
|
+
|
64
|
+
## Model Configuration
|
65
|
+
|
66
|
+
Using a `configure` block on models, you can define a default url (host), a [parser](#configparser), a [builder](#configbuilder) and can define a list of [replacements](#configreplacements):
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class AstronautsInSpace < ApiBlueprint::Model
|
70
|
+
configure do |config|
|
71
|
+
config.host = "http://api.open-notify.org"
|
72
|
+
config.parser = CustomResponseParser.new
|
73
|
+
config.builder = CustomObjectBuilder.new
|
74
|
+
config.replacements = {}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### Config.builder
|
80
|
+
|
81
|
+
When running a blueprint, after the response is returned and parsed, the result is passed to a builder, which is responsible for initializing objects from the response. The default [ApiBlueprint::Builder](https://github.com/iZettle/api-blueprint/blob/master/lib/api-blueprint/builder.rb)
|
82
|
+
will pass the attributes from the response into the initializer for the class the blueprint was defined in.
|
83
|
+
|
84
|
+
If you want to change the behavior of the builder, or have a complex response which needs manipulation before it should be passed to the initializer, you can define a custom builder. Custom builders must inherit from the default builder, and can override any combination of the core methods which are used to build responses; `build`, `prepare_item`, and `build_item`. Refer to the [default builder](https://github.com/iZettle/api-blueprint/blob/master/lib/api-blueprint/builder.rb) to see what those methods do.
|
85
|
+
|
86
|
+
### Config.parser
|
87
|
+
|
88
|
+
The parser is responsible for taking the raw response body string and generating a useful object from it, which can be passed into the builder to generate instances of the model. The [default parser](https://github.com/iZettle/api-blueprint/blob/master/lib/api-blueprint/parser.rb) is used to parse json strings and return a hash.
|
89
|
+
|
90
|
+
If you need a custom parser (for example, an XML parser), you must define a class which inherits from `ApiBlueprint::Parser`, and overrides the `#parse` method.
|
91
|
+
|
92
|
+
### Config.replacements
|
93
|
+
|
94
|
+
Replacements can be used to handle poorly named keys in api responses, or to re-word things without the need to creating a custom builder. For example, if the api by default returned a key called `numberOfAstronautsInSpace` and you wanted this to assign the `number` attribute on the model, you could use a replacement to handle that:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
config.replacements = {
|
98
|
+
numberOfAstronautsInSpace: :number
|
99
|
+
}
|
100
|
+
```
|
101
|
+
|
102
|
+
## Blueprint options
|
103
|
+
|
104
|
+
When defining a blueprint in a model, you can pass it a number of options to set request headers, params, body, or to run code after an instance of the model has been initialized. Here's some examples:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
# Most basic usage
|
108
|
+
blueprint :get, "/endpoint"
|
109
|
+
|
110
|
+
# Different http methods are supported (:get, :post, :put, :delete, :head, :patch, :options)
|
111
|
+
blueprint :put, "/endpoint"
|
112
|
+
|
113
|
+
# Request headers, body, or params can be passed along
|
114
|
+
blueprint :post, "/endpoint", {
|
115
|
+
headers: { "Content-Type": "application/json" },
|
116
|
+
params: { hello: "world" },
|
117
|
+
body: { something: "in the body" }
|
118
|
+
}
|
119
|
+
|
120
|
+
# If you need to modify the instance which will be returned, or run subsequent requests using
|
121
|
+
# the runner, you can do so in a block. Note, this is the only place the runner will be available
|
122
|
+
# when running the blueprint.
|
123
|
+
blueprint :get, "/endpoint" do |runner, result|
|
124
|
+
result.tap do |astronaut|
|
125
|
+
astronaut.number = 23 # override something
|
126
|
+
astronaut.more_info = runner.run SomeOtherModel.fetch # run another request
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
## A note on Dry::Struct immutability
|
132
|
+
|
133
|
+
Models you create use `Dry::Struct` to handle initialization and assignment. `Dry::Struct` is designed with immutability in mind, so if you need to mutate the objects you have, there are two possibilities; explicitly define an `attr_writer` for the attributes which you want to mutate, or do things the "Dry::Struct way" and use the current instance to initialize a new instance:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
astros = AstronautsInSpace.new number: 5, foo: "bar"
|
137
|
+
astros.number # => 5
|
138
|
+
astros.number = 10 # NoMethodError: undefined method `number=' for #<AstronautsInSpace number=5 foo="bar">
|
139
|
+
new_astros = astros.new number: 10
|
140
|
+
new_astros # #<AstronautsInSpace number=10 foo="bar">
|
141
|
+
```
|
@@ -47,7 +47,7 @@ module ApiBlueprint
|
|
47
47
|
def call_api(options)
|
48
48
|
connection.send options[:http_method] do |req|
|
49
49
|
req.url options[:url]
|
50
|
-
req.headers.merge! options[:headers]
|
50
|
+
req.headers.merge!({ "Content-Type": "application/json" }.merge(options[:headers]))
|
51
51
|
req.params = options[:params]
|
52
52
|
req.body = options[:body].to_json
|
53
53
|
end
|
@@ -56,6 +56,7 @@ module ApiBlueprint
|
|
56
56
|
def connection
|
57
57
|
Faraday.new do |conn|
|
58
58
|
conn.response :json, content_type: /\bjson$/
|
59
|
+
conn.response :raise_error
|
59
60
|
# conn.response :logger
|
60
61
|
|
61
62
|
conn.adapter Faraday.default_adapter
|