class Josean::Application
Public Class Methods
# File lib/josean.rb, line 7 def initialize application_url @application_url = application_url @total_configuration = {} @total_configuration[:head] = {} @total_configuration[:body] = {} @total_configuration[:body][:sections] = [] end
Public Instance Methods
Sets the body header of the application image URL to the image style_image Hash containing the CSS style of the image style Hash containing the CSS style of the frame
# File lib/josean.rb, line 33 def body_header image, style_image, style @total_configuration[:body][:header] = { title: { type: 'image', url: image, style: style_image }, style: style } end
Add a body style section style Hash containing the CSS style of the frame
# File lib/josean.rb, line 47 def body_style style @total_configuration[:body][:style] = style end
Add a get call with a given action name name_of_resource Name of the resource that this request will be attached to url URL to which the request will be issued view View to which the application will be transitioned on success
# File lib/josean.rb, line 90 def get_request_action name_of_resource, url, view create_network_request 'get', name_of_resource, url, nil, view end
Sets the head of the application title Title of the application description Description of the application icon URL to the icon image style Hash containing the CSS style
# File lib/josean.rb, line 21 def head title, description, icon, style @total_configuration[:head] = {title: title, description: description, icon: icon, style: style} end
Add a post call with a given action name name_of_resource Name of the resource that this request will be attached to url URL to which the request will be issued view View to which the application will be transitioned on success
# File lib/josean.rb, line 99 def post_request_action name_of_resource, url, data_hash, view create_network_request 'post', name_of_resource, url, data_hash, view end
Add a put call with a given action name name_of_resource Name of the resource that this request will be attached to url URL to which the request will be issued view View to which the application will be transitioned on success
# File lib/josean.rb, line 108 def put_request_action name_of_resource, url, data_hash, view create_network_request 'put', name_of_resource, url, data_hash, view end
Add a body section that can scroll components and return a handler for it
# File lib/josean.rb, line 59 def scrolled_section type, style if valid_type?(type) and style.is_a? Hash @total_configuration[:body][:sections] << {items: []} @total_configuration[:body][:sections].last[:items] << {type: type, style: style, components: []} @total_configuration[:body][:sections].last[:items] else nil end end
Add a component to a given scrolled section section handler to which section component Component to be added. It needs to be a Jasonette object
# File lib/josean.rb, line 81 def scrolled_section_component section, component section.last[:components] << component if section.is_a? Array and section.last.keys.include? :components end
Add a body section and return a handler for it
# File lib/josean.rb, line 52 def section @total_configuration[:body][:sections] << {items: []} @total_configuration[:body][:sections].last[:items] end
Add an item to a given section section handler to which section item Item to be added. It needs to be a Jasonette object
# File lib/josean.rb, line 73 def section_item section, item section << item if section.is_a? Array end
Returns the json so that it can be served
# File lib/josean.rb, line 114 def to_json if valid? '{"$jason":' + JSON.unparse(@total_configuration) + '}' end end
Private Instance Methods
Checks that the array of tags and content is correct
# File lib/josean.rb, line 151 def check_content list, region_of_configuration list.map do |k| @total_configuration[region_of_configuration].keys.include?(k.first) ? (@total_configuration[region_of_configuration][k.first].is_a?(k.last)) : true end.include?(false) ? false : true end
Checks whether the actions hash is created on the current jason and creates it in case not
# File lib/josean.rb, line 159 def check_if_actions_is_created unless @total_configuration[:head][:actions].is_a? Hash @total_configuration[:head][:actions] = {} end end
Creates a network request as an action and returns the hash verb : HTTP verb (supported: get, put, post, patch name_of_resource: Action name url: URL to which it will be requested data_hash: data sent to server view: view to which it'll transition
# File lib/josean.rb, line 172 def create_network_request verb, name_of_resource, url, data_hash, view check_if_actions_is_created @total_configuration[:head][:actions]["#{name_of_resource}"] = if ['get', 'put', 'post', 'patch'].include? verb.downcase { type: "$network.request", options: { url: url, method: verb.downcase, data: data_hash }, success: { type: "href", options: { url: @application_url + '/' + view, transtion: 'replace' } }, error: { type: "$util.toast", options: { text: "Something went wrong when #{verb.upcase} on #{url}.", type: "Error" } } } end end
Checks whether the configuration is correct
# File lib/josean.rb, line 123 def valid? return_value = false unless @total_configuration.keys.length != 2 and @total_configuration[:head].empty? or @total_configuration[:body].empty? return_value = valid_header? and valid_body? end return_value end
Checks wether the body is valid
# File lib/josean.rb, line 139 def valid_body? check_content [[:header, Hash], [:sections, Array], [:layers, Array], [:footer, Hash]], :body end
Checks wether the header is valid
# File lib/josean.rb, line 133 def valid_header? check_content [[:title, String], [:description, String], [:icon, String], [:offline, String], [:styles, Hash], [:actions, Hash], [:templates, Hash], [:data, Hash], [:agents, Hash]], :head end
Checks wether the type of a section is valid
# File lib/josean.rb, line 145 def valid_type? type ['vertical', 'horizontal'].include? type end