Module: BiteMediaSeo::LocalSeoSchema

Defined in:
lib/bitemediaseo/local_seo_schema.rb,
lib/bitemediaseo/local_seo_schema/version.rb

Constant Summary collapse

DEFAULT_TYPE =
"ProfessionalService"
ALLOWED_LOCAL_TYPES =
%w[
  LocalBusiness
  Organization
  ProfessionalService
  Store
].freeze
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.create_local_business(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bitemediaseo/local_seo_schema.rb', line 19

def create_local_business(options)
  validate_hash!(options, "options")

  type = optional_text(options[:type] || options["type"], "type") || DEFAULT_TYPE
  unless ALLOWED_LOCAL_TYPES.include?(type)
    raise ArgumentError, "Unsupported Schema.org type: #{type}"
  end

  compact_hash(
    "@context" => "https://schema.org",
    "@type" => type,
    "name" => require_text(options[:name] || options["name"], "name"),
    "url" => validate_url(options[:url] || options["url"], "url"),
    "description" => optional_text(options[:description] || options["description"], "description"),
    "telephone" => optional_text(options[:telephone] || options["telephone"], "telephone"),
    "priceRange" => optional_text(options[:price_range] || options[:priceRange] || options["priceRange"], "priceRange"),
    "areaServed" => optional_text(options[:area_served] || options[:areaServed] || options["areaServed"], "areaServed"),
    "address" => postal_address(options[:address] || options["address"]),
    "geo" => geo_coordinates(options[:geo] || options["geo"]),
    "sameAs" => same_as(options[:same_as] || options[:sameAs] || options["sameAs"])
  )
end

.create_service(options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bitemediaseo/local_seo_schema.rb', line 42

def create_service(options)
  validate_hash!(options, "options")

  provider = options[:provider] || options["provider"]
  provider = create_local_business(provider) if provider.is_a?(Hash)

  compact_hash(
    "@context" => "https://schema.org",
    "@type" => "Service",
    "name" => require_text(options[:name] || options["name"], "name"),
    "url" => validate_url(options[:url] || options["url"], "url"),
    "description" => optional_text(options[:description] || options["description"], "description"),
    "serviceType" => optional_text(options[:service_type] || options[:serviceType] || options["serviceType"], "serviceType"),
    "areaServed" => optional_text(options[:area_served] || options[:areaServed] || options["areaServed"], "areaServed"),
    "provider" => provider
  )
end

.for_bitemediaseoObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bitemediaseo/local_seo_schema.rb', line 60

def for_bitemediaseo
  create_service(
    name: "Pozycjonowanie stron Szczecin - BiteMediaSEO",
    url: "https://bitemediaseo.pl/",
    description: "Pozycjonowanie stron w Szczecinie: SEO lokalne, AI/GEO, audyty i transparentne raporty.",
    service_type: "Pozycjonowanie stron internetowych",
    area_served: "Szczecin, Polska",
    provider: {
      name: "BiteMediaSEO",
      url: "https://bitemediaseo.pl/",
      type: "ProfessionalService",
      description: "Agencja SEO wspierajaca widocznosc firm w Google, mapach i odpowiedziach AI.",
      area_served: "Szczecin, Polska",
      address: {
        addressLocality: "Szczecin",
        addressCountry: "PL"
      }
    }
  )
end

.script_tag(schema) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/bitemediaseo/local_seo_schema.rb', line 86

def script_tag(schema)
  json = to_json_ld(schema)
  safe_json = json
              .gsub("<", "\\u003c")
              .gsub(">", "\\u003e")
              .gsub("&", "\\u0026")
  %(<script type="application/ld+json">#{safe_json}</script>)
end

.to_json_ld(schema, pretty: false) ⇒ Object



81
82
83
84
# File 'lib/bitemediaseo/local_seo_schema.rb', line 81

def to_json_ld(schema, pretty: false)
  validate_hash!(schema, "schema")
  pretty ? JSON.pretty_generate(schema) : JSON.generate(schema)
end