lib/her/api.rb in her-0.4 vs lib/her/api.rb in her-0.4.1
- old
+ new
@@ -9,10 +9,26 @@
def self.setup(attrs={}, &block)
@@default_api = new
@@default_api.setup(attrs, &block)
end
+ # Create a new API object. This is useful to create multiple APIs and use them with the `uses_api` method.
+ # If your application uses only one API, you should use Her::API.setup to configure the default API
+ #
+ # @example Setting up a new API
+ # api = Her::API.new :url => "https://api.example" do |connection|
+ # connection.use Faraday::Request::UrlEncoded
+ # connection.use Her::Middleware::DefaultParseJSON
+ # end
+ #
+ # class User
+ # uses_api api
+ # end
+ def initialize(*args, &blk)
+ self.setup(*args, &blk)
+ end
+
# Setup the API connection.
#
# @param [Hash] attrs the Faraday options
# @option attrs [String] :url The main HTTP API root (eg. `https://api.example.com`)
# @option attrs [String] :ssl A hash containing [SSL options](https://github.com/technoweenie/faraday/wiki/Setting-up-SSL-certificates)
@@ -47,16 +63,17 @@
# Her::API.setup :url => "https://api.example.com" do |connection|
# connection.use Faraday::Request::UrlEncoded
# connection.use MyCustomParser
# connection.use Faraday::Adapter::NetHttp
# end
- def setup(attrs={})
+ def setup(attrs={}, &blk)
attrs[:url] = attrs.delete(:base_uri) if attrs.include?(:base_uri) # Support legacy :base_uri option
@base_uri = attrs[:url]
@options = attrs
@connection = Faraday.new(@options) do |connection|
yield connection if block_given?
end
+ self
end
# Define a custom parsing procedure. The procedure is passed the response object and is
# expected to return a hash with three keys: a main data Hash, an errors Hash
# and a metadata Hash.