class VersionOne::Client

Constants

XML_CONTENT_TYPE

Public Class Methods

service_uri() click to toggle source
# File lib/version-one/client.rb, line 61
def self.service_uri
  @service_uri ||= get_service_uri
end

Private Class Methods

get_service_uri() click to toggle source
# File lib/version-one/client.rb, line 71
def self.get_service_uri
  raise "VersionOne service_uri must be configured" unless VersionOne.config.service_uri
  URI.parse(VersionOne.config.service_uri)
end

Public Instance Methods

cache_store() click to toggle source
# File lib/version-one/client.rb, line 53
def cache_store
  VersionOne.cache
end
can_cache?() click to toggle source
# File lib/version-one/client.rb, line 57
def can_cache?
  !!cache_store
end
get(path, *fields) click to toggle source
# File lib/version-one/client.rb, line 10
def get(path, *fields)
  uri = path_uri(path)
  options = fields.last.is_a?(Hash) ? fields.pop : {}

  unless fields.empty?
    fields.concat(Query::REQUIRED_FIELDS)
    uri.query = "sel=#{fields.join(',')}"
  end

  get_uri uri, options
end
get_uri(uri, options={}) click to toggle source
# File lib/version-one/client.rb, line 49
def get_uri(uri, options={})
  request :get, uri, options
end
post(path, query={}, &block) click to toggle source
# File lib/version-one/client.rb, line 32
def post(path, query={}, &block)
  uri = path_uri(path)
  if query && !query.empty?
    uri.query = URI.encode_www_form(query)
  end

  if block_given?
    request :post, uri, &block
  else
    request :post, uri do |r|
      r.body = ''
      r.content_type = XML_CONTENT_TYPE
    end
  end

end
post_xml(path, xml) click to toggle source
# File lib/version-one/client.rb, line 22
def post_xml(path, xml)
  uri = path_uri(path)
  xml = xml.root if xml.respond_to?(:root)
  xml.attributes['href'] = uri.path
  request :post, uri do |r|
    r.body = xml.to_s
    r.content_type = XML_CONTENT_TYPE
  end
end
service_uri() click to toggle source
# File lib/version-one/client.rb, line 65
def service_uri
  self.class.service_uri
end

Private Instance Methods

cached_request(type, uri, options={}) click to toggle source
# File lib/version-one/client.rb, line 95
def cached_request(type, uri, options={})
  cache_options = options[:cache]
  xml = nil

  if can_cache? && cache_options
    xml = cache_store.read(cache_options[:key])

    if xml
      xml = XML::Document.string(xml).root
      VersionOne.logger.debug('%s CACHE %s' % [type.to_s.upcase, uri.to_s])
    end
  end

  xml ||= uncached_request type, uri, options
  xml
end
handle_error(xml, response, request) click to toggle source
# File lib/version-one/client.rb, line 153
def handle_error(xml, response, request)
  msg = ''
  if xml && (xml.name == 'Error')
    xml.each do |el|
      msg << el.content
      msg << "\n"
    end
  else
    msg = response.body
  end
  VersionOne.logger.error("%s %s\n%s\n%s" % [response.code.to_s, request.path, response.message, msg])
  raise "VersionOne Error: #{response.message} (#{request.path}) #{msg}"
end
path_uri(path) click to toggle source
# File lib/version-one/client.rb, line 76
def path_uri(path)
  if path =~ /^#{service_uri.path}/i
    uri = service_uri.dup
    uri.path = path
    uri
  else
    uri_string = service_uri.to_s + '/' + path.sub(/^\/+/, '')
    URI.parse(uri_string)
  end
end
request(type, uri, options={}, &block) click to toggle source
# File lib/version-one/client.rb, line 87
def request(type, uri, options={}, &block)
  if (type == :get)
    cached_request type, uri, options, &block
  else
    uncached_request type, uri, options, &block
  end
end
uncached_request(type, uri, options={}) { |request| ... } click to toggle source
# File lib/version-one/client.rb, line 112
def uncached_request(type, uri, options={})
  xml = nil
  xml_body = nil
  VersionOne.logger.debug('%s %s' % [type.to_s.upcase, uri.to_s])

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'

  request_path = uri.path
  request_path += '?' + uri.query if uri.query

  http.start do
    klass = case type
              when :get
                Net::HTTP::Get
              when :post
                Net::HTTP::Post
              else
                raise ArgumentError, "Unhandled request type: #{type}"
            end
    request = klass.new(request_path)
    request.basic_auth(VersionOne.config.user, VersionOne.config.password)
    yield request if block_given?
    response = http.request(request)

    if response.content_type == XML_CONTENT_TYPE
      xml_body = response.body.to_s
      xml = XML::Document.string(xml_body).root
    end

    if (response.code.to_i / 100) != 2
      handle_error(xml, response, request)
    elsif can_cache? && options[:cache]
      cache_store.write(options[:cache][:key], xml_body, options[:cache][:options])
      VersionOne.logger.debug "CACHED #{options[:cache][:key]}"
    end
  end

  xml || true
end