class TokyoMetro::Factory::Get::Api::MetaClass::Fundamental

API からデータを取得するための Factory Pattern のクラス(最上位クラス)

Public Class Methods

new( parse_json , generate_instance , to_inspect ) click to toggle source

Constructor @param parse_json [Boolean] JSON をパースするか否かの設定 @param generate_instance [Boolean] JSON を変換し Ruby のインスタンスを作成するか否かの設定 @note generate_instance を true にする場合は、parse_json も true にしなければならない。

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 10
def initialize( parse_json , generate_instance , to_inspect )
  if generate_instance and !( parse_json )
    raise "Error: If you want to generate instance, you must set \"generate_instance\" to true and \"parse_json\" to true."
  end
  @parse_json = parse_json
  @generate_instance = generate_instance
  @to_inspect = to_inspect
end

Private Class Methods

info_class() click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 119
def self.info_class
  raise "This method \'#{ __method__ }\' is not defined in \'#{ self }\'."
end
list_class() click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 115
def self.list_class
  raise "This method \'#{ __method__ }\' is not defined in \'#{ self }\'."
end
process() click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 111
def self.process
  raise "This method \'#{ __method__ }\' is not defined in \'#{ self }\'."
end

Public Instance Methods

get_data( http_client ) click to toggle source

API からデータを取得するメソッド @param http_client [HTTPClient] HTTPClient のインスタンス【必須】 @return [::Array <Hash>] インスタンス変数 parse_json が true の場合は、JSON を配列に変換して返す。 @return [String] インスタンス変数 parse_json が false の場合は、JSON を文字列として返す。

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 23
def get_data( http_client )
  check_validity_of_settings( http_client )
  if @to_inspect
    time_begin = Time.now
    # progress_bar = ::ForRails::Progress.new
    # progress_bar.display( parameters_to_strf )
    puts "-" * 128
    puts ""
    puts parameters_to_strf
    puts ""
  end
  response = response_from_api( http_client )
  if @to_inspect
    t = calc_time( time_begin )
    puts "Download: #{t} sec"
    puts ""
  end

  process_response( response )
end

Private Instance Methods

access_point_url() click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 107
def access_point_url
  raise "This method \'#{ __method__ }\' is not defined in \'#{ self.class }\'."
end
body_of_response( response ) click to toggle source

レスポンスの body 部分を取得するメソッド @param response [HTTPClient] API のレスポンス @return [Stirng]

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 88
def body_of_response( response )
  response.body
end
check_validity_of_settings( http_client ) click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 52
def check_validity_of_settings( http_client )
  raise "Error" unless ::TokyoMetro::ACCESS_TOKEN.string?
  raise "Error" unless http_client.instance_of?( ::HTTPClient )
end
generate_new_array_instance( *args ) click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 99
def generate_new_array_instance( *args )
  raise "This method \'#{ __method__ }\' is not defined in \'#{ self.class }\'."
end
parameters() click to toggle source

パラメータを格納したハッシュを取得するメソッド @return [Hash]

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 48
def parameters
  { "acl:consumerKey" => ::TokyoMetro::ACCESS_TOKEN }
end
parameters_to_strf() click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 57
def parameters_to_strf
  parameters.map { | k , v | k.ljust(24) + " ... " + v }.join( "\n" )
end
parsed_json( response ) click to toggle source

レスポンスの body 部分の JSON をパースし、配列やハッシュに変換するメソッド @param response [HTTPClient] API のレスポンス @return [::Array or Hash]

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 95
def parsed_json( response )
  ::JSON.parse( body_of_response( response ) )
end
process_response( response ) click to toggle source

API のレスポンスを設定に応じて処理するメソッド @param response [HTTPClient] API のレスポンス @return [subclass of ::TokyoMetro::Api::MetaClass::Fundamental::List] JSON を変換し Ruby のインスタンスが作成される場合 @return [::Array] JSON をパースするだけで、Ruby のインスタンスを作成しない場合 @return [String] JSON をパースしない場合

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 72
def process_response( response )
  if @parse_json
    ary = parsed_json( response )
    if @generate_instance
      generate_new_array_instance( ary )
    else
      ary
    end
  else
    body_of_response( response )
  end
end
response_from_api( http_client ) click to toggle source

API からのレスポンスを受け取るメソッド @param http_client [::HTTPClient] HTTPクライアント

# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 63
def response_from_api( http_client )
  http_client.get( access_point_url , parameters )
end
set_parameter_send_to_api() click to toggle source
# File lib/tokyo_metro/factory/get/api/meta_class/fundamental.rb, line 103
def set_parameter_send_to_api
  raise "This method \'#{ __method__ }\' is not defined in \'#{ self.class }\'."
end