Module: AppManager::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/app_manager/model.rb

Instance Method Summary collapse

Instance Method Details

#get_chargeObject



85
86
87
88
89
90
91
92
93
# File 'lib/app_manager/model.rb', line 85

def get_charge
    begin
        shop_domain = self[AppManager.configuration.shopify_domain_field]
        plan_obj = AppManager::Client.new
      return  plan_obj.get_charge(shop_domain) 
    rescue Exception => e
      return "#{e.inspect}" 
    end
end

#get_feature(slug) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/app_manager/model.rb', line 55

def get_feature(slug)
    plan_features = self.plan_features
    features = plan_features.select{|x| x['slug'].to_s == slug }
	if features.any? 
        feature = features.first
        return casted_value(feature['value'],feature['value_type'])
    else
        nil
    end
end

#get_planObject



77
78
79
80
81
82
83
# File 'lib/app_manager/model.rb', line 77

def get_plan
    if !self[AppManager.configuration.plan_id_or_name_field]
        raise Error, "Invalid params, must have charge_id,shop && plan"
    end
    plan_obj = AppManager::Client.new
    return plan_obj.get_plan(self[AppManager.configuration.plan_id_or_name_field])
end

#get_plans_by_features(feature_slugs) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/app_manager/model.rb', line 120

def get_plans_by_features(feature_slugs)
    if feature_slugs.any?
        plan_data = []
        shop_domain = self[AppManager.configuration.shopify_domain_field]
        plan_obj = AppManager::Client.new
        plans = plan_obj.get_plans(shop_domain)
        plans = plans.select{|x| x['interval'] == 'EVERY_30_DAYS'  && x['public'] == true}
        plans.each do |plan|
            if plan['features'].any? && plan['features'].values.any? && plan['features'].values.collect{|c| c['slug']}.any?
             plan_feature_slugs = plan['features'].values.collect{|c| c['slug']}.sort
             if (feature_slugs & plan_feature_slugs).any? && ((feature_slugs & plan_feature_slugs).sort == feature_slugs.sort)
                    plan_data.push(plan)      
             end
            end
        end
        return plan_data
    else
        return []
    end
end

#get_remaining_daysObject



66
67
68
69
70
71
72
73
74
# File 'lib/app_manager/model.rb', line 66

def get_remaining_days
    shop_domain = self[AppManager.configuration.shopify_domain_field]
    trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at']
    trial_activated_at_val = self[trial_activated_at_field]
    plan_field = AppManager.configuration.field_names['plan_id']
    plan_id = self[plan_field]
    plan_obj = AppManager::Client.new
    shop_domain ? plan_obj.get_remaining_days(shop_domain,trial_activated_at_val,plan_id) : 0
end

#has_feature(slug) ⇒ Object



50
51
52
# File 'lib/app_manager/model.rb', line 50

def has_feature(slug)
	self.plan_features.select{|x| x['slug'].to_s == slug }.size > 0
end

#has_planObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/app_manager/model.rb', line 7

def has_plan
    if !self[AppManager.configuration.plan_id_or_name_field]
        return false;
    end 
    if self[AppManager.configuration.field_names['grandfathered']]
        return true;
    end
    plan_id = self[AppManager.configuration.plan_id_or_name_field]
    if !plan_id
        Rails.logger.info "Plan id found nil or not set"
        return false;
    end
    remaining_days = self.get_remaining_days
    if remaining_days > 0
        return true
    end
    plan_obj = AppManager::Client.new
    shop_domain = self[AppManager.configuration.shopify_domain_field]
    active_charge = plan_obj.get_charge(shop_domain) rescue nil
    return active_charge['active_charge'].present? && !active_charge['active_charge'].nil? ? true : false
end

#plan_featuresObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/app_manager/model.rb', line 31

def plan_features
	plan_id = self[AppManager.configuration.plan_id_or_name_field]
	if !plan_id
		raise Error, "Plan id not found"
	end
		plan_obj = AppManager::Client.new
		plans = plan_obj.get_plan(plan_id)
		if plans && plans['features'].blank?
			return []
		end
	features_by_plan = plans['features'].map { |h| h.slice('value', 'feature_id') }
		all_features = AppManager.configuration.plan_features
    feature_plan_ids = features_by_plan.map{|c| c['feature_id']}
    pf = all_features.select{|x| feature_plan_ids.include?(x['uuid'])}.each{|g| g["value"] = features_by_plan.find{|e| e['feature_id'] == g['uuid']}['value'] } 
    return pf
end

#set_default_plan(plan_id = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/app_manager/model.rb', line 95

def set_default_plan(plan_id=nil)
    begin
        if plan_id.nil?
            shop_domain = self[AppManager.configuration.shopify_domain_field]
            plan_obj = AppManager::Client.new
            plans = plan_obj.get_plans(shop_domain)
            free_plans = plans.select{|x| x['price'] == 0 && x['public'] == true}
            if free_plans.any?
                self.plan_id = free_plans.first["id"]
                self.save
            else
                raise Error, "Free Plan is not available"
            end
        else
            self.plan_id = plan_id
            self.save
        end
    rescue Exception => e
        return "#{e.inspect}" 
    end
end