Class: Takeout::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/takeout/client.rb

Constant Summary collapse

CALLBACKS =

A constant specifying the kind of event callbacks and if they should or should not raise an error

{failure: true, missing: true, redirect: false, success: false}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

The main client initialization method.

Attributes

  • options - The main atrtibute and extra global options to set for the client

Options

  • :uri - A string defining the URI for the API to call.

  • :headers - A hash specifying the headers to apply to each request

  • :ssl - A boolean to specify whether or not SSL is turned on

  • :schemas - A hash specifying the custom per-endpoint schema templates

  • :extension - A string with the extension to be appended on each request



48
49
50
51
52
53
54
# File 'lib/takeout/client.rb', line 48

def initialize(options={})
  if block_given?
    yield self
  else
    extract_instance_variables_from_options(options)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *attributes, &block) ⇒ Object (private)



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/takeout/client.rb', line 184

def method_missing(method_sym, *attributes, &block)
  request_type = method_sym.to_s.scan(/^(?:get|post|put|delete|patch|update)/).first
  request_name = method_sym.to_s.scan(/(?<=_{1}).*/).first
  if (request_type && request_name)
    self.define_singleton_method(method_sym) do |options={}, &block|
      # Extract values that we store separately from the options hash and then clean it up
      headers.merge!(options[:headers]) if options[:headers]

      # Merge in global options
      options.merge!(@options) if @options

      # Build the request_url and update the options to remove templated values (if there are any)
      request_url, options = generate_request_url(request_name, request_type, options)

      # Clean up options hash before performing request
      [:headers, :extension, :object_id, :endpoint].each { |value| options.delete(value)}

      return perform_curl_request(request_type, request_url, options, headers)
    end

    self.send(method_sym, *attributes, &block)
  else
    super
  end
end

Instance Attribute Details

#debugBoolean

Returns a boolean specifying whether or not to run curl with teh verbose setting.

Returns:

  • (Boolean)

    a boolean specifying whether or not to run curl with teh verbose setting



10
11
12
# File 'lib/takeout/client.rb', line 10

def debug
  @debug
end

#endpoint_prefixObject

Returns the value of attribute endpoint_prefix.



32
33
34
# File 'lib/takeout/client.rb', line 32

def endpoint_prefix
  @endpoint_prefix
end

#extensionString

Returns a string with the extension to be appended on each request.

Returns:

  • (String)

    a string with the extension to be appended on each request



19
20
21
# File 'lib/takeout/client.rb', line 19

def extension
  @extension
end

#headersHash

Returns a hash specifying the headers to apply to each request.

Returns:

  • (Hash)

    a hash specifying the headers to apply to each request



16
17
18
# File 'lib/takeout/client.rb', line 16

def headers
  @headers
end

#optionsHash

Returns a hash specifying the global options to apply to each request.

Returns:

  • (Hash)

    a hash specifying the global options to apply to each request



13
14
15
# File 'lib/takeout/client.rb', line 13

def options
  @options
end

#portObject

Returns the value of attribute port.



30
31
32
# File 'lib/takeout/client.rb', line 30

def port
  @port
end

#schemasHash

Returns a hash specifying the custom per-endpoint schema templates.

Returns:

  • (Hash)

    a hash specifying the custom per-endpoint schema templates



25
26
27
# File 'lib/takeout/client.rb', line 25

def schemas
  @schemas
end

#sslBoolean

Returns a boolean to specify whether or not SSL is turned on.

Returns:

  • (Boolean)

    a boolean to specify whether or not SSL is turned on



22
23
24
# File 'lib/takeout/client.rb', line 22

def ssl
  @ssl
end

#uriString

Returns the uri to send requests to.

Returns:

  • (String)

    the uri to send requests to



28
29
30
# File 'lib/takeout/client.rb', line 28

def uri
  @uri
end

Instance Method Details

#disable_sslObject

Flips the @ssl instance variable to false



74
75
76
# File 'lib/takeout/client.rb', line 74

def disable_ssl
  @ssl=false
end

#enable_sslObject

Flips the @ssl instance variable to true



69
70
71
# File 'lib/takeout/client.rb', line 69

def enable_ssl
  @ssl=true
end

#port?Boolean

Check if a port is specified.

Returns:

  • (Boolean)

    Returns true if a port is enabled, false if nil.



64
65
66
# File 'lib/takeout/client.rb', line 64

def port?
  return !port.nil?
end

#ssl?Boolean

Check if SSL is enabled.

Returns:

  • (Boolean)

    Returns true if SSL is enabled, false if disabled



58
59
60
# File 'lib/takeout/client.rb', line 58

def ssl?
  return @ssl
end