Class: Mints::Pub

Inherits:
Object
  • Object
show all
Includes:
MintsHelper
Defined in:
lib/pub.rb

Overview

Public context API

Pub class contains functions that needs only an API key as authentication

Usage example

For Mints::BaseController inheritance:

If the controller is inheriting from Mints::BaseController, Only use the class variable mints_pub Example:

@mints_pub.get_stories

For standalone usage:

Initialize

pub = Mints::Pub.new(mints_url, api_key)

or if host and api_key are provided by mints_config.yml.erb

pub = Mints::Pub.new

Call any function

pub.get_products

Single resource options

  • include - [String] Specify additional information to be included in the results from the objects relations. Example:

    { "include": "events" }
    
  • attributes - [Boolean] If present, attributes will be returned for each record in the results. Example:

    { "attributes": true }
    
  • categories - [Boolean] If present, categories will be returned for each record in the results. Example:

    { "categories": true }
    
  • tags - [Boolean] If present, tags will be returned for each record in the results. Example:

    { "tags": true }
    
  • fields - [String] Specify the fields that you want to be returned. If empty, all fields are returned. The object index can also be used to specify specific fields from relations. Example:

    { "fields": "id, title, slug" }
    { "fields[products]": "id, title, slug" }
    

Resource collections options

  • search - [String] If present, it will search for records matching the search string. Example:

    { "search": "searchstring" }
    
  • scopes - [String] If present, it will apply the specified Model’s scopes. Example:

    { "scopes": "approved, recent" }
    
  • filters - [String] This is a powerful parameter that allows the data to be filtered by any of its fields. Currently only exact matches are supported. Example:

    { "filters[title]": "titleToFilter" }
    
  • jfilters - [String] A complex filter configuration, as used in segments, in JSON format, base64 encoded and URLencoded. Example:

    jfilter = {
      "type":"group",
      "items":[
        {
          "type":"attribute",
          "operator":"==",
          "slug":"title",
          "value":"Action movies"
        }
      ],
      "operator":"or"
    }
    options = { "jfilters": jfilter }
    
  • catfilters - [String] filter by categories. Example:

    { "catfilters": "categoryName" }
    
  • fields - [String] Specify the fields that you want to be returned. If empty, all fields are returned. The object index can also be used to specify specific fields from relations. Example:

    { "fields": "id, title, slug" }
    { "fields[products]": "id, title, slug" }
    
  • sort - [String] The name of the field to perform the sort. Prefix the value with a minus sign - for ascending order. Example:

    { "sort": "title" }
    { "sort": "-title" }
    
  • include - [String] Specify additional information to be included in the results from the objects relations. Example:

    { "include": "events" }
    
  • attributes - [Boolean] If present, attributes will be returned for each record in the results. Example:

    { "attributes": true }
    
  • categories - [Boolean] If present, categories will be returned for each record in the results. Example:

    { "categories": true }
    
  • taxonomies - [Boolean] If present, taxonomies will be returned for each record in the results. Example:

    { "taxonomies": true }
    
  • tags - [Boolean] If present, tags will be returned for each record in the results. Example:

    { "tags": true }
    

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MintsHelper

#correct_json, #data_transform, #get_query_results

Constructor Details

#initialize(host, api_key, contact_token_id = nil, visit_id = nil, debug = false) ⇒ Pub

Initialize.

Class constructor.

Parameters

host

(String) – It’s the visitor IP.

api_key

(String) – Mints instance api key.

contact_token_id

(Integer) – Cookie ‘mints_contact_id’ value (mints_contact_token).

Return

Returns a Client object.


86
87
88
# File 'lib/pub.rb', line 86

def initialize(host, api_key, contact_token_id = nil, visit_id = nil, debug = false)
  @client = Mints::Client.new(host, api_key, 'public', nil, contact_token_id, visit_id, debug)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.


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

def client
  @client
end

Instance Method Details

#get_asset_info(slug) ⇒ Object

Get Asset Info.

Get a description of an Asset.

Parameters

slug

(String) – It’s the string identifier of the asset.

Example

