README.md in api_client_base-0.1.0 vs README.md in api_client_base-0.2.0
- old
+ new
@@ -14,10 +14,38 @@
- Actions that your gem can perform are done through a class, like `APIWrapper::Client`.
- Each action has request and response classes.
- Request class takes care of appending the endpoint's path to the host, preparing params, and specifying the right http method to call
- Response class takes care of parsing the response and making the data easily accessible for consumption.
+### Configuring the gem's base module
+
+Do this:
+
+```ruby
+module MyGem
+ include APIClientBase::Base.module
+
+ with_configuration do
+ has :host, classes: String
+ has :username, classes: String
+ has :password, classes: String
+ end
+end
+```
+
+And you can
+
+- configure (thanks to [gem_config](https://github.com/krautcomputing/gem_config)) the gem's base module with defaults:
+
+```ruby
+MyGem.configure do |c|
+ c.host = "https://test.api.com"
+end
+```
+
+- instantiate `MyGem::Client` by calling `MyGem.new(host: "https://api.com", username: "user", password: "password")`. If you do not specify an option, it will use the gem's default.
+
### Configuring the `Client`
#### Default Options
```ruby
@@ -29,16 +57,16 @@
include APIClientBase::Client.module(default_opts: :default_opts)
private
def default_opts
+ # At least include `host`. Other things you may need to include: `token`, `secret`
{ host: HOST, secret: MyGem.configuration.secret }
end
end
class Client
- # You may also give a hash directly
HOST = "https://production.com"
include APIClientBase::Client.module(default_opts: {host: HOST})
end
end
```
@@ -59,11 +87,10 @@
# end
private
def all_opts
- # At least include `host`. Other things you may need to include: `token`, `secret`
{ host: "http://prod.com" }
end
end
end
```
@@ -73,21 +100,30 @@
You still need to create `MyGem::GetUserRequest` and `MyGem::GetUserResponse`. See the "requests" and "responses" section below.
#### Requests
+Requests assume a REST-like structure. This currently does not play well with a SOAP server. You could still use the `Base`, `Client`, and `Response` modules however. For SOAP APIs, write your own Request class that defines `#call`. This method needs to return the `raw_response`.
+
```ruby
module MyGem
class GetUserRequest
+ # You must install typhoeus if you use the `APIClientBase::Request`. Add it to your gemfile.
include APIClientBase::Request.module(
# you may define the action by `action: :post`. Defaults to `:get`.
# You may also opt to define `#default_action` (see below)
)
private
def path
- "/api/v1/users/#{self.user_id}"
+ # all occurrences of `/:\w+/` in the string will have the matches replaced with the
+ # request object's value of that method. For example, if `request.user_id` is 33
+ # then you will get "/api/v1/users/33" as the path
+ "/api/v1/users/:user_id"
+
+ # Or you can interpolate it yourself if you want
+ # "/api/v1/users/#{self.user_id}"
end
# Following methods are optional. Override them if you need to send something specific
def headers