Sha256: a3c6bcaacd8a2f9e02c92756c92725ccdc24156494cc843be15bcf3a8e18ab8f

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

require 'timeout'

module Centaman
  #:nodoc:
  class Service < Wrapper
    DEFAULT_TIMEOUT_TIME = 15

    def after_init(args)
      # overwritten by subclasses
    end

    def wrap_request_in_case_of_timeout(proc, options = {})
      time = options.fetch(:timeout_time, DEFAULT_TIMEOUT_TIME)
      resp = nil
      begin
        resp = Timeout.timeout(time) do
          proc.call
        end
      rescue Timeout::Error
        raise Exceptions::CentamanTimeout
      end
      resp
    end

    def fetch_all
      @get_request ||= begin
        req = Proc.new do
          self.class.get(endpoint, payload(:get))
        end
        resp = wrap_request_in_case_of_timeout(req, timeout_time: 20)

        raise resp['Message'] if resp.is_a?(Hash)
        resp
      end
    end

    def post
      @post_request ||= begin
        req = Proc.new do
          self.class.post(endpoint, payload(:post))
        end
        resp = wrap_request_in_case_of_timeout(req)
        self.respond_to?(:build_object) ? after_post(resp) : resp
      end
    end

    def after_post(response)
      build_object(response)
    end

    def payload(request_type = :get)
      hash = { payload_key(request_type) => options_hash }
      hash.merge(headers: headers)
    end

    def payload_key(request_type)
      request_type == :get ? :query : :body
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
centaman-2.0.0 lib/centaman/service.rb
centaman-1.0.0 lib/centaman/service.rb
centaman-0.2.3 lib/centaman/service.rb
centaman-0.2.2 lib/centaman/service.rb
centaman-0.2.1 lib/centaman/service.rb
centaman-0.2.0 lib/centaman/service.rb