lib/litexbrl/tdnet/summary.rb in litexbrl-0.0.2 vs lib/litexbrl/tdnet/summary.rb in litexbrl-0.0.3

- old
+ new

@@ -1,155 +1,148 @@ -require 'nokogiri' - module LiteXBRL module TDnet - class Summary - extend Utils + class Summary < FinancialInformation + include SummaryAttribute - attr_accessor :code, :year, :quarter, - :net_sales, :operating_income, :ordinary_income, :net_income, :net_income_per_share + def self.read(doc) + xbrl = read_data doc - # 名前空間 - NS = { - 'xbrli' => 'http://www.xbrl.org/2003/instance', - 'tse-t-ed' => 'http://www.xbrl.tdnet.info/jp/br/tdnet/t/ed/2007-06-30' - } + {summary: xbrl.attributes, + results_forecast: [xbrl.attributes_results_forecast]} + end - class << self - def parse(path) - doc = File.open(path) {|f| Nokogiri::XML f } - read doc - end + private - def parse_string(str) - doc = Nokogiri::XML str - read doc - end + def self.read_data(doc) + xbrl, context = find_base_data(doc) - private + find_data(doc, xbrl, context) + end - def read(doc) - consolidation, season = find_consolidation_and_season(doc) - context = context_hash(consolidation, season) + def self.find_base_data(doc) + consolidation, season = find_consolidation_and_season(doc) + context = context_hash(consolidation, season) - xbrl = Summary.new + xbrl = new - # 証券コード - xbrl.code = find_securities_code(doc, consolidation) - # 会計年度 - xbrl.year = find_year(doc, consolidation) - # 四半期 - xbrl.quarter = find_quarter(doc, consolidation, context) + # 証券コード + xbrl.code = find_securities_code(doc, consolidation) + # 決算年 + xbrl.year = find_year(doc, consolidation) + # 決算月 + xbrl.month = find_month(doc, consolidation) + # 四半期 + xbrl.quarter = find_quarter(doc, consolidation, context) - # 売上高 - xbrl.net_sales = to_mill(find_value(doc, "NetSales", context[:context_duration])) - # 営業利益 - xbrl.operating_income = to_mill(find_value(doc, "OperatingIncome", context[:context_duration])) - # 経常利益 - xbrl.ordinary_income = to_mill(find_value(doc, "OrdinaryIncome", context[:context_duration])) - # 純利益 - xbrl.net_income = to_mill(find_value(doc, "NetIncome", context[:context_duration])) - # 1株当たり純利益 - xbrl.net_income_per_share = find_value(doc, "NetIncomePerShare", context[:context_duration]).to_f + return xbrl, context + end - xbrl - end + def self.find_consolidation_and_season(doc) + consolidation = find_consolidation(doc) + season = find_season(doc, consolidation) - def find_consolidation_and_season(doc) - consolidation = find_consolidation(doc) + # 連結で取れない場合、非連結にする + unless season + consolidation = "NonConsolidated" season = find_season(doc, consolidation) - - # 連結で取れない場合、非連結にする - unless season - consolidation = "NonConsolidated" - season = find_season(doc, consolidation) - end - - return consolidation, season end - # - # 連結・非連結を取得します - # - def find_consolidation(doc) - cons = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYearConsolidatedDuration']/xbrli:entity/xbrli:identifier", NS) - non_cons = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYearNonConsolidatedDuration']/xbrli:entity/xbrli:identifier", NS) + return consolidation, season + end - if cons - "Consolidated" - elsif non_cons - "NonConsolidated" - else - raise StandardError.new("連結・非連結ともに該当しません。") - end + # + # 通期・四半期を取得します + # + def self.find_season(doc, consolidation) + year = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYear#{consolidation}Instant']/xbrli:entity/xbrli:identifier") + quarter = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentQuarter#{consolidation}Instant']/xbrli:entity/xbrli:identifier") + q1 = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentAccumulatedQ1#{consolidation}Instant']/xbrli:entity/xbrli:identifier") + q2 = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentAccumulatedQ2#{consolidation}Instant']/xbrli:entity/xbrli:identifier") + q3 = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentAccumulatedQ3#{consolidation}Instant']/xbrli:entity/xbrli:identifier") + + if year + "Year" + elsif quarter + "Quarter" + elsif q1 + "AccumulatedQ1" + elsif q2 + "AccumulatedQ2" + elsif q3 + "AccumulatedQ3" end + end - # - # 通期・四半期を取得します - # - def find_season(doc, consolidation) - year = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYear#{consolidation}Instant']/xbrli:entity/xbrli:identifier", NS) - quarter = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentQuarter#{consolidation}Instant']/xbrli:entity/xbrli:identifier", NS) - q1 = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentAccumulatedQ1#{consolidation}Instant']/xbrli:entity/xbrli:identifier", NS) - q2 = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentAccumulatedQ2#{consolidation}Instant']/xbrli:entity/xbrli:identifier", NS) - q3 = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentAccumulatedQ3#{consolidation}Instant']/xbrli:entity/xbrli:identifier", NS) + def self.find_data(doc, xbrl, context) + # 売上高 + xbrl.net_sales = find_value_to_mill(doc, NET_SALES, context[:context_duration]) + # 営業利益 + xbrl.operating_income = find_value_to_mill(doc, OPERATING_INCOME, context[:context_duration]) + # 経常利益 + xbrl.ordinary_income = find_value_to_mill(doc, ORDINARY_INCOME, context[:context_duration]) + # 純利益 + xbrl.net_income = find_value_to_mill(doc, NET_INCOME, context[:context_duration]) + # 1株当たり純利益 + xbrl.net_income_per_share = find_value_to_f(doc, NET_INCOME_PER_SHARE, context[:context_duration]) - if year - "Year" - elsif quarter - "Quarter" - elsif q1 - "AccumulatedQ1" - elsif q2 - "AccumulatedQ2" - elsif q3 - "AccumulatedQ3" - end - end + # 売上高前年比 + xbrl.change_in_net_sales = find_value_to_f(doc, CHANGE_IN_NET_SALES, context[:context_duration]) + # 営業利益前年比 + xbrl.change_in_operating_income = find_value_to_f(doc, CHANGE_IN_OPERATING_INCOME, context[:context_duration]) + # 経常利益前年比 + xbrl.change_in_ordinary_income = find_value_to_f(doc, CHANGE_IN_ORDINARY_INCOME, context[:context_duration]) + # 純利益前年比 + xbrl.change_in_net_income = find_value_to_f(doc, CHANGE_IN_NET_INCOME, context[:context_duration]) - # - # contextを設定します - # - def context_hash(consolidation, season) - raise StandardError.new("通期・四半期が設定されていません。") unless season + # 前期売上高 + xbrl.prior_net_sales = find_value_to_mill(doc, NET_SALES, context[:context_prior_duration]) + # 前期営業利益 + xbrl.prior_operating_income = find_value_to_mill(doc, OPERATING_INCOME, context[:context_prior_duration]) + # 前期経常利益 + xbrl.prior_ordinary_income = find_value_to_mill(doc, ORDINARY_INCOME, context[:context_prior_duration]) + # 前期純利益 + xbrl.prior_net_income = find_value_to_mill(doc, NET_INCOME, context[:context_prior_duration]) + # 前期1株当たり純利益 + xbrl.prior_net_income_per_share = find_value_to_f(doc, NET_INCOME_PER_SHARE, context[:context_prior_duration]) - { - context_duration: "Current#{season}#{consolidation}Duration", - context_instant: "Current#{season}#{consolidation}Instant" - } - end + # 前期売上高前年比 + xbrl.change_in_prior_net_sales = find_value_to_f(doc, CHANGE_IN_NET_SALES, context[:context_prior_duration]) + # 前期営業利益前年比 + xbrl.change_in_prior_operating_income = find_value_to_f(doc, CHANGE_IN_OPERATING_INCOME, context[:context_prior_duration]) + # 前期経常利益前年比 + xbrl.change_in_prior_ordinary_income = find_value_to_f(doc, CHANGE_IN_ORDINARY_INCOME, context[:context_prior_duration]) + # 前期純利益前年比 + xbrl.change_in_prior_net_income = find_value_to_f(doc, CHANGE_IN_NET_INCOME, context[:context_prior_duration]) - # - # 証券コードを取得します - # - def find_securities_code(doc, consolidation) - elm_code = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYear#{consolidation}Duration']/xbrli:entity/xbrli:identifier", NS) - to_securities_code(elm_code) - end + # 通期予想売上高 + xbrl.forecast_net_sales = find_value_to_mill(doc, FORECAST_NET_SALES, context[:context_forecast].call(xbrl.quarter)) + # 通期予想営業利益 + xbrl.forecast_operating_income = find_value_to_mill(doc, FORECAST_OPERATING_INCOME, context[:context_forecast].call(xbrl.quarter)) + # 通期予想経常利益 + xbrl.forecast_ordinary_income = find_value_to_mill(doc, FORECAST_ORDINARY_INCOME, context[:context_forecast].call(xbrl.quarter)) + # 通期予想純利益 + xbrl.forecast_net_income = find_value_to_mill(doc, FORECAST_NET_INCOME, context[:context_forecast].call(xbrl.quarter)) + # 通期予想1株当たり純利益 + xbrl.forecast_net_income_per_share = find_value_to_f(doc, FORECAST_NET_INCOME_PER_SHARE, context[:context_forecast].call(xbrl.quarter)) - # - # 会計年度を取得します - # - def find_year(doc, consolidation) - elm_end = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYear#{consolidation}Duration']/xbrli:period/xbrli:endDate", NS) - to_year(elm_end) - end + # 通期予想売上高前年比 + xbrl.change_in_forecast_net_sales = find_value_to_f(doc, CHANGE_FORECAST_NET_SALES, context[:context_forecast].call(xbrl.quarter)) + # 通期予想営業利益前年比 + xbrl.change_in_forecast_operating_income = find_value_to_f(doc, CHANGE_FORECAST_OPERATING_INCOME, context[:context_forecast].call(xbrl.quarter)) + # 通期予想経常利益前年比 + xbrl.change_in_forecast_ordinary_income = find_value_to_f(doc, CHANGE_FORECAST_ORDINARY_INCOME, context[:context_forecast].call(xbrl.quarter)) + # 通期予想純利益前年比 + xbrl.change_in_forecast_net_income = find_value_to_f(doc, CHANGE_FORECAST_NET_INCOME, context[:context_forecast].call(xbrl.quarter)) - # - # 四半期を取得します - # - def find_quarter(doc, consolidation, context) - elm_end = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='CurrentYear#{consolidation}Duration']/xbrli:period/xbrli:endDate", NS) - elm_instant = doc.at_xpath("//xbrli:xbrl/xbrli:context[@id='#{context[:context_instant]}']/xbrli:period/xbrli:instant", NS) - to_quarter(elm_end, elm_instant) - end + xbrl + end - # - # 勘定科目の値を取得します - # - def find_value(doc, item, context) - doc.at_xpath("//xbrli:xbrl/tse-t-ed:#{item}[@contextRef='#{context}']", NS).content - end + def self.find_value_to_mill(doc, item, context) + to_mill find_value_tse_t_ed(doc, item, context) + end + + def self.find_value_to_f(doc, item, context) + to_f find_value_tse_t_ed(doc, item, context) end end end end \ No newline at end of file