Class: TypeformData::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, logger: nil) ⇒ Client

For the sake of usability, we’re breaking convention here and accepting an API key as the first parameter instead of an instance of TypeformData::Config. ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html)

Parameters:

  • api_key (String)
  • logger (Object) (defaults to: nil)

    Should implement the same API as



12
13
14
# File 'lib/typeform_data/client.rb', line 12

def initialize(api_key:, logger: nil)
  @config = TypeformData::Config.new(api_key: api_key, logger: logger)
end

Class Method Details

.new_from_config(config) ⇒ Object



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

def self.new_from_config(config)
  raise TypeformData::ArgumentError, 'Missing config' unless config
  new(api_key: config.api_key, logger: config.logger)
end

Instance Method Details

#all_typeformsObject



31
32
33
34
35
# File 'lib/typeform_data/client.rb', line 31

def all_typeforms
  get('forms').parsed_json.map do |form_hash|
    TypeformData::Typeform.new(@config, form_hash)
  end
end

#as_json(*_args) ⇒ Object



70
71
72
# File 'lib/typeform_data/client.rb', line 70

def as_json(*_args)
  raise 'Do not serialize TypeformData::Client-- it contains your API key'
end

#dump(object) ⇒ Object

The goals of this alias are:

1. To bring the serialization process within TypeformData's API, so
     we can modify it in the future if needed.
2. Maintain symmetry with #load, which needs to be part of the API.


45
46
47
# File 'lib/typeform_data/client.rb', line 45

def dump(object)
  Marshal.dump(object)
end

#get(endpoint, params = {}) ⇒ TypeformData::ApiResponse

Your API key will automatically be added to the request URL as a query param, as required by the API.

Parameters:

  • (String)
  • (Hash)

Returns:



27
28
29
# File 'lib/typeform_data/client.rb', line 27

def get(endpoint, params = {})
  TypeformData::Requestor.get(@config, endpoint, params)
end

#load(serialized, default = nil) ⇒ Object

Parameters:

  • serialized (String)

    The output of Marshal.dump(vci) where vci is either (1) an instance of TypeformData::ValueClass or (2) an array of instances of TypeformData::ValueClass.

  • default (Object) (defaults to: nil)

    What to return if ‘serialized’ is blank or not a String.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/typeform_data/client.rb', line 53

def load(serialized, default = nil)
  return default unless serialized.is_a?(String) && !serialized.empty?

  Marshal.load(serialized).tap { |marshaled|
    case marshaled
    when Array
      marshaled.each { |object| object.reconfig(@config) }
    else
      marshaled.reconfig(@config)
    end
  }
end

#marshal_dumpObject



66
67
68
# File 'lib/typeform_data/client.rb', line 66

def marshal_dump
  raise 'Do not serialize TypeformData::Client-- it contains your API key'
end

#typeform(id) ⇒ Object



37
38
39
# File 'lib/typeform_data/client.rb', line 37

def typeform(id)
  TypeformData::Typeform.new(@config, id: id)
end