Class: Xcal::Parktronic::ApiClient

Inherits:
Object
  • Object
show all
Includes:
Routes
Defined in:
lib/xcal/parktronic.rb,
lib/xcal/parktronic/api_client.rb

Instance Method Summary collapse

Methods included from Routes

#get_alarm_tags, #search_alarm

Methods included from Routes::CustomQueries

#find_custom_query, #get_custom_queries, #post_custom_query, #update_custom_query

Methods included from Routes::CommandNotifications

#get_paged_command_notifications, #update_command_notification

Methods included from Routes::EventHistoryItems

#get_paged_event_history_items

Methods included from Routes::StackChanges

#get_paged_stack_changes, #post_stack_change

Methods included from Routes::Outages

#get_paged_outages

Methods included from Routes::Events

#get_event, #get_event_tags, #update_event

Methods included from Routes::Metrics

#get_metric, #get_metric_by_name, #get_paged_metrics, #post_metric, #update_metric

Methods included from Routes::Alarms

#get_alarm, #get_paged_alarms, #post_alarm, #update_alarm

Constructor Details

#initialize(args) ⇒ ApiClient

API Client initialization.

Parameters

  • endpoint - OIV endpoint

  • proxy - enables proxy if proxy url is passed

  • access_token - access key to application

  • memcached_host - memcached host:port if you want enable caching

  • memcached_ttl - cache time to live (seconds), 60 seconds by default

  • version - api version. defaults to v1

Examples

api = Xcal::Parktronic::ApiClient.new(access_token: ‘your_access_token’, endpoint: ‘api-host.com’)



19
20
21
22
# File 'lib/xcal/parktronic/api_client.rb', line 19

def initialize(args)
  @args = args
  @args.default_proc = proc{|h, k| h.key?(k.to_s) ? h[k.to_s] : nil}
end

Instance Method Details

#access_tokenObject



37
38
39
# File 'lib/xcal/parktronic/api_client.rb', line 37

def access_token
  @args[:access_token]
end

#api_versionObject



41
42
43
# File 'lib/xcal/parktronic/api_client.rb', line 41

def api_version
  @args[:version] || 'v1'
end

#cacheObject



24
25
26
27
# File 'lib/xcal/parktronic/api_client.rb', line 24

def cache
  return nil if @args[:memcached_host].nil?
  @cache ||= Dalli::Client.new(@args[:memcached_host], :expires_in => (@args[:memcached_ttl] || 60), :namespace => "xcal_parktronic", :compress => true)
end

#get_response(url) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/xcal/parktronic/api_client.rb', line 68

def get_response(url)
  # Try to find response in cache
  cache_key = Digest::SHA1.hexdigest(url)
  return cache.get(cache_key) if cache && !cache.get(cache_key).nil?

  # Get response and save to cache (if cacing enabled)
  responce = http.get url
  cache.set(cache_key, responce) if cache

  responce
end

#hostObject



29
30
31
# File 'lib/xcal/parktronic/api_client.rb', line 29

def host
  uri.host
end

#httpObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xcal/parktronic/api_client.rb', line 49

def http
  return @http if @http

  proxy_http = @args[:proxy]
  if proxy_http.nil? || proxy_http.empty?
    @http = Net::HTTP.new(uri.host, uri.port)
  else
    proxy_uri = URI.parse(proxy_http)
    @http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port)
  end

  if uri.scheme == 'https'
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  @http
end

#portObject



45
46
47
# File 'lib/xcal/parktronic/api_client.rb', line 45

def port
  uri.port
end

#uriObject



33
34
35
# File 'lib/xcal/parktronic/api_client.rb', line 33

def uri
  @uri ||= URI(@args[:endpoint])
end