Class: MaropostApi::RelationalTableRows

Inherits:
Object
  • Object
show all
Defined in:
lib/maropost_api/relational_tables.rb

Overview

Contains methods to operate various operations like create, update, delete or read from the Relational Table Api

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"], table_name:) ⇒ RelationalTableRows

Creates a new instance of Reports class.

Parameters:

  • account (Integer) (defaults to: ENV["ACCOUNT"])

    is authentic user account id (Integer) provided by maropost.com

  • api_key (String) (defaults to: ENV["API_KEY"])

    is the auth token (String) that is validated on the server to authenticate the user

  • table_name (String)

    sets which relational table the service’s operations should act against



15
16
17
18
19
20
21
# File 'lib/maropost_api/relational_tables.rb', line 15

def initialize( = ENV["ACCOUNT"], api_key = ENV["API_KEY"], table_name:)
  MaropostApi.instance_variable_set(:@api_key, api_key)
  MaropostApi.instance_variable_set(:@account, )
  @table_name = table_name
  
  MaropostApi.base_uri "https://rdb.maropost.com/#{}"
end

Instance Attribute Details

#table_nameObject

Returns the value of attribute table_name.



8
9
10
# File 'lib/maropost_api/relational_tables.rb', line 8

def table_name
  @table_name
end

Instance Method Details

#create(key_value_col, *key_value_cols) ⇒ Object

Adds a record to the Relational Table

Parameters:

  • key_value_col (Hash)

    A key value pair to be added to the table

  • key_value_cols (Hash)

    Other multile key value pairs that can be added at once in the table

Raises:

  • (TypeError)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/maropost_api/relational_tables.rb', line 49

def create(key_value_col, *key_value_cols)
  raise TypeError.new("#{key_value_col.inspect} is not type of Hash") unless key_value_col.kind_of? Hash
  if (key_value_cols)
    invalid_cols = key_value_cols.select{|c| !c.is_a? Hash }
    raise TypeError.new("#{invalid_cols.join(', ')} are not type of Hash") unless invalid_cols.size == 0
  end
  full_path = full_resource_path '/create'
  all_key_values = [key_value_col] + key_value_cols
  
  MaropostApi.post_result(full_path, :record => all_key_values)
end

#delete(unique_field_name, value) ⇒ Object

Deletes the given record from the Relational Table

Parameters:

  • unique_field_name (String)

    name of the unique identifier of the record to be deleted

  • value (Mixed)

    value matching the unique identifier

Raises:

  • (TypeError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/maropost_api/relational_tables.rb', line 100

def delete(unique_field_name, value)
  raise TypeError.new("#{unique_field_name.inspect} is not a String") unless unique_field_name.is_a? String
  record = {:record => {}}
  record[:record][unique_field_name] = value
  
  # first check if key value exists
  show_record = show(unique_field_name, value)
  return show_record unless show_record.success
  
  full_path = full_resource_path '/delete'
  query_params = MaropostApi.set_query_params
  
  MaropostApi.delete_result(full_path, query_params, record)      
end

#getObject

Gets the records of the relational table



25
26
27
28
29
30
# File 'lib/maropost_api/relational_tables.rb', line 25

def get
  full_path = full_resource_path
  query_params = MaropostApi.set_query_params
  
  MaropostApi.get_result(full_path, query_params)
end

#show(unique_field_name, value) ⇒ Object

Gets the specified record from the relational table

Parameters:

  • unique_field_name (String)

    the name of the field to retrieve

  • value (Mixed)

    Value of the unique_field_name

Raises:

  • (TypeError)


36
37
38
39
40
41
42
43
# File 'lib/maropost_api/relational_tables.rb', line 36

def show(unique_field_name, value)
  raise TypeError.new("#{unique_field_name.inspect} should be a string") unless unique_field_name.is_a? String
  params = {:record => {}}
  params[:record][unique_field_name.to_sym] = value
  full_path = full_resource_path '/show'
  
  MaropostApi.post_result(full_path, params)
end

#update(key_value_col, *key_value_cols) ⇒ Object

Updates a record in the Relational Table

Parameters:

  • key_value_col (Hash)

    A key value to be updated in the table

  • key_value_cols (Hash)

    Rest of the key value pairs to be updated if there are multiple

Raises:

  • (TypeError)


65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/maropost_api/relational_tables.rb', line 65

def update(key_value_col, *key_value_cols)
  raise TypeError.new("#{key_value_col.inspect} is not type of Hash") unless key_value_col.kind_of? Hash
  if (key_value_cols)
    invalid_cols = key_value_cols.select{|c| !c.is_a? Hash }
    raise TypeError.new("#{invalid_cols.join(', ')} are not type of Hash") unless invalid_cols.size == 0
  end
  full_path = full_resource_path '/update'
  all_key_values = [key_value_col] + key_value_cols
  query_params = MaropostApi.set_query_params
  
  MaropostApi.put_result(full_path, {:record => all_key_values}, query_params)
end

#upsert(key_value_col, *key_value_cols) ⇒ Object

Creates or updates a record in the Relational Table

Parameters:

  • key_value_col (Hash)

    A key value to be updated inthe table

  • key_value_cols (Hash)

    Rest of the key value pairs to be added/updated in case of multiple fields

Raises:

  • (TypeError)


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/maropost_api/relational_tables.rb', line 82

def upsert(key_value_col, *key_value_cols)
  raise TypeError.new("#{key_value_col.inspect} is not type of Hash") unless key_value_col.kind_of? Hash
  if (key_value_cols)
    invalid_cols = key_value_cols.select{|c| !c.is_a? Hash }
    raise TypeError.new("#{invalid_cols.join(', ')} are not type of Hash") unless invalid_cols.size == 0
  end
  full_path = full_resource_path '/upsert'
  all_key_values = [key_value_col] + key_value_cols

  query_params = MaropostApi.set_query_params
  
  MaropostApi.put_result(full_path, {:record => all_key_values}, query_params)
end