lib/shipcloud.rb in shipcloud-0.6.0 vs lib/shipcloud.rb in shipcloud-0.7.0

- old
+ new

@@ -1,21 +1,20 @@ require "net/http" require "net/https" require "json" require "shipcloud/version" +require "shipcloud/shipcloud_error" module Shipcloud API_VERSION = "v1" ROOT_PATH = File.dirname(__FILE__) API_HEADERS = { "Content-Type" => "application/json", "User-Agent" => "shipcloud-ruby v#{Shipcloud::VERSION}, API #{Shipcloud::API_VERSION}, #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}" } - @@api_key = nil - autoload :Base, "shipcloud/base" autoload :Shipment, "shipcloud/shipment" autoload :Carrier, "shipcloud/carrier" autoload :Address, "shipcloud/address" autoload :ShipmentQuote, "shipcloud/shipment_quote" @@ -31,17 +30,12 @@ module Request autoload :Base, "shipcloud/request/base" autoload :Connection, "shipcloud/request/connection" autoload :Info, "shipcloud/request/info" - autoload :Validator, "shipcloud/request/validator" end - class ShipcloudError < StandardError; end - class AuthenticationError < ShipcloudError; end - class APIError < ShipcloudError; end - class << self attr_accessor :configuration end def self.configuration @@ -73,20 +67,22 @@ # Sets the api key # # @param [String] api_key The api key def self.api_key=(api_key) - @@api_key = api_key configuration.api_key = api_key end # Makes a request against the shipcloud API # # @param [Symbol] http_method The http method to use, must be one of :get, :post, :put and :delete # @param [String] api_url The API url to use # @param [Hash] data The data to send, e.g. used when creating new objects. + # @param [String] optional api_key The api key. If no api key is given, Shipcloud.api_key will + # be used for the request # @return [Array] The parsed JSON response. - def self.request(http_method, api_url, data) - info = Request::Info.new(http_method, api_url, data) + def self.request(http_method, api_url, data, api_key: nil) + api_key ||= Shipcloud.api_key + info = Request::Info.new(http_method, api_url, api_key, data) Request::Base.new(info).perform end end