Class: TurbaKit::Client

Inherits:
Object
  • Object
show all
Includes:
Availability, CallToAction, Checkins, Content, Countries, CrowdTable, Link, Misc, Notification, Phonenumber, Place, Services, Tip, Units
Defined in:
lib/turba_kit.rb

Instance Method Summary collapse

Methods included from Notification

#create_service_notification, #delete_service_notification, #update_service_notification

Methods included from Place

#create_service_place, #delete_service_place, #update_service_place

Methods included from Content

#create_service_content, #delete_service_content, #update_service_content

Methods included from Tip

#create_service_tip, #delete_service_tip, #update_service_tip

Methods included from Phonenumber

#create_service_phonenumber, #delete_service_phonenumber, #update_service_phonenumber

Methods included from CallToAction

#create_service_call_to_action, #delete_service_call_to_action, #update_service_call_to_action

Methods included from Misc

#create_service_misc, #delete_service_misc, #update_service_misc

Methods included from Link

#create_service_link, #delete_service_link, #update_service_link

Methods included from CrowdTable

#create_service_crowd_table, #delete_service_crowd_table, #service_crowd_table, #update_service_crowd_table

Methods included from Services

#search_service, #service, #services, #update_service

Methods included from Units

#create_unit_container, #delete_unit_container, #update_unit, #update_unit_container

Methods included from Availability

#availability

Methods included from Checkins

#checkins, #create_checkin

Methods included from Countries

#countries, #country

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
45
46
# File 'lib/turba_kit.rb', line 38

def initialize(api_key, options = {})
  @api_key = api_key
  default_options = {
    version: '/v2',
    domain: 'api.turba-webservices.com',
    scheme: 'https'
  }
  @options = default_options.merge(options)
end

Instance Method Details

#api_urlObject



48
49
50
# File 'lib/turba_kit.rb', line 48

def api_url
  @api_url ||= "#{@options[:scheme]}://#{@options[:domain]}#{@options[:version]}"
end

#default_paramsObject



52
53
54
# File 'lib/turba_kit.rb', line 52

def default_params
  @default_params ||= { api_key: @api_key }
end

#delete_request(endpoint) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/turba_kit.rb', line 86

def delete_request(endpoint)
  request = Typhoeus::Request.new(
    "#{api_url}#{endpoint}",
    method: :delete,
    params: default_params,
  ).run

  response(request, nil)
end

#get_request(endpoint, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/turba_kit.rb', line 96

def get_request(endpoint, options = {})
  options[:namespace] ||= nil
  options[:accept] ||= "application/json"
  options[:params] ||= {}

  request = Typhoeus::Request.new(
    "#{api_url}#{endpoint}",
    method: :get,
    params: default_params.merge(options[:params]),
    headers: { Accept: options[:accept] }
  ).run

  p request

  response(request, options[:namespace])
end

#post_request(endpoint, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/turba_kit.rb', line 71

def post_request(endpoint, options = {})
  options[:namespace] ||= nil
  options[:accept] ||= "application/json"
  options[:params] ||= {}

  request = Typhoeus::Request.new(
    "#{api_url}#{endpoint}",
    method: :post,
    params: default_params,
    body: Oj.dump(options[:params], mode: :compat),
    headers: { "Content-Type" => options[:accept] }
  ).run
  response(request, options[:namespace])
end

#put_request(endpoint, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/turba_kit.rb', line 56

def put_request(endpoint, options = {})
  options[:namespace] ||= nil
  options[:accept] ||= "application/json"
  options[:params] ||= {}

  request = Typhoeus::Request.new(
    "#{api_url}#{endpoint}",
    method: :put,
    params: default_params,
    body: Oj.dump(options[:params], mode: :compat),
    headers: { "Content-Type" => options[:accept] }
  ).run
  response(request, options[:namespace])
end

#response(request, namespace) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/turba_kit.rb', line 113

def response(request, namespace)
  body = Oj.load(request.body)
  body = body.with_indifferent_access unless body.nil?
  status = request.options[:response_code]
  body ||= request.body

  if (400..599).include?(status)
    return Error.from_response(body, status)
  end

  return "" if body.empty?
  RecursiveOpenStruct.new(body, recurse_over_arrays: true)
end