Class: Centaman::Service

Inherits:
Wrapper show all
Defined in:
lib/centaman/service.rb

Overview

:nodoc:

Defined Under Namespace

Classes: BookingTime, BookingType, Capacity, CouponCheck, CreateCustomer, Extra, GiftTicket, PurchaseTicket, TicketType

Constant Summary collapse

DEFAULT_TIMEOUT_TIME =
15

Constants inherited from Wrapper

Wrapper::FIXIE

Instance Attribute Summary

Attributes inherited from Wrapper

#api_password, #api_token, #api_username

Instance Method Summary collapse

Methods inherited from Wrapper

#generate_token, #headers, #initialize, #options, #options_hash

Constructor Details

This class inherits a constructor from Centaman::Wrapper

Instance Method Details

#after_init(args) ⇒ Object



8
9
10
# File 'lib/centaman/service.rb', line 8

def after_init(args)
  # overwritten by subclasses
end

#after_post(response) ⇒ Object



47
48
49
# File 'lib/centaman/service.rb', line 47

def after_post(response)
  build_object(response)
end

#fetch_allObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/centaman/service.rb', line 25

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

#payload(request_type = :get) ⇒ Object



51
52
53
54
# File 'lib/centaman/service.rb', line 51

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

#payload_key(request_type) ⇒ Object



56
57
58
# File 'lib/centaman/service.rb', line 56

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

#postObject



37
38
39
40
41
42
43
44
45
# File 'lib/centaman/service.rb', line 37

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

#wrap_request_in_case_of_timeout(proc, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/centaman/service.rb', line 12

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