Class: MaropostApi::RelationalTableRows
- Inherits:
-
Object
- Object
- MaropostApi::RelationalTableRows
- 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
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#create(key_value_col, *key_value_cols) ⇒ Object
Adds a record to the Relational Table.
-
#delete(unique_field_name, value) ⇒ Object
Deletes the given record from the Relational Table.
-
#get ⇒ Object
Gets the records of the relational table.
-
#initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"], table_name:) ⇒ RelationalTableRows
constructor
Creates a new instance of Reports class.
-
#show(unique_field_name, value) ⇒ Object
Gets the specified record from the relational table.
-
#update(key_value_col, *key_value_cols) ⇒ Object
Updates a record in the Relational Table.
-
#upsert(key_value_col, *key_value_cols) ⇒ Object
Creates or updates a record in the Relational Table.
Constructor Details
#initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"], table_name:) ⇒ RelationalTableRows
Creates a new instance of Reports class.
15 16 17 18 19 20 21 |
# File 'lib/maropost_api/relational_tables.rb', line 15 def initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"], table_name:) MaropostApi.instance_variable_set(:@api_key, api_key) MaropostApi.instance_variable_set(:@account, account) @table_name = table_name MaropostApi.base_uri "https://rdb.maropost.com/#{account}" end |
Instance Attribute Details
#table_name ⇒ Object
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
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
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 |
#get ⇒ Object
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
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
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
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 |