# Generated by API Builder - https://www.apibuilder.io # Service version: 0.2.6 # User agent: apibuilder app.apibuilder.io/bombas/storeback-api/0.2.6/ruby_client require 'cgi' require 'net/http' require 'net/https' require 'uri' require 'base64' require 'date' require 'rubygems' require 'json' require 'bigdecimal' # storeback-api is a test repo to poof of concept of PDP resource serving. module Bombas module Storeback module Api module V0 class Client module Constants BASE_URL = 'https://bombas-storeback-api.global.ssl.fastly.net/v1' unless defined?(Constants::BASE_URL) NAMESPACE = 'bombas.storeback.api.v0' unless defined?(Constants::NAMESPACE) USER_AGENT = 'apibuilder app.apibuilder.io/bombas/storeback-api/0.2.6/ruby_client' unless defined?(Constants::USER_AGENT) VERSION = '0.2.6' unless defined?(Constants::VERSION) VERSION_MAJOR = 0 unless defined?(VERSION_MAJOR) end attr_reader :url def initialize(url, opts={}) @url = HttpClient::Preconditions.assert_class('url', url, String) @base_url = URI(url) @authorization = HttpClient::Preconditions.assert_class_or_nil('authorization', opts.delete(:authorization), HttpClient::Authorization) @default_headers = HttpClient::Preconditions.assert_class('default_headers', opts.delete(:default_headers) || {}, Hash) @http_handler = opts.delete(:http_handler) || HttpClient::DefaultHttpHandler.new HttpClient::Preconditions.assert_empty_opts(opts) HttpClient::Preconditions.check_state(url.match(/http.+/i), "URL[%s] must start with http" % url) end # Creates an instance of the client using the base url specified in the API spec. def Client.at_base_url(opts={}) Client.new(Constants::BASE_URL, opts) end def request(path=nil) HttpClient::Preconditions.assert_class_or_nil('path', path, String) request = HttpClient::Request.new(@http_handler, @base_url, path.to_s).with_header('User-Agent', Constants::USER_AGENT).with_header('X-Apidoc-Version', Constants::VERSION).with_header('X-Apidoc-Version-Major', Constants::VERSION_MAJOR) @default_headers.each do |key, value| request = request.with_header(key, value) end if @authorization request = request.with_auth(@authorization) end request end def products @products ||= ::Bombas::Storeback::Api::V0::Clients::Products.new(self) end end module Clients class Products def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Bombas::Storeback::Api::V0::Client) end # Returns a product by ID or by handle. # The following API will return a product object sanitized of private # information from shopify. It should be thought of as a serializer or # alturnative interface to the public shopify information per a product. def get(product_id) HttpClient::Preconditions.assert_class('product_id', product_id, String) r = @client.request("/v1/products/#{CGI.escape(product_id)}").get ::Bombas::Storeback::Api::V0::Models::Product.new(r) end end end module Models class Product attr_reader :id, :title, :handle, :description, :description_html, :item_count, :product_type, :gender, :colors, :sizes, :images, :variants, :breadcrumbs, :details, :created_at, :updated_at def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Integer)) @title = (x = opts.delete(:title); x.nil? ? nil : HttpClient::Preconditions.assert_class('title', x, String)) @handle = (x = opts.delete(:handle); x.nil? ? nil : HttpClient::Preconditions.assert_class('handle', x, String)) @description = (x = opts.delete(:description); x.nil? ? nil : HttpClient::Preconditions.assert_class('description', x, String)) @description_html = (x = opts.delete(:description_html); x.nil? ? nil : HttpClient::Preconditions.assert_class('description_html', x, String)) @item_count = (x = opts.delete(:item_count); x.nil? ? nil : HttpClient::Preconditions.assert_class('item_count', x, Integer)) @product_type = (x = opts.delete(:product_type); x.nil? ? nil : HttpClient::Preconditions.assert_class('product_type', x, String)) @gender = (x = opts.delete(:gender); x.nil? ? nil : HttpClient::Preconditions.assert_class('gender', x, String)) @colors = (x = opts.delete(:colors); x.nil? ? nil : HttpClient::Preconditions.assert_class('colors', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Color) ? x : ::Bombas::Storeback::Api::V0::Models::Color.new(x)) }) @sizes = (x = opts.delete(:sizes); x.nil? ? nil : HttpClient::Preconditions.assert_class('sizes', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Size) ? x : ::Bombas::Storeback::Api::V0::Models::Size.new(x)) }) @images = (x = opts.delete(:images); x.nil? ? nil : HttpClient::Preconditions.assert_class('images', x, Array).map { |v| HttpClient::Preconditions.assert_class('images', HttpClient::Helper.to_object(v), Hash) }) @variants = (x = opts.delete(:variants); x.nil? ? nil : HttpClient::Preconditions.assert_class('variants', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Variant) ? x : ::Bombas::Storeback::Api::V0::Models::Variant.new(x)) }) @breadcrumbs = (x = opts.delete(:breadcrumbs); x.nil? ? nil : HttpClient::Preconditions.assert_class('breadcrumbs', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Breadcrumb) ? x : ::Bombas::Storeback::Api::V0::Models::Breadcrumb.new(x)) }) @details = (x = opts.delete(:details); x.nil? ? nil : HttpClient::Preconditions.assert_class('details', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Detail) ? x : ::Bombas::Storeback::Api::V0::Models::Detail.new(x)) }) @created_at = (x = opts.delete(:created_at); x.nil? ? nil : HttpClient::Preconditions.assert_class('created_at', x, String)) @updated_at = (x = opts.delete(:updated_at); x.nil? ? nil : HttpClient::Preconditions.assert_class('updated_at', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Product.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :title => title, :handle => handle, :description => description, :description_html => description_html, :item_count => item_count, :product_type => product_type, :gender => gender, :colors => colors.nil? ? nil : colors.map { |o| o.to_hash }, :sizes => sizes.nil? ? nil : sizes.map { |o| o.to_hash }, :images => images.nil? ? nil : images, :variants => variants.nil? ? nil : variants.map { |o| o.to_hash }, :breadcrumbs => breadcrumbs.nil? ? nil : breadcrumbs.map { |o| o.to_hash }, :details => details.nil? ? nil : details.map { |o| o.to_hash }, :created_at => created_at, :updated_at => updated_at } end end # A particular size options for a product class Size attr_reader :name, :values def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @values = (x = opts.delete(:values); x.nil? ? nil : HttpClient::Preconditions.assert_class('values', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::SizeValues) ? x : ::Bombas::Storeback::Api::V0::Models::SizeValues.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Size.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :name => name, :values => values.nil? ? nil : values.map { |o| o.to_hash } } end end class SizeValues attr_reader :id, :name, :guidance def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @guidance = (x = opts.delete(:guidance); x.nil? ? nil : HttpClient::Preconditions.assert_class('guidance', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) SizeValues.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :name => name, :guidance => guidance } end end class Color attr_reader :id, :position, :name, :fill def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, String)) @position = (x = opts.delete(:position); x.nil? ? nil : HttpClient::Preconditions.assert_class('position', x, Integer)) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @fill = (x = opts.delete(:fill); x.nil? ? nil : HttpClient::Preconditions.assert_class('fill', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Color.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :position => position, :name => name, :fill => fill } end end class Variant attr_reader :id, :product_id, :sku, :title, :is_available, :price, :shipping, :created_at, :updated_at, :sizes def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Integer)) @product_id = (x = opts.delete(:product_id); x.nil? ? nil : HttpClient::Preconditions.assert_class('product_id', x, Integer)) @sku = (x = opts.delete(:sku); x.nil? ? nil : HttpClient::Preconditions.assert_class('sku', x, String)) @title = (x = opts.delete(:title); x.nil? ? nil : HttpClient::Preconditions.assert_class('title', x, String)) @is_available = (x = opts.delete(:is_available); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('is_available', x)) @price = (x = opts.delete(:price); x.nil? ? nil : (x = x; x.is_a?(::Bombas::Storeback::Api::V0::Models::Price) ? x : ::Bombas::Storeback::Api::V0::Models::Price.new(x))) @shipping = (x = opts.delete(:shipping); x.nil? ? nil : (x = x; x.is_a?(::Bombas::Storeback::Api::V0::Models::Shipping) ? x : ::Bombas::Storeback::Api::V0::Models::Shipping.new(x))) @created_at = (x = opts.delete(:created_at); x.nil? ? nil : HttpClient::Preconditions.assert_class('created_at', x, String)) @updated_at = (x = opts.delete(:updated_at); x.nil? ? nil : HttpClient::Preconditions.assert_class('updated_at', x, String)) @sizes = (x = opts.delete(:sizes); x.nil? ? nil : HttpClient::Preconditions.assert_class('sizes', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Size) ? x : ::Bombas::Storeback::Api::V0::Models::Size.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Variant.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :product_id => product_id, :sku => sku, :title => title, :is_available => is_available, :price => price.nil? ? nil : price.to_hash, :shipping => shipping.nil? ? nil : shipping.to_hash, :created_at => created_at, :updated_at => updated_at, :sizes => sizes.nil? ? nil : sizes.map { |o| o.to_hash } } end end class Shipping attr_reader :requires_shipping, :grams, :weight, :weight_unit def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @requires_shipping = (x = opts.delete(:requires_shipping); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('requires_shipping', x)) @grams = (x = opts.delete(:grams); x.nil? ? nil : HttpClient::Preconditions.assert_class('grams', x, Integer)) @weight = (x = opts.delete(:weight); x.nil? ? nil : HttpClient::Preconditions.assert_class('weight', x, Integer)) @weight_unit = (x = opts.delete(:weight_unit); x.nil? ? nil : HttpClient::Preconditions.assert_class('weight_unit', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Shipping.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :requires_shipping => requires_shipping, :grams => grams, :weight => weight, :weight_unit => weight_unit } end end class Breadcrumb attr_reader :name, :link, :position def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @name = (x = opts.delete(:name); x.nil? ? nil : HttpClient::Preconditions.assert_class('name', x, String)) @link = (x = opts.delete(:link); x.nil? ? nil : HttpClient::Preconditions.assert_class('link', x, String)) @position = (x = opts.delete(:position); x.nil? ? nil : HttpClient::Preconditions.assert_class('position', x, Integer)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Breadcrumb.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :name => name, :link => link, :position => position } end end class Image attr_reader :id, :position, :alt, :src def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @id = (x = opts.delete(:id); x.nil? ? nil : HttpClient::Preconditions.assert_class('id', x, Integer)) @position = (x = opts.delete(:position); x.nil? ? nil : HttpClient::Preconditions.assert_class('position', x, Integer)) @alt = (x = opts.delete(:alt); x.nil? ? nil : HttpClient::Preconditions.assert_class('alt', x, String)) @src = (x = opts.delete(:src); x.nil? ? nil : HttpClient::Preconditions.assert_class('src', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Image.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :id => id, :position => position, :alt => alt, :src => src } end end class Price attr_reader :msrp, :price, :isDiscounted, :percentOff def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @msrp = (x = opts.delete(:msrp); x.nil? ? nil : HttpClient::Preconditions.assert_class('msrp', x, String)) @price = (x = opts.delete(:price); x.nil? ? nil : HttpClient::Preconditions.assert_class('price', x, String)) @isDiscounted = (x = opts.delete(:isDiscounted); x.nil? ? nil : HttpClient::Preconditions.assert_boolean('isDiscounted', x)) @percentOff = (x = opts.delete(:percentOff); x.nil? ? nil : HttpClient::Preconditions.assert_class('percentOff', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Price.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :msrp => msrp, :price => price, :isDiscounted => isDiscounted, :percentOff => percentOff } end end class Detail attr_reader :icon_handle, :title, :tech def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @icon_handle = (x = opts.delete(:icon_handle); x.nil? ? nil : HttpClient::Preconditions.assert_class('icon_handle', x, String)) @title = (x = opts.delete(:title); x.nil? ? nil : HttpClient::Preconditions.assert_class('title', x, String)) @tech = (x = opts.delete(:tech); x.nil? ? nil : HttpClient::Preconditions.assert_class('tech', x, Array).map { |v| (x = v; x.is_a?(::Bombas::Storeback::Api::V0::Models::Tech) ? x : ::Bombas::Storeback::Api::V0::Models::Tech.new(x)) }) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Detail.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :icon_handle => icon_handle, :title => title, :tech => tech.nil? ? nil : tech.map { |o| o.to_hash } } end end class Tech attr_reader :title, :handle, :content def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @title = (x = opts.delete(:title); x.nil? ? nil : HttpClient::Preconditions.assert_class('title', x, String)) @handle = (x = opts.delete(:handle); x.nil? ? nil : HttpClient::Preconditions.assert_class('handle', x, String)) @content = (x = opts.delete(:content); x.nil? ? nil : HttpClient::Preconditions.assert_class('content', x, String)) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Tech.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :title => title, :handle => handle, :content => content } end end end # ===== END OF SERVICE DEFINITION ===== module HttpClient class HttpHandler # Returns a client instance to use # # @param base_uri The base URI for this API # @param path the Requested full http path (including any query strings) def instance(base_uri, path) raise "Override in subclass" end end class HttpHandlerInstance # Executes a request. The provided request object will be an # instance of Net::HTTP (e.g. Net::HTTP::Get) def execute(request) raise "Override in subclass" end end class DefaultHttpHandler < HttpHandler def instance(base_uri, path) DefaultHttpHandlerInstance.new(base_uri) end end class DefaultHttpHandlerInstance < HttpHandlerInstance attr_reader :client def initialize(base_uri) @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @client = Net::HTTP.new(@base_uri.host, @base_uri.port) if @base_uri.scheme == "https" configure_ssl end end def execute(request) response = begin @client.request(request) rescue SocketError => e raise StandardError.new("Error accessing uri[#{full_uri(request.path)}]: #{e}") end case response when Net::HTTPSuccess response.body else body = response.body rescue nil raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => full_uri(request.path).to_s) end end def full_uri(path) path.start_with?(@base_uri.to_s) ? path : File.join(@base_uri.to_s, path) end # Called to configure SSL if the base uri requires it def configure_ssl @client.use_ssl = true @client.verify_mode = OpenSSL::SSL::VERIFY_PEER @client.cert_store = OpenSSL::X509::Store.new @client.cert_store.set_default_paths end end class Request attr_reader :base_uri, :path, :full_uri def initialize(http_handler, base_uri, path) @http_handler = http_handler @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @path = Preconditions.assert_class('path', path, String) @full_uri = @base_uri.to_s + @path @params = nil @body = nil @auth = nil @headers = {} @header_keys_lower_case = [] end def with_header(name, value) Preconditions.check_not_blank('name', name, "Header name is required") Preconditions.check_not_blank('value', value, "Header value is required") Preconditions.check_state(!@headers.has_key?(name), "Duplicate header named[%s]" % name) @headers[name] = value @header_keys_lower_case << name.downcase self end def with_auth(auth) Preconditions.assert_class('auth', auth, HttpClient::Authorization) Preconditions.check_state(@auth.nil?, "auth previously set") if auth.scheme.name == AuthScheme::BASIC.name @auth = auth else raise "Auth Scheme[#{auth.scheme.name}] not supported" end self end def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end # Wrapper to set Content-Type header to application/json and set # the provided json document as the body def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end def get(&block) do_request(Net::HTTP::Get, &block) end def delete(&block) do_request(Net::HTTP::Delete, &block) end def options(&block) do_request(Net::HTTP::Options, &block) end def post(&block) do_request(Net::HTTP::Post, &block) end def put(&block) do_request(Net::HTTP::Put, &block) end class PATCH < Net::HTTP::Put METHOD = "PATCH" end def patch(&block) do_request(PATCH, &block) end def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @full_uri.dup if q = to_query(@params) uri += "?%s" % q end request = klass.send(:new, uri) # DEBUG curl = ['curl'] # DEBUG if klass != Net::HTTP::Get # DEBUG curl << "-X%s" % klass.name.split("::").last.upcase # DEBUG end if @body # DEBUG tmpfile = "/tmp/rest_client.tmp" # DEBUG File.open(tmpfile, "w") { |os| os << @body.to_s } # DEBUG curl << "-d@%s" % tmpfile request.body = @body end if @auth # DEBUG curl << "-u \"%s:%s\"" % [@auth.username, @auth.password] Preconditions.check_state(!@header_keys_lower_case.include?("authorization"), "Cannot specify both an Authorization header and an auth instance") user_pass = "%s:%s" % [@auth.username, @auth.password] encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join request.add_field("Authorization", "Basic %s" % encoded) end @headers.each { |key, value| # DEBUG curl << "-H \"%s: %s\"" % [key, value] request.add_field(key, value) } # DEBUG curl << "'%s%s'" % [@base_uri, path] # DEBUG puts curl.join(" ") raw_response = @http_handler.instance(@base_uri, request.path).execute(request) response = raw_response.to_s == "" ? nil : JSON.parse(raw_response) if block_given? yield response else response end end private def to_query(params={}) parts = (params || {}).map { |k,v| if v.is_a?(Enumerable) v.map { |el| "%s=%s" % [k, CGI.escape(el.to_s)] } else "%s=%s" % [k, CGI.escape(v.to_s)] end } parts.empty? ? nil : parts.join("&") end end class ServerError < StandardError attr_reader :code, :details, :body, :uri def initialize(code, details, incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @code = HttpClient::Preconditions.assert_class('code', code, Integer) @details = HttpClient::Preconditions.assert_class('details', details, String) @body = HttpClient::Preconditions.assert_class_or_nil('body', opts.delete(:body), String) @uri = HttpClient::Preconditions.assert_class_or_nil('uri', opts.delete(:uri), String) HttpClient::Preconditions.assert_empty_opts(opts) super(self.message) end def message m = "%s %s" % [@code, @details] if @body m << ": %s" % @body end m end def body_json @body ? JSON.parse(@body) : nil end end class PreconditionException < StandardError attr_reader :message def initialize(message) super(message) @message = message end end module Preconditions def Preconditions.check_argument(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_argument failed") end nil end def Preconditions.check_state(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_state failed") end nil end def Preconditions.check_not_nil(field_name, reference, error_message=nil) if reference.nil? raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name) end reference end def Preconditions.check_not_blank(field_name, reference, error_message=nil) if reference.to_s.strip == "" raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name) end reference end # Throws an error if opts is not empty. Useful when parsing # arguments to a function def Preconditions.assert_empty_opts(opts) if !opts.empty? raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}") end end # Requires that the provided hash has the specified keys. # @param fields A list of symbols def Preconditions.require_keys(hash, fields, error_prefix=nil) missing = fields.select { |f| !hash.has_key?(f) } if !missing.empty? msg = "Missing required fields: " + missing.join(", ") raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}") end end # Asserts that value is not nill and is_?(klass). Returns # value. Common use is # # amount = Preconditions.assert_class('amount', amount, BigDecimal) def Preconditions.assert_class(field_name, value, klass) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('klass', klass) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name]) Preconditions.check_state(value.is_a?(klass), "Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_class_or_nil(field_name, value, klass) if !value.nil? Preconditions.assert_class(field_name, value, klass) end end def Preconditions.assert_boolean(field_name, value) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name) Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass), "Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_boolean_or_nil(field_name, value) if !value.nil? Preconditions.assert_boolean(field_name, value) end end def Preconditions.assert_collection_of_class(field_name, values, klass) Preconditions.assert_class(field_name, values, Array) values.each { |v| Preconditions.assert_class(field_name, v, klass) } end def Preconditions.assert_hash_of_class(field_name, hash, klass) Preconditions.assert_class(field_name, hash, Hash) values.each { |k, v| Preconditions.assert_class(field_name, v, klass) } end end class AuthScheme attr_reader :name def initialize(name) @name = HttpClient::Preconditions.check_not_blank('name', name) end BASIC = AuthScheme.new("basic") unless defined?(BASIC) end class Authorization attr_reader :scheme, :username, :password def initialize(scheme, username, opts={}) @scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme) @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required") @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String) HttpClient::Preconditions.assert_empty_opts(opts) end def Authorization.basic(username, password=nil) Authorization.new(AuthScheme::BASIC, username, :password => password) end end module Helper def Helper.symbolize_keys(hash) Preconditions.assert_class('hash', hash, Hash) new_hash = {} hash.each { |k, v| new_hash[k.to_sym] = v } new_hash end def Helper.to_big_decimal(value) value ? BigDecimal(value.to_s) : nil end def Helper.to_object(value) value ? (value.is_a?(Hash) ? value : JSON.parse(value)) : nil end def Helper.to_uuid(value) Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/), "Invalid guid[%s]" % value) value end def Helper.to_date_iso8601(value) if value.is_a?(Date) value elsif value Date.parse(value.to_s) else nil end end def Helper.to_date_time_iso8601(value) if value.is_a?(DateTime) value elsif value DateTime.parse(value.to_s) else nil end end def Helper.date_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%d') end def Helper.date_time_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z') end TRUE_STRINGS = ['t', 'true', 'y', 'yes', 'on', '1', 'trueclass'] unless defined?(TRUE_STRINGS) FALSE_STRINGS = ['f', 'false', 'n', 'no', 'off', '0', 'falseclass'] unless defined?(FALSE_STRINGS) def Helper.to_boolean(field_name, value) string = value.to_s.strip.downcase if TRUE_STRINGS.include?(string) true elsif FALSE_STRINGS.include?(string) false elsif string != "" raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}") else nil end end end end end end end end