Module: LookerSDK::Client::Dynamic

Included in:
LookerSDK::Client
Defined in:
lib/looker-sdk/client/dynamic.rb

Constant Summary collapse

@@sharable_operations =

If a given client is created with ‘:shared_swagger => true’ then it will try to use a globally sharable @@operations hash built from one fetch of the swagger.json for the given api_endpoint. This is an optimization for the cases where many sdk clients get created and destroyed (perhaps with different access tokens) while all talking to the same endpoints. This cuts down overhead for such cases considerably.

Hash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



105
106
107
108
# File 'lib/looker-sdk/client/dynamic.rb', line 105

def method_missing(method_name, *args, &block)
  entry = find_entry(method_name) || (return super)
  invoke_remote(entry, method_name, *args, &block)
end

Instance Attribute Details

#dynamicObject

Returns the value of attribute dynamic.



30
31
32
# File 'lib/looker-sdk/client/dynamic.rb', line 30

def dynamic
  @dynamic
end

Instance Method Details

#clear_swaggerObject



45
46
47
# File 'lib/looker-sdk/client/dynamic.rb', line 45

def clear_swagger
  @swagger = @operations = nil
end

#invoke(method_name, *args, &block) ⇒ Object

Callers can explicitly ‘invoke’ remote methods or let ‘method_missing’ do the trick. If nothing else, this gives clients a way to deal with potential conflicts between remote method names and names of methods on client itself.



100
101
102
103
# File 'lib/looker-sdk/client/dynamic.rb', line 100

def invoke(method_name, *args, &block)
  entry = find_entry(method_name) || raise(NameError, "undefined remote method '#{method_name}'")
  invoke_remote(entry, method_name, *args, &block)
end

#load_swaggerObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/looker-sdk/client/dynamic.rb', line 49

def load_swagger
  # We only need the swagger if we are going to be building our own 'operations' hash
  return if shared_swagger && @@sharable_operations[api_endpoint]
  # First, try to load swagger.json w/o authenticating
  @swagger ||= without_authentication { try_load_swagger }

  unless @swagger
    # try again, this time with authentication
    @swagger = try_load_swagger
  end

  # in unit tests, @swagger may be nil and last_response nil because no HTTP request was made
  if @swagger.nil?
    if @last_error
      raise @last_error
    else
      raise "Load of swagger.json failed."
    end
  end

  @swagger
end


92
93
94
95
# File 'lib/looker-sdk/client/dynamic.rb', line 92

def method_link(entry)
  uri = URI.parse(api_endpoint)
  "#{uri.scheme}://#{uri.host}:#{uri.port}/api-docs/index.html#!/#{entry[:info][:tags].first}/#{entry[:info][:operationId]}" rescue "http://docs.looker.com/"
end

#operationsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/looker-sdk/client/dynamic.rb', line 72

def operations
  return @@sharable_operations[api_endpoint] if shared_swagger && @@sharable_operations[api_endpoint]

  if !@swagger && @lazy_swagger
    load_swagger
  end

  return nil unless @swagger
  @operations ||= Hash[
    @swagger[:paths].map do |path_name, path_info|
      path_info.map do |method, route_info|
        route = @swagger[:basePath].to_s + path_name.to_s
        [route_info[:operationId].to_sym, {:route => route, :method => method, :info => route_info}]
      end
    end.reduce(:+)
  ].freeze

  shared_swagger ? (@@sharable_operations[api_endpoint] = @operations) : @operations
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/looker-sdk/client/dynamic.rb', line 110

def respond_to?(method_name, include_private=false)
  !!find_entry(method_name) || super
end

#try_load_swaggerObject



32
33
34
35
# File 'lib/looker-sdk/client/dynamic.rb', line 32

def try_load_swagger
  resp = get('swagger.json') rescue nil
  resp && last_response && last_response.status == 200 && last_response.data && last_response.data.to_attrs
end