Class: AstroChart::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/astro_chart/chart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(birth_date:, birth_time:, latitude:, longitude:, timezone:) ⇒ Chart

birth_date: “YYYY-MM-DD” birth_time: “HH:MM” latitude / longitude: Float timezone: IANA timezone string (e.g. “Asia/Taipei”)



9
10
11
12
13
14
15
# File 'lib/astro_chart/chart.rb', line 9

def initialize(birth_date:, birth_time:, latitude:, longitude:, timezone:)
  @birth_date = birth_date
  @birth_time = birth_time
  @latitude   = latitude.to_f
  @longitude  = longitude.to_f
  @timezone   = timezone
end

Instance Attribute Details

#birth_dateObject (readonly)

Returns the value of attribute birth_date.



3
4
5
# File 'lib/astro_chart/chart.rb', line 3

def birth_date
  @birth_date
end

#birth_timeObject (readonly)

Returns the value of attribute birth_time.



3
4
5
# File 'lib/astro_chart/chart.rb', line 3

def birth_time
  @birth_time
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



3
4
5
# File 'lib/astro_chart/chart.rb', line 3

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



3
4
5
# File 'lib/astro_chart/chart.rb', line 3

def longitude
  @longitude
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



3
4
5
# File 'lib/astro_chart/chart.rb', line 3

def timezone
  @timezone
end

Instance Method Details

#generateObject

Generate complete chart data. Returns a Hash with string keys, compatible with the existing JSON API.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/astro_chart/chart.rb', line 19

def generate
  jd = TimeConversion.to_julian_day(birth_date, birth_time, timezone)

  cusps, ascendant = Houses.calculate(jd, latitude, longitude)
  asc_zodiac = Zodiac.sign_name(ascendant)

  positions = Planets.calculate_positions(jd)
  planet_details = Planets.build_details(positions, cusps)

  kp = Planets.key_points_data(positions, cusps, ascendant)

  # Merge aspects into planet details
  aspect_map = {
    "太陽"  => "sun_aspects",
    "月亮"  => "moon_aspects",
    "土星"  => "saturn_aspects",
    "金星"  => "venus_aspects",
    "北交點" => "north_node_aspects",
    "南交點" => "south_node_aspects",
  }

  planet_details.each do |planet|
    key = aspect_map[planet["planet"]]
    planet["aspects"] = kp[key] if key
  end

  # Append ruler points
  planet_details.concat(kp["additional_points"])

  houses_data = cusps.each_with_index.map do |deg, i|
    {
      "house_number" => i + 1,
      "degree"       => deg.round(4),
      "zodiac"       => Zodiac.sign_name(deg),
    }
  end

  {
    "input" => {
      "birth_date"  => birth_date,
      "birth_time"  => birth_time,
      "coordinates" => {
        "latitude"  => latitude,
        "longitude" => longitude,
      },
      "timezone"    => timezone,
    },
    "chart" => {
      "ascendant" => {
        "zodiac"       => asc_zodiac,
        "degree"       => (ascendant % 30).round(4),
        "total_degree" => ascendant.round(4),
      },
      "planets" => planet_details,
      "houses"  => houses_data,
    },
  }
end