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

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

def get_attributes
  return @client.raw("get", "/config/attributes")
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")

265
266
267
# File 'lib/pub.rb', line 265

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)

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

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

#get_content_page(slug) ⇒ 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")

289
290
291
# File 'lib/pub.rb', line 289

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

#get_form(slug) ⇒ 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")

208
209
210
# File 'lib/pub.rb', line 208

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

#get_formsObject

Get Forms.

Get a collection of forms.

Example

@data = @mints_pub.get_forms

195
196
197
# File 'lib/pub.rb', line 195

def get_forms
  return @client.raw("get", "/content/forms")
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)

313
314
315
# File 'lib/pub.rb', line 313

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)

355
356
357
# File 'lib/pub.rb', line 355

def get_product(slug, options = nil)
  return @client.raw("get", "/ecommerce/products/#{slug}", options, nil, nil, nil, true)
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)

335
336
337
# File 'lib/pub.rb', line 335

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)

405
406
407
# File 'lib/pub.rb', line 405

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)

381
382
383
# File 'lib/pub.rb', line 381

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, title"
}
@data = @mints_pub.get_stories(options)

Third Example

options = {
  "fields": "id, title"
}
@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, nil, nil, nil, true)
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)

444
445
446
# File 'lib/pub.rb', line 444

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)

424
425
426
# File 'lib/pub.rb', line 424

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)

470
471
472
# File 'lib/pub.rb', line 470

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)

490
491
492
# File 'lib/pub.rb', line 490

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

#submit_form(data) ⇒ Object

Submit Form.

Submit a form with data.

Parameters

data

(Hash) – Data to be submited.

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)

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

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