lib/her/api.rb in her-0.6.3 vs lib/her/api.rb in her-0.6.4

- old
+ new

@@ -4,13 +4,13 @@ class API # @private attr_reader :base_uri, :connection, :options # Setup a default API connection. Accepted arguments and options are the same as {API#setup}. - def self.setup(attrs={}, &block) + def self.setup(opts={}, &block) @default_api = new - @default_api.setup(attrs, &block) + @default_api.setup(opts, &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 # @@ -27,13 +27,13 @@ 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) + # @param [Hash] opts the Faraday options + # @option opts [String] :url The main HTTP API root (eg. `https://api.example.com`) + # @option opts [String] :ssl A hash containing [SSL options](https://github.com/technoweenie/faraday/wiki/Setting-up-SSL-certificates) # # @return Faraday::Connection # # @example Setting up the default API connection # Her::API.setup :url => "https://api.example" @@ -63,14 +63,14 @@ # 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={}, &blk) - attrs[:url] = attrs.delete(:base_uri) if attrs.include?(:base_uri) # Support legacy :base_uri option - @base_uri = attrs[:url] - @options = attrs + def setup(opts={}, &blk) + opts[:url] = opts.delete(:base_uri) if opts.include?(:base_uri) # Support legacy :base_uri option + @base_uri = opts[:url] + @options = opts @connection = Faraday.new(@options) do |connection| yield connection if block_given? end self end @@ -78,32 +78,32 @@ # 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. # # @private - def request(attrs={}) - method = attrs.delete(:_method) - path = attrs.delete(:_path) - headers = attrs.delete(:_headers) - attrs.delete_if { |key, value| key.to_s =~ /^_/ } # Remove all internal parameters + def request(opts={}) + method = opts.delete(:_method) + path = opts.delete(:_path) + headers = opts.delete(:_headers) + opts.delete_if { |key, value| key.to_s =~ /^_/ } # Remove all internal parameters response = @connection.send method do |request| request.headers.merge!(headers) if headers if method == :get # For GET requests, treat additional parameters as querystring data - request.url path, attrs + request.url path, opts else # For POST, PUT and DELETE requests, treat additional parameters as request body request.url path - request.body = attrs + request.body = opts end end { :parsed_data => response.env[:body], :response => response } end private # @private - def self.default_api(attrs={}) + def self.default_api(opts={}) defined?(@default_api) ? @default_api : nil end end end