class DatapointsController

Public Instance Methods

index() click to toggle source
# File app/controllers/datapoints_controller.rb, line 6
def index

 if params.length == 3
     head :bad_request and return
 end

  @results = { 'results' => {}, 'remains'  => false}

  @station_diagram_results = Stationdiagram.criteria
  @station_diagram_results = @station_diagram_results.only("ucode","title","route" ).where( "title" => params[:dc_title] ) if params[:dc_title].present?
   
  @station_diagram_results.each do |i|
    @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                  {
                                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ {"value" => "http://opendata.ubin.jp/puti#diagram",
                                                                                             "type" => "uri"}],
                                    "http://purl.org/dc/elements/1.1/title"           => [ {"value" => i[:title],
                                                                                             "type" => "literal"}],
                                    "http://opendata.ubin.jp/puti#diagram"            => [ {"value" => "https://api.opendata.ubin.jp/api/v1/puti/diagram/ucode_" + i[:ucode] ,
                                                                                             "type" => "literal"}]
                                  }
                                })
  end


  @busstop_diagram_results = Busstopdiagram.criteria
  @busstop_diagram_results = @busstop_diagram_results.only("ucode","title","route" ).where( "title" => params[:dc_title] ) if params[:dc_title].present?

  @busstop_diagram_results.each do |i|
    @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                  {
                                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ {"value" => "http://opendata.ubin.jp/puti#diagram",
                                                                                             "type" => "uri"}],
                                    "http://purl.org/dc/elements/1.1/title"           => [ {"value" => i[:title],
                                                                                             "type" => "literal"}],
                                    "http://opendata.ubin.jp/puti#diagram"            => [ {"value" => "https://api.opendata.ubin.jp/api/v1/puti/diagram/ucode_" + i[:ucode] ,
                                                                                             "type" => "literal"}]
                                  }
                                })
  end

  @station_results = Station.criteria
  @station_results = @station_results.where( "puti_yearOfTogether.value" =>  params[:puti_yearoftogether] ) if params[:puti_yearoftogether].present?
  @station_results = @station_results.where( "puti_yearOfBegin.value"    =>  params[:puti_yearofbegin]    ) if params[:puti_yearofbegin].present? 
  @station_results = @station_results.where( "puti_nameOfStation.value"  =>  params[:puti_nameofstation]  ) if params[:puti_nameofstation].present? 
  @station_results = @station_results.where( "puti_nameOfRailway.value"  =>  params[:puti_nameofrailway]  ) if params[:puti_nameofrailway].present? 
  @station_results = @station_results.where( "puti_operatorCode.value"   =>  params[:puti_operatorcode]   ) if params[:puti_operatorcode].present? 
  @station_results = @station_results.where( "puti_operator.value"       =>  params[:puti_operator]       ) if params[:puti_operator].present? 
  @station_results = @station_results.where( "dc_title.value"            =>  params[:dc_title]            ) if params[:dc_title].present? 

  @station_results.each do |i|
    @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                  {
                                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                             "type"  => "uri" } ],
                                    "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                    "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                    "type" => "literal"}],
                                    "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                    "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                    "http://opendata.ubin.jp/puti#OperatorCode"    => i[:puti_operatorCode],
                                    "http://opendata.ubin.jp/puti#NameOfRailway"   => i[:puti_nameOfRailway],
                                    "http://opendata.ubin.jp/puti#NameOfStation"   => i[:puti_nameOfStation],
                                    "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                    "http://opendata.ubin.jp/puti#YearOfTogether"  => i[:puti_yearOfTogether],
                                    "http://opendata.ubin.jp/puti#YearOfBegin"     => i[:puti_yearOfBegin]
                                  }
                                })
  end

  @busstop_results = Busstop.criteria
  @busstop_results = @busstop_results.where( "puti_typeOfBus.value" =>  params[:puti_typeofbus]                  ) if params[:puti_typeofbus].present? 
  @busstop_results = @busstop_results.where( "puti_nameOfBusRoutes.value" =>  %r#{params[:puti_nameofbusroute]}/    ) if params[:puti_nameofbusroute].present? 
  @busstop_results = @busstop_results.where( "puti_nameOfBusStop.value"  =>  params[:puti_nameofbusstop]           ) if params[:puti_nameofbusstop].present? 
  @busstop_results = @busstop_results.where( "puti_operators.value"       =>  %r#{params[:puti_operator]}/          ) if params[:puti_operator].present? 
  @busstop_results = @busstop_results.where( "dc_title.value"            =>  params[:dc_title]                     ) if params[:dc_title].present? 

  @busstop_results.each do |i|

    @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                  {
                                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                             "type"  => "uri" } ],
                                    "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                    "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                    "type" => "literal"}],
                                    "http://opendata.ubin.jp/puti#NameOfBusStop"   => i[:puti_nameOfBusStop],
                                    "http://opendata.ubin.jp/puti#TypeOfBus"       => i[:puti_typeOfBus],
                                    "http://opendata.ubin.jp/puti#Operators"       => i[:puti_operators],
                                    "http://opendata.ubin.jp/puti#NameOfBusRoutes" => i[:puti_nameOfBusRoutes]
                                  }
                                })
  end # @results_all.each do |i|

  @railroad_results = Railroad.criteria
  @railroad_results = @railroad_results.where( "puti_yearOfTogether.value" =>  params[:puti_yearoftogether] ) if params[:puti_yearoftogether].present?
  @railroad_results = @railroad_results.where( "puti_yearOfBegin.value"    =>  params[:puti_yearofbegin]    ) if params[:puti_yearofbegin].present? 
  @railroad_results = @railroad_results.where( "puti_nameOfRailway.value"  =>  params[:puti_nameofrailway]  ) if params[:puti_nameofrailway].present? 
  @railroad_results = @railroad_results.where( "puti_operatorCode.value"   =>  params[:puti_operatorcode]   ) if params[:puti_operatorcode].present? 
  @railroad_results = @railroad_results.where( "puti_operator.value"       =>  params[:puti_operator]       ) if params[:puti_operator].present? 
  @railroad_results = @railroad_results.where( "dc_title.value"            =>  params[:dc_title]            ) if params[:dc_title].present? 


  @railroad_results.each do |i|
      
    @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                  {
                                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                             "type"  => "uri" } ],
                                    "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                    "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                    "type" => "literal"}],
                                    "http://opendata.ubin.jp/puti#OperatorCode"    => i[:puti_operatorCode],
                                    "http://opendata.ubin.jp/puti#NameOfRailway"   => i[:puti_nameOfRailway],
                                    "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                    "http://opendata.ubin.jp/puti#YearOfTogether"  => i[:puti_yearOfTogether],
                                    "http://opendata.ubin.jp/puti#YearOfBegin"     => i[:puti_yearOfBegin]
                                  }
                                })
  end # @results_all.each do |i|


  @busroute_results = Busroute.criteria
  @busroute_results = @busroute_results.where( "puti_frequencyInHoliday.value" =>  params[:puti_frequencyinholiday]     ) if params[:puti_frequencyinholiday].present?
  @busroute_results = @busroute_results.where( "puti_frequencyInSaturday.value" =>  params[:puti_frequencyinsaturday]   ) if params[:puti_frequencyinsaturday].present? 
  @busroute_results = @busroute_results.where( "puti_frequencyInWeekday.value" =>  params[:puti_frequencyinweekday]     ) if params[:puti_frequencyinweekday].present? 
  @busroute_results = @busroute_results.where( "puti_typeOfBus.value"      =>  params[:puti_typeofbus]      ) if params[:puti_typeofbus].present? 
  @busroute_results = @busroute_results.where( "puti_nameOfBusRoute.value" =>  params[:puti_nameofbusroute] ) if params[:puti_nameofbusroute].present? 
  @busroute_results = @busroute_results.where( "puti_operator.value"       =>  params[:puti_operator]       ) if params[:puti_operator].present? 
  @busroute_results = @busroute_results.where( "dc_title.value"            =>  params[:dc_title]            ) if params[:dc_title].present? 

  @busroute_results.each do |i|

    @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                  {
                                    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                             "type"  => "uri" } ],
                                    "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                    "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                    "type" => "literal"}],
                                    "http://opendata.ubin.jp/puti#NameOfBusRoute"  => i[:puti_nameOfBusRoute],
                                    "http://opendata.ubin.jp/puti#TypeOfBus"       => i[:puti_typeOfBus],
                                    "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                    "http://opendata.ubin.jp/puti#NameOfBusRoute"  => i[:puti_nameOfBusRoute]
                                  }
                                })

  end # @busroute_results.each do |i|


 @results_all = @station_results + @busstop_results + @railroad_results + @busroute_results
 @results_diag  = @station_diagram_results + @busstop_diagram_results

 if @results_all.length == 0 and @results_diag.length == 0 
      head :not_found and return
 end
  

  str = JSON.generate(@results['results'])
  str2 =  JSON.utf8_to_json_ascii(str)
  str2.gsub!(%r\\"/,'"')
  str2.gsub!(%r\\u/,'u')
  str2.gsub!(%ru([0-9a-f][0-9a-f][0-9a-f][0-9a-f])/,'\U\1')

  data = RDF::Reader.for(:json).new(str2)

  sio = StringIO.new('')

  RDF::Writer.for(:rdfxml).dump( data, sio, :standard_prefixes=>true )

  respond_to do |format|
    format.json      {  render :json => JSON.pretty_generate(@results) } 
    format.xml       {  render :xml  => sio.string.gsub(%rU([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")} }
  end
  
end
show() click to toggle source
# File app/controllers/datapoints_controller.rb, line 184
def show

  @results = { 'results' => {}, 'remains'  => false}

  if params[:id].present?
    ucode = params[:id].sub("urn:ucode:_","").sub("ucode_","")
    
    @diagrams_all  = Busstopdiagram.only('ucode').where( "title_ucode" => ucode )
    @diagrams_all += Stationdiagram.only('ucode').where( "title_ucode" => ucode )

    @diagrams = []

    @diagrams_all.each do |i|
      @diagrams.push( { "value" => "https://api.opendata.ubin.jp/api/v1/diagram/urn:ucode:_" + i.ucode, 
                        "type"  => "uri"
                      })
    end
    
    if (@results_all  = Station.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then
      
      @results_all.each do |i|
        
        @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                     {
                                       "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                "type"  => "uri" } ],
                                       "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                       "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                       "type" => "literal"}],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                       "http://opendata.ubin.jp/puti#OperatorCode"    => i[:puti_operatorCode],
                                       "http://opendata.ubin.jp/puti#NameOfRailway"   => i[:puti_nameOfRailway],
                                       "http://opendata.ubin.jp/puti#NameOfStation"   => i[:puti_nameOfStation],
                                       "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                       "http://opendata.ubin.jp/puti#YearOfTogether"  => i[:puti_yearOfTogether],
                                       "http://opendata.ubin.jp/puti#YearOfBegin"     => i[:puti_yearOfBegin]
                                     }
                                   })
        
        if @diagrams.size > 0

          @results['results'][ "urn:ucode:_" + i.ucode ].merge!(
                                                               {
                                                                 "http://opendata.ubin.jp/puti#StationDiagram"  => @diagrams
                                                               }
                                                               )
          
        end # @diagrams.size > 0
        
      end #  @results_all.each do |i|
      
    elsif (@results_all = Busstop.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then

      @results_all.each do |i|

        @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                     {
                                       "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                              "type"  => "uri" } ],
                                       "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                       "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                       "type" => "literal"}],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                       "http://opendata.ubin.jp/puti#NameOfBusStop"   => i[:puti_nameOfBusStop],
                                       "http://opendata.ubin.jp/puti#TypeOfBus"       => i[:puti_typeOfBus],
                                       "http://opendata.ubin.jp/puti#Operators"       => i[:puti_operators],
                                       "http://opendata.ubin.jp/puti#NameOfBusRoutes" => i[:puti_nameOfBusRoutes]
                                     }
                                   })
        if @diagrams.size > 0
          
          @results['results'][ "urn:ucode:_" + i.ucode ].merge!(
                                                               {
                                                                 "http://opendata.ubin.jp/puti#BusStopDiagram"  => @diagrams
                                                               }
                                                               )
        end # if @diagrams.size > 0
      end # @results_all.each do |i|

    elsif ( @results_all = Railroad.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then

      @results_all.each do |i|
      
        @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                     {
                                       "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                "type"  => "uri" } ],
                                       "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                       "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                       "type" => "literal"}],
                                       "http://opendata.ubin.jp/puti#OperatorCode"    => i[:puti_operatorCode],
                                       "http://opendata.ubin.jp/puti#NameOfRailway"   => i[:puti_nameOfRailway],
                                       "http://opendata.ubin.jp/puti#Operator"        => i[:puti_operator],
                                       "http://opendata.ubin.jp/puti#YearOfTogether"  => i[:puti_yearOfTogether],
                                       "http://opendata.ubin.jp/puti#YearOfBegin"     => i[:puti_yearOfBegin]
                                     }
                                   })

      end # @results_all.each do |i|

    elsif ( @results_all = Busroute.where( "ucode" =>  ucode ) and @results_all.length == 1 )

      @results_all.each do |i|

        @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                     {
                                       "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [ { "value" => "http://uidcenter.org/vocab/ucr/" + i.rdf_type[0].value.sub(":","#") ,
                                                                                                "type"  => "uri" } ],
                                       "http://purl.org/dc/elements/1.1/title" => i[:dc_title],
                                       "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "http://opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                       "type" => "literal"}],
                                       "http://opendata.ubin.jp/puti#TypeOfBus"           => i[:puti_typeOfBus],
                                       "http://opendata.ubin.jp/puti#Operator"            => i[:puti_operator],
                                       "http://opendata.ubin.jp/puti#NameOfBusRoute"      => i[:puti_nameOfBusRoute],
                                       "http://opendata.ubin.jp/puti#FrequencyInWeekday"  => i[:puti_frequencyInWeekday],
                                       "http://opendata.ubin.jp/puti#FrequencyInSaturday" => i[:puti_frequencyInSaturday],
                                       "http://opendata.ubin.jp/puti#FrequencyInHoliday"  => i[:puti_frequencyInHoliday]
                                     }
                                   })
        if @diagrams.size > 0
          
          @results['results'][ "urn:ucode:_" + i.ucode ].merge!(
                                                               {
                                                                 "http://opendata.ubin.jp/puti#BusStopDiagram"  => @diagrams
                                                               }
                                                               )
        end # if @diagrams.size > 0
      end # @results_all.each do |i|

    elsif (@results_all = Kokosil.where( "ucode" =>  %r#{ucode}/ ) and @results_all.length == 1 ) then

      @results_all.each do |i|

        @results['results'].merge!( { "urn:ucode:_" + i.ucode =>
                                     {
                                       "http://uidcenter.org/vocab/ucr/ug#place" => [{"value" => "https://api.opendata.ubin.jp/api/v1/places/urn:ucode:_" + i.ucode + ".geojson",
                                                                                       "type" => "literal"}],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#lat"  => i[:w3cgeo_lat],
                                       "http://www.w3.org/2003/01/geo/wgs84_pos#long" => i[:w3cgeo_long],
                                     }
                                    })

        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                       "http://purl.org/dc/elements/1.1/title"   => i[:dc_title],
                                                             }) if i[:dc_title].present?
        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://purl.org/dc/elements/1.1/time"   => i[:dc_time],
                                                              }) if i[:dc_time].present?
        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                              "http://uidcenter.org/vocab/ucr/ug#type"  => i[:ug_type],
                                                             }) if i[:ug_type].present?
        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                              "http://uidcenter.org/vocab/ucr/ug#floor" => i[:ug_floor],
                                                             }) if i[:ug_floor].present?

        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                               "http://uidcenter.org/vocab/ucr/uc#temperature" => i[:uc_temperature]
                                                             }) if i[:uc_temperature].present?
        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                               "http://uidcenter.org/vocab/ucr/uc#pollen" => i[:uc_pollen]
                                                             }) if i[:uc_pollen].present?
        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                               "http://uidcenter.org/vocab/ucr/uc#humidity" => i[:uc_humidity]
                                                             }) if i[:uc_humidity].present?
        @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                               "http://uidcenter.org/vocab/ucr/uc#address" => i[:ug_address]
                                                             }) if i[:ug_address].present?

        if i[:rdf_type].present?

          if %r^ug:(\w+)/ =~ i['rdf_type'][0]['value'].to_s
            uri = "http://uidcenter.org/vocab/ucr/ug#" + $1
          elsif %r^puti:(\w+)/ =~ i['rdf_type'][0]['value'].to_s
            uri = "http://opendata.ubin.jp/puti#" + $1
          end

          @results['results']["urn:ucode:_" + i.ucode].merge!({
                                                                "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [
                                                                                                                      { "value" => uri,
                                                                                                                        "type"  => "uri"
                                                                                                                      }
                                                                                                                     ]
                                                              }) unless uri.nil?
        end # i[:rdf_type].present 

      end # @results_all.each do |i|
    end # if (@results_all  = Station.where( "ucode" =>  ucode ) and @results_all.length == 1 ) then


    if @results_all.length == 0
      head :not_found and return
    end

    str = JSON.generate(@results['results'])
    str2 =  JSON.utf8_to_json_ascii(str)
    str2.gsub!(%r\\"/,'"')
    str2.gsub!(%r\\u/,'u')
    str2.gsub!(%ru([0-9a-f][0-9a-f][0-9a-f][0-9a-f])/,'\U\1')

    data = RDF::Reader.for(:json).new(str2)

    sio = StringIO.new('')

    RDF::Writer.for(:rdfxml).dump( data, sio, :standard_prefixes=>true )

    respond_to do |format|
      format.json      {  render :json => JSON.pretty_generate(@results) } 
      format.xml       {  render :xml  => sio.string.gsub(%rU([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")} }
    end

  else
    head :bad_request
  end
end