@data = @mints_pub.get_asset_info("asset_slug")

142
143
144
# File 'lib/pub.rb', line 142

def get_asset_info(slug)
  return @client.raw("get", "/content/asset-info/#{slug}")
end

#get_attributesObject

Get Attributes.

Get a collection of attributes.

Example

@data = @mints_pub.get_attributes

556
557
558
# File 'lib/pub.rb', line 556

def get_attributes
  return @client.raw("get", "/config/attributes")
end

#get_content_bundle(slug, options = nil) ⇒ Object

Get Content Bundle.

Get a single content bundle.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

Example

@data = @mints_pub.get_content_page("test-page")

345
346
347
# File 'lib/pub.rb', line 345

def get_content_bundle(slug, options = nil)
  return @client.raw("get", "/content/content-pages/#{slug}", options)
end

#get_content_instance(slug) ⇒ Object

Get Content Instance.

Get a single content instance.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

Example

@data = @mints_pub.get_content_instance("content_instance_slug")

308
309
310
# File 'lib/pub.rb', line 308

def get_content_instance(slug)
  return @client.raw("get", "/content/content-instances/#{slug}")
end

#get_content_instances(options) ⇒ Object

Get Content Instances.

Get a collection of content instances. Note: Options must be specified.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

First Example

options = {
  "template": "content_instance_template_slug"
}
@data = @mints_pub.get_content_instances(options)

Second Example

options = {
  "template": "content_instance_template_slug",
  "sort": "-id"
}
@data = @mints_pub.get_content_instances(options)

295
296
297
# File 'lib/pub.rb', line 295

def get_content_instances(options)
  return @client.raw("get", "/content/content-instances", options)
end

#get_content_page(slug, options = nil) ⇒ Object

Get Content Page.

Get a single content page.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

Example

@data = @mints_pub.get_content_page("test-page")

332
333
334
# File 'lib/pub.rb', line 332

def get_content_page(slug, options = nil)
  return @client.raw("get", "/content/content-pages/#{slug}", options)
end

#get_form(slug, options = nil) ⇒ Object

Get Form.

Get a single form.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

Example

@data = @mints_pub.get_form("form_slug")

251
252
253
# File 'lib/pub.rb', line 251

def get_form(slug, options = nil)
  return @client.raw("get", "/content/forms/#{slug}", options)
end

#get_forms(options = nil) ⇒ Object

Get Forms.

Get a collection of forms.

Example

@data = @mints_pub.get_forms

238
239
240
# File 'lib/pub.rb', line 238

def get_forms(options = nil)
  return @client.raw("get", "/content/forms", options)
end

#get_locations(options = nil, use_post = true) ⇒ Object

Get Locations.

Get all locations.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

use_post

(Boolean) – Variable to determine if the request is by ‘post’ or ‘get’ functions.

First Example

@data = @mints_pub.get_locations

Second Example

options = { "fields": "title" }
@data = @mints_pub.get_locations(options)

Third Example

options = { "fields": "title" }
@data = @mints_pub.get_locations(options, false)

369
370
371
# File 'lib/pub.rb', line 369

def get_locations(options = nil, use_post = true)
  return get_query_results("/ecommerce/locations", options, use_post)
end

#get_product(slug, options = nil) ⇒ Object

Get Product.

Get a single product.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

@data = @mints_pub.get_product("product_slug")

Second Example

options = {
  "fields": "id, slug"
}
@data = @mints_pub.get_product("lego-set", options)

411
412
413
# File 'lib/pub.rb', line 411

def get_product(slug, options = nil)
  return @client.raw("get", "/ecommerce/products/#{slug}", options)
end

#get_products(options = nil, use_post = true) ⇒ Object

Get Products.

Get a collection of products.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

use_post

(Boolean) – Variable to determine if the request is by ‘post’ or ‘get’ functions.

First Example

@data = @mints_pub.get_products

Second Example

options = { "fields": "title" }
@data = @mints_pub.get_products(options)

Third Example

options = { "fields": "title" }
@data = @mints_pub.get_products(options, false)

391
392
393
# File 'lib/pub.rb', line 391

