Class: EPlat::Base

Inherits:
ActiveResource::Base
  • Object
show all
Extended by:
Countable
Includes:
Concerns::Aliases, Concerns::Dirty, Concerns::FullJson, Concerns::GraphQLable, Concerns::OverwriteInstanceMethods, Concerns::OverwriteRequestMethods
Defined in:
lib/e_plat/resource/base.rb

Constant Summary

Constants included from Concerns::GraphQLable

Concerns::GraphQLable::FILTER_ARGS, Concerns::GraphQLable::QUERY_ARG_ARGS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Concerns::Aliases

#type_coercer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Countable

count

Methods included from Concerns::GraphQLable

#graphql_mutation_string, #mutate_graphql, #remove_mutation_root_from

Methods included from Concerns::Aliases

#add_aliases!

Methods included from Concerns::FullJson

#as_full_json, #to_full_json

Methods included from Concerns::OverwriteRequestMethods

#collection_path, #element_path, included

Methods included from Concerns::OverwriteInstanceMethods

#as_eplat_json, #as_json, #create, #create_resource_for, #to_eplat_json, #to_json, #update

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Base

Returns a new instance of Base.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/e_plat/resource/base.rb', line 93

def initialize(attributes = {}, persisted = false)
  site = client.base_url
  ActiveResource::Base.include_root_in_json = client.shopify? && top_level_resource?

  super
  
  self.mapping = mapping_instance

  @mapped_attributes = ensure_e_plat_attributes!
  ensure_native_attributes!
  
  add_aliases!(mapping.native_attribute_aliases, type_schema || {})
end

Class Attribute Details

.read_onlyObject

Returns the value of attribute read_only.



18
19
20
# File 'lib/e_plat/resource/base.rb', line 18

def read_only
  @read_only
end

Instance Attribute Details

#mapped_attributesObject

Returns the value of attribute mapped_attributes.



91
92
93
# File 'lib/e_plat/resource/base.rb', line 91

def mapped_attributes
  @mapped_attributes
end

#mappingObject

Returns the value of attribute mapping.



91
92
93
# File 'lib/e_plat/resource/base.rb', line 91

def mapping
  @mapping
end

Class Method Details

.cached_shopify_webhook?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/e_plat/resource/base.rb', line 75

def cached_shopify_webhook?
  self.to_s.include?("EPlat::ShopifyWebhook")
end

.clientObject



40
41
42
# File 'lib/e_plat/resource/base.rb', line 40

def client
  EPlat::Current.e_plat_session
end

.exclude_from_json(read_only) ⇒ Object



67
68
69
# File 'lib/e_plat/resource/base.rb', line 67

def exclude_from_json(read_only)
  self.read_only = read_only
end

.inherited(subclass) ⇒ Object



23
24
25
26
27
# File 'lib/e_plat/resource/base.rb', line 23

def inherited(subclass)
  super
  subclass.exclude_from_json(read_only) if read_only
  subclass.schema ||= schema.dup
end

.initialize_singleton!Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/e_plat/resource/base.rb', line 29

def initialize_singleton!
  self.site  = client.base_url # .site is an ActiveResource method that uses the threadsafe _site attribute underneath
  self.prefix  = client.url_prefix(klass: self) # this is overwritten below in prefix= for threadsafety
  
  self.timeout = 5
  self.include_format_in_path = client.include_format_in_path?
  self.mapping = mapping_instance

  client.platform_headers.each {|name, value| self.headers[name] = value}
end

.platform_specific_class?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/e_plat/resource/base.rb', line 71

def platform_specific_class?
  self.class.to_s.include? client.platform.to_s.capitalize
end

.prefix=(value = "/") ⇒ Object

monkey patch prefix= so we can add thread-local value to the generated prefix_source() method



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/e_plat/resource/base.rb', line 46

def prefix=(value = "/")
  # Replace :placeholders with '#{embedded options[:lookups]}'
  prefix_call = value.gsub(/:\w+/) { |key| "\#{URI::DEFAULT_PARSER.escape options[#{key}].to_s}" }
    
  # Clear prefix parameters in case they have been cached
  @prefix_parameters = nil
    
  silence_warnings do
    # Redefine the new methods.
    instance_eval "    def prefix_source() \n      client.url_prefix(klass: self) || \"\#{value}\" \n    end\n    def prefix(options={}) \"\#{prefix_call}\" end\n    RUBY_EVAL\n  end\nrescue Exception => e\n  logger.error \"Couldn't set prefix: \#{e}\\n  \#{code}\" if logger\n  raise\nend\n", __FILE__, __LINE__ + 1

Instance Method Details

#clientObject



107
108
109
# File 'lib/e_plat/resource/base.rb', line 107

def client
  EPlat::Current.e_plat_session
end

#formatted_idObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/e_plat/resource/base.rb', line 139

def formatted_id
  return id unless client.shopify? && client.api_version != "2024_01"
  return if id.to_s.include?("gid://")

  case element_name
  when "product"
    "gid://shopify/Product/#{id}"
  when "variant"
    "gid://shopify/ProductVariant/#{id}"
  when "image"
    "gid://shopify/ProductImage/#{id}"
  else
    id
  end
end

#graphql_inputObject

Raises:



155
156
157
# File 'lib/e_plat/resource/base.rb', line 155

def graphql_input
  raise EPlat::Error.new("GraphQL input not supported for this resource and platform")
end

#headersObject



129
130
131
# File 'lib/e_plat/resource/base.rb', line 129

def headers
  full_response&.each_header&.to_h || {}
end

#include_root_in_jsonObject



133
134
135
136
137
# File 'lib/e_plat/resource/base.rb', line 133

def include_root_in_json
  return false unless client.shopify?
  
  top_level_resource?
end

#mapped?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/e_plat/resource/base.rb', line 111

def mapped?(attribute)
  mapped_attributes.include? attribute.to_s
end

#native_keysObject



115
116
117
118
119
120
121
122
123
# File 'lib/e_plat/resource/base.rb', line 115

def native_keys
  if client.shopify? 
    @mapping.native_attributes.presence || schema.keys # EPlat uses Shopify's schema, apar from a couple of Graphql only fields
  elsif mapping.class.to_s != EPlat::Mapping::Base && @mapping.native_attributes.present?
    @mapping.native_attributes.presence
  else
    attributes.keys
  end
end

#read_only?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/e_plat/resource/base.rb', line 125

def read_only?
  !!self.class.read_only
end