app/models/stackmap_fetcher.rb
# returns a MapInfo object OR nil if no map is available
returns a MapInfo object OR nil if no map is available
returns a MapInfo object OR nil if no map is available
def fetch_map_info
    return nil unless @holding && @holding.call_number && @holding.collection.internal_code
    base_url = "https://jhu.stackmap.com/json/"
    item_call = CGI::escape(@holding.call_number)
    item_location = @holding.collection.internal_code
    item_library = CGI::escape("Milton S. Eisenhower Library")
    request_url = "#{base_url}?callno=#{item_call}&location=#{item_location}&library=#{item_library}"
    response  =  Faraday.get(request_url)
    if response.status != 200
      map_info = MapInfo.new(:status => "A #{response.status} response we received",
                             :error => "No map is available for this item at this time.")
    else
      response_body = response.body
      stackmap = MultiJson.load(response_body)
      if stackmap["stat"] != "OK"
        map_info = MapInfo.new(:status => stackmap["stat"],
                    :error => stackmap["message"])
      elsif stackmap["stat"]== "OK"
        if  stackmap["results"]["maps"].size > 0
          map_info = MapInfo.new(:status => stackmap["stat"],
                  :map_url => "#{stackmap["results"]["maps"].first["mapurl"]}&marker=1",
                  :floor_name => "#{stackmap["results"]["maps"].first["floorname"]}",
                  :range_name => "#{stackmap["results"]["maps"].first["ranges"].first["rangename"]}" )
        elsif stackmap["results"]["maps"].size == 0
          map_info = MapInfo.new(:status => "OK but no map",
                    :error => "No map is available for this item.")
        end
      end
    end
    return map_info
  end
def fetch_map_info return nil unless @holding && @holding . call_number && @holding . collection . internal_code base_url = "https://jhu.stackmap.com/json/" item_call = CGI :: escape ( @holding . call_number ) item_location = @holding . collection . internal_code item_library = CGI :: escape ( "Milton S. Eisenhower Library" ) request_url = "#{base_url}?callno=#{item_call}&location=#{item_location}&library=#{item_library}" response = Faraday . get ( request_url ) if response . status != 200 map_info = MapInfo . new ( :status => "A #{response.status} response we received" , :error => "No map is available for this item at this time." ) else response_body = response . body stackmap = MultiJson . load ( response_body ) if stackmap [ "stat" ] != "OK" map_info = MapInfo . new ( :status => stackmap [ "stat" ] , :error => stackmap [ "message" ] ) elsif stackmap [ "stat" ] == "OK" if stackmap [ "results" ] [ "maps" ] . size > 0 map_info = MapInfo . new ( :status => stackmap [ "stat" ] , :map_url => "#{stackmap["results"]["maps"].first["mapurl"]}&marker=1" , :floor_name => "#{stackmap["results"]["maps"].first["floorname"]}" , :range_name => "#{stackmap["results"]["maps"].first["ranges"].first["rangename"]}" ) elsif stackmap [ "results" ] [ "maps" ] . size == 0 map_info = MapInfo . new ( :status => "OK but no map" , :error => "No map is available for this item." ) end end end return map_info end
returns a MapInfo object OR nil if no map is available
returns a MapInfo object OR nil if no map is available
# hard-coded, stackmap value for our account # stackmap = begin