def get_products(options = nil, use_post = true)
  return get_query_results("/ecommerce/products", options, use_post)
end

#get_public_folder(slug, options) ⇒ Object

Get Public Folder.

Get a public folder info.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

options = {
  "object_type": "products"
}
@data = @mints_pub.get_public_folder('yellow', options)

Second Example

options = {
  "object_type": "products",
  "fields": "id, title"
}
@data = @mints_pub.get_public_folder('yellow', options)

461
462
463
# File 'lib/pub.rb', line 461

def get_public_folder(slug, options)
  return @client.raw("get", "/config/public-folders/#{slug}", options)
end

#get_public_folders(options) ⇒ Object

Get Public Folders.

Get a collection of public folders.

Parameters

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

options = {
  "object_type": "products"
}
@data = @mints_pub.get_public_folders(options)

Second Example

options = {
  "object_type": "products",
  "fields": "id",
  "sort": "-id"
}
@data = @mints_pub.get_public_folders(options)

437
438
439
# File 'lib/pub.rb', line 437

def get_public_folders(options)
  return @client.raw("get", "/config/public-folders", options)
end

#get_stories(options = nil, use_post = true) ⇒ Object

Get Stories.

Get a collection of stories.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

use_post

(Boolean) – Variable to determine if the request is by ‘post’ or ‘get’ functions.

First Example

@data = @mints_pub.get_stories

Second Example

options = {
  "fields": "id, slug"
}
@data = @mints_pub.get_stories(options)

Third Example

options = {
  "fields": "id, slug"
}
@data = @mints_pub.get_stories(options, false)

168
169
170
# File 'lib/pub.rb', line 168

def get_stories(options = nil, use_post = true)
  return get_query_results("/content/stories", options, use_post)
end

#get_story(slug, options = nil) ⇒ Object

Get Story.

Get a single story.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

@data = @mints_pub.get_story("story_slug")

Second Example

@data = @mints_pub.get_story("story_slug", options.to_json)

185
186
187
# File 'lib/pub.rb', line 185

def get_story(slug, options = nil)
  return @client.raw("get", "/content/stories/#{slug}", options)
end

#get_story_version(slug, options = nil) ⇒ Object

Get Story Version.

Get a single story version.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

@data = @mints_pub.get_story_version("story_slug")

Second Example

@data = @mints_pub.get_story_version("story_slug", options.to_json)

228
229
230
# File 'lib/pub.rb', line 228

def get_story_version(slug, options = nil)
  return @client.raw("get", "/content/story-versions/#{slug}", options)
end

#get_story_versions(options = nil, use_post = true) ⇒ Object

Get Story Versions.

Get a collection of story version.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

use_post

(Boolean) – Variable to determine if the request is by ‘post’ or ‘get’ functions.

First Example

@data = @mints_pub.get_story_versions

Second Example

options = {
  "fields": "id, title"
}
@data = @mints_pub.get_story_versions(options)

Third Example

options = {
  "fields": "id, title"
}
@data = @mints_pub.get_story_versions(options, false)

211
212
213
# File 'lib/pub.rb', line 211

def get_story_versions(options = nil, use_post = true)
  return get_query_results("/content/story-versions", options, use_post)
end

#get_tag(slug, options = nil) ⇒ Object

Get Tag.

Get a single tag.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

@data = @mints_pub.get_tag("tag_slug")

Second Example

options = {
  "fields": "id, tag"
}
@data = @mints_pub.get_tag("velit-0", options)

500
501
502
# File 'lib/pub.rb', line 500

def get_tag(slug, options = nil)
  return @client.raw("get", "/config/tags/#{slug}", options)
end

#get_tags(options = nil) ⇒ Object

Get Tags.

Get a collection of tags.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

First Example

@data = @mints_pub.get_tags

Second Example

options = {
  "fields": "id, tag"
}
@data = @mints_pub.get_tags(options)

480
481
482
# File 'lib/pub.rb', line 480

def get_tags(options = nil)
  return @client.raw("get", "/config/tags", options)
end

