Class: TurbaKit::Client
- Inherits:
-
Object
- Object
- TurbaKit::Client
- 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
- #api_url ⇒ Object
- #default_params ⇒ Object
- #delete_request(endpoint) ⇒ Object
- #get_request(endpoint, options = {}) ⇒ Object
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
- #post_request(endpoint, options = {}) ⇒ Object
- #put_request(endpoint, options = {}) ⇒ Object
- #response(request, namespace) ⇒ Object
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
Methods included from Checkins
Methods included from Countries
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, = {}) @api_key = api_key = { version: '/v2', domain: 'api.turba-webservices.com', scheme: 'https' } = .merge() end |
Instance Method Details
#api_url ⇒ Object
48 49 50 |
# File 'lib/turba_kit.rb', line 48 def api_url @api_url ||= "#{@options[:scheme]}://#{@options[:domain]}#{@options[:version]}" end |
#default_params ⇒ Object
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, = {}) [:namespace] ||= nil [:accept] ||= "application/json" [:params] ||= {} request = Typhoeus::Request.new( "#{api_url}#{endpoint}", method: :get, params: default_params.merge([:params]), headers: { Accept: [:accept] } ).run p request response(request, [: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, = {}) [:namespace] ||= nil [:accept] ||= "application/json" [:params] ||= {} request = Typhoeus::Request.new( "#{api_url}#{endpoint}", method: :post, params: default_params, body: Oj.dump([:params], mode: :compat), headers: { "Content-Type" => [:accept] } ).run response(request, [: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, = {}) [:namespace] ||= nil [:accept] ||= "application/json" [:params] ||= {} request = Typhoeus::Request.new( "#{api_url}#{endpoint}", method: :put, params: default_params, body: Oj.dump([:params], mode: :compat), headers: { "Content-Type" => [:accept] } ).run response(request, [: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.[: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 |