Class: FFXIV::Lodestone::FreeCompany

Inherits:
Model
  • Object
show all
Defined in:
lib/ffxiv/lodestone/free-company.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Model

#attributes, find_by_name, #initialize

Constructor Details

This class inherits a constructor from FFXIV::Lodestone::Model

Instance Attribute Details

#activeObject

Returns the value of attribute active.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def active
  @active
end

#focusObject

Returns the value of attribute focus.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def focus
  @focus
end

#formedObject

Returns the value of attribute formed.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def formed
  @formed
end

#grand_companyObject

Returns the value of attribute grand_company.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def grand_company
  @grand_company
end

#grand_company_standingObject

Returns the value of attribute grand_company_standing.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def grand_company_standing
  @grand_company_standing
end

#housingObject

Returns the value of attribute housing.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def housing
  @housing
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def id
  @id
end

#logo_uriObject

Returns the value of attribute logo_uri.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def logo_uri
  @logo_uri
end

#membersObject

Returns the value of attribute members.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def members
  @members
end

#monthly_rankObject

Returns the value of attribute monthly_rank.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def monthly_rank
  @monthly_rank
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def name
  @name
end

#num_membersObject

Returns the value of attribute num_members.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def num_members
  @num_members
end

#rankObject

Returns the value of attribute rank.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def rank
  @rank
end

#recruitingObject Also known as: recruiting?

Returns the value of attribute recruiting.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def recruiting
  @recruiting
end

#seekingObject

Returns the value of attribute seeking.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def seeking
  @seeking
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def server
  @server
end

#sloganObject

Returns the value of attribute slogan.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def slogan
  @slogan
end

#tagObject

Returns the value of attribute tag.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def tag
  @tag
end

#weekly_rankObject

Returns the value of attribute weekly_rank.



5
6
7
# File 'lib/ffxiv/lodestone/free-company.rb', line 5

def weekly_rank
  @weekly_rank
end

Class Method Details

.find_by_id(id) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ffxiv/lodestone/free-company.rb', line 52

def find_by_id(id)
  begin
    dom = Lodestone.fetch("freecompany/#{id}")

    props = {}
    props[:id] = id

    props[:logo_uri] = dom.search("div.ic_crest_64 img").map{|n| n.attr("src")}

    gcs_node = dom.at("span.friendship_color")
    props[:grand_company_standing] = gcs_node.inner_text[1...-1]

    gc_node = gcs_node.previous_sibling
    props[:grand_company] = gc_node.inner_text.strip

    name_node = gcs_node.next_sibling.next_sibling.next_sibling
    props[:name] = name_node.inner_text

    server_node = name_node.next_sibling.next_sibling
    props[:server] = server_node.inner_text[1...-1]

    props[:tag] = dom.at("td.vm").last_element_child.next_sibling.inner_text[1...-1]

    dom.search("table.table_style2 tr").each do |tr|
      td_node = tr.at("td")
      td_text = td_node.inner_text.strip
      case tr.at("th").inner_text
        when "Formed"
          props[:formed] = Time.at(td_text.match(/ldst_strftime\((\d{10}), 'YMD'\)/)[1].to_i).utc
        when "Rank"
          props[:rank] = td_text.to_i
        when "Active Members"
          props[:num_members] = td_text.to_i
        when "Ranking"
          weekly, monthly, __garbage = td_node.inner_html.split("<br>").map(&:strip)
          props[:weekly_rank] = weekly.include?("--") ? nil : weekly.match(/\d+/)[0].to_i
          props[:monthly_rank] = monthly.include?("--") ? nil : monthly.match(/\d+/)[0].to_i
        when "Company Slogan"
          props[:slogan] = td_text == "" ? nil : td_text
        when "Active"
          props[:active] = td_text == "Not specified" ? nil : td_text
        when "Recruitment"
          props[:recruiting] = td_text == "Open"
        when "Focus"
          props[:focus] = td_node.search("li:not(.icon_off) img").map{|n| n.attr("title")}
        when "Seeking"
          props[:seeking] = td_node.search("li:not(.icon_off) img").map{|n| n.attr("title")}
        when "Estate Profile"
          name_node = td_node.at("div.txt_yellow")
          if name_node
            address_size, greeting = td_node.search("p").map(&:inner_text)
            address, size = address_size.split(" (")
            props[:housing] = {
              name: name_node.inner_text,
              address: address,
              size: size[0...-1],
              greeting: greeting == "No greeting available." ? nil : greeting
            }
          else
            props[:housing] = nil
          end
      end
    end
    self.new(props)
  rescue => e
    pp e
    nil
  end
end

.name_to_id(name, server) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/ffxiv/lodestone/free-company.rb', line 42

def name_to_id(name, server)
  search_results = self.search(name, server: server)
  if search_results
    search_results[:free_companies].each do |fc|
      return fc.id if name.downcase == fc.name.downcase
    end
  end
  nil
end

.search(keyword, server: nil, page: 1, verbose: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ffxiv/lodestone/free-company.rb', line 12

def search(keyword, server: nil, page: 1, verbose: false)
  dom = Lodestone.fetch("freecompany/?q=#{URI.escape(keyword)}&worldname=#{URI.escape(server)}&page=#{URI.escape(page.to_s)}")
  free_companies = dom.search("table.table_elements_com_fc tr").map do |tr|
    h4 = tr.at("td h4")
    a = h4.at("a")
    id = a.attr("href").split("/")[-1].to_i
    if verbose
      self.find_by_id(id)
    else
      self.new({
        id: id,
        name: a.content,
        server: h4.at("span").content.strip[1...-1],
        grand_company: tr.at("div.groundcompany_name").content
      })
    end
  end
  pagination = dom.at("div.current_list")
  if pagination
    total = pagination.at("span.total").content.to_i
    results = {
      show_start: pagination.at("span.show_start").content.to_i,
      show_end: pagination.at("span.show_end").content.to_i,
      total: total,
      num_pages: (total / 50.0).ceil,
      free_companies: free_companies
    }
  end
end