#get_taxonomies(options = nil, use_post = true) ⇒ Object

Get Taxonomies.

Get a collection of taxonomies.

Parameters

options

(Hash) – List of Resource collection Options shown above can be used as parameter.

use_post

(Boolean) – Variable to determine if the request is by ‘post’ or ‘get’ functions.

First Example

@data = @mints_pub.get_taxonomies

Second Example

options = {
  "fields": "id, title"
}
@data = @mints_pub.get_taxonomies(options)

Third Example

options = {
  "fields": "id, title"
}
@data = @mints_pub.get_taxonomies(options, false)

526
527
528
# File 'lib/pub.rb', line 526

def get_taxonomies(options = nil, use_post = true)
  return get_query_results("/config/taxonomies", options, use_post)
end

#get_taxonomy(slug, options = nil) ⇒ Object

Get Taxonomy.

Get a single taxonomy.

Parameters

slug

(String) – It’s the string identifier generated by Mints.

options

(Hash) – List of Single Resource Options shown above can be used as parameter.

First Example

@data = @mints_pub.get_taxonomy("taxonomy_slug")

Second Example

options = {
  "fields": "title"
}
@data = @mints_pub.get_taxonomy("taxonomy_slug", options)

546
547
548
# File 'lib/pub.rb', line 546

def get_taxonomy(slug, options = nil)
  return @client.raw("get", "/config/taxonomies/#{slug}", options)
end

#register_visit(request, ip = nil, user_agent = nil, url = nil) ⇒ Object

Register Visit.

Register a ghost/contact visit in Mints.Cloud.

Parameters

request

(ActionDispatch::Request) – request.

ip

(String) – It’s the visitor IP.

user_agent

(String) – The visitor’s browser user agent.

url

(String) – URL visited.

Example

request = {
  "remote_ip" => "http://1.1.1.1/",
  "user_agent" => "User Agent",
  "fullpath" => "https://fullpath/example"
}
@data = @mints_pub.register_visit(request, request["remote_ip"], request["user_agent"], request["fullpath"])

107
108
109
110
111
112
113
114
115
# File 'lib/pub.rb', line 107

def register_visit(request, ip = nil, user_agent = nil, url = nil)
  data = {
    ip_address: ip || request.remote_ip,
    user_agent: user_agent || request.user_agent,
    url: url || request.fullpath
  }
  response = @client.raw("post", "/register-visit", nil, data.to_json)
  return response
end

#register_visit_timer(visit, time) ⇒ Object

Register Visit timer.

Register a page visit time.

Parameters

visit

(String) – It’s the visitor IP.

time

(Integer) – The visitor’s browser user agent.

Example

@data = @mints_pub.register_visit_timer("60da2325d29acc7e55684472", 4)

127
128
129
# File 'lib/pub.rb', line 127

def register_visit_timer(visit, time)
  return @client.raw("get", "/register-visit-timer?visit=#{visit}&time=#{time}")
end

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/pub.rb', line 560

def send_user_magic_link(email_or_phone, template_slug, redirect_url = '', life_time = 1440, max_visits = nil, driver = 'email')
  data = {
    driver: driver,
    lifeTime: life_time,
    maxVisits: max_visits,
    redirectUrl: redirect_url,
    templateId: template_slug
  }
  if driver === 'sms' or driver === 'whatsapp'
    data['phone'] = email_or_phone
  else
    data['email'] = email_or_phone
  end
  return @client.raw("post", "/users/magic-link", nil, { data: data }.to_json, '/api/v1')
end

#submit_form(data) ⇒ Object

Submit Form.

Submit a form with data.

Parameters

data

(Hash) – Data to be submitted.

Example

data = {
  'form_slug': 'form_slug',
  'email': 'email@example.com',
  'given_name': 'given_name',
  'f1': 'Field 1 answer',
  'f2': 'Field 2 answer',
  'f3': 'Field 3 answer'
}
@data = @mints_pub.submit_form(data)

272
273
274
# File 'lib/pub.rb', line 272

def submit_form(data)
  return @client.raw("post", "/content/forms/submit", nil, data_transform(data))
end