是否有通过 Ruby 使用 LocationLabs 或 Loc-Aid 的开源示例?

发布于 2024-11-03 23:57:33 字数 237 浏览 1 评论 0原文

LocationLabs and Loc-Aid are location aggregation services that expose REST APIs. They currently offer Java, .NET and PHP SDKs. The API is not complex but, still, as a learning tool, it would be nice to have a Ruby tutorial or example to play with, extent, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-11-10 23:57:33

经过进一步分析,我在 Loc-Aid 的基础设施上构建了我的应用程序。由于我找不到很多 Ruby 示例,因此我分享了我的应用程序中的一个片段。

  # Get SAVON soap client locaid location services
  # Parameters:
  #   - none
  # Returns: SOAP client for locaid location services
  def get_location_client
    Savon::Client.new do
      wsdl.document = LOCAID_CONFIG['use_local_wsdl'] ?
        File.expand_path(LOCAID_CONFIG['get_location_wsdl'].to_s, ::Rails.root.to_s) :
        LOCAID_CONFIG['get_location_wsdl'].to_s
      wsdl.endpoint = LOCAID_CONFIG['get_location_endpoint'].to_s
    end
  end


  # Strip the return result from locaid response as a hash
  # Parameters:
  #   - raw_response: Raw response XMLfrom locaid services
  #   - response_name: Response name which wrap the response return result in locaid response XML
  # Returns: Hash corresponding to the key "return" in locaid soap response hash.
  # Sample Raw Response:
  #   {:subscribe_phone_response=>{:return=>{:error=>{:error_code=>"00001", :error_message=>"Invalid or inactive user"}, :transaction_id=>"14028251"},
  #       :"@xmlns:ns2"=>"http://webservice.portico.locaid.net/"}}
  def strip_locaid_return(raw_response, response_name)
    unless raw_response.to_hash.has_key?(response_name)
      raise TropoExceptions::ExternalError
    end
    raw_response[response_name][:return]
  end

  # Get location from locaid by the caller id
  # Parameters:
  #     - @caller_id: Caller id get from scope value
  # Returns: none
  def location_from_locaid
    client = get_location_client
    client.http.read_timeout = LOCAID_CONFIG['get_location_timeout'].to_i
    # Call locaider service to get location
    response = client.request :wsdl, :get_locations_x do |soap|
      soap.body = {
          :login  => LOCAID_CONFIG['login'],
          :password  => LOCAID_CONFIG['password'],
          :class_id => LOCAID_CONFIG['class_id'],
          :msisdn_list => ["1#{@caller_id}"],
          :coor_type => "DECIMAL",
          :location_method => LOCAID_CONFIG['location_method'],
          :sync_type => "SYN",
          :overage => "1"
      }
    end

    result_hash = strip_locaid_return(response, :get_locations_x_response)
    if result_hash.has_key?(:error)
      raise TropoExceptions::ExternalError
    end

    yield result_hash[:location_response].is_a?(Array) ?
        result_hash[:location_response][0] :
        result_hash[:location_response]

  rescue Savon::Error, Timeout::Error => e
    logger.error e
    yield nil
  end
end

After further analysis, I built my application on Loc-Aid's infrastructure. Since there weren't many Ruby examples I could find, I'm sharing a snippet from my app.

  # Get SAVON soap client locaid location services
  # Parameters:
  #   - none
  # Returns: SOAP client for locaid location services
  def get_location_client
    Savon::Client.new do
      wsdl.document = LOCAID_CONFIG['use_local_wsdl'] ?
        File.expand_path(LOCAID_CONFIG['get_location_wsdl'].to_s, ::Rails.root.to_s) :
        LOCAID_CONFIG['get_location_wsdl'].to_s
      wsdl.endpoint = LOCAID_CONFIG['get_location_endpoint'].to_s
    end
  end


  # Strip the return result from locaid response as a hash
  # Parameters:
  #   - raw_response: Raw response XMLfrom locaid services
  #   - response_name: Response name which wrap the response return result in locaid response XML
  # Returns: Hash corresponding to the key "return" in locaid soap response hash.
  # Sample Raw Response:
  #   {:subscribe_phone_response=>{:return=>{:error=>{:error_code=>"00001", :error_message=>"Invalid or inactive user"}, :transaction_id=>"14028251"},
  #       :"@xmlns:ns2"=>"http://webservice.portico.locaid.net/"}}
  def strip_locaid_return(raw_response, response_name)
    unless raw_response.to_hash.has_key?(response_name)
      raise TropoExceptions::ExternalError
    end
    raw_response[response_name][:return]
  end

  # Get location from locaid by the caller id
  # Parameters:
  #     - @caller_id: Caller id get from scope value
  # Returns: none
  def location_from_locaid
    client = get_location_client
    client.http.read_timeout = LOCAID_CONFIG['get_location_timeout'].to_i
    # Call locaider service to get location
    response = client.request :wsdl, :get_locations_x do |soap|
      soap.body = {
          :login  => LOCAID_CONFIG['login'],
          :password  => LOCAID_CONFIG['password'],
          :class_id => LOCAID_CONFIG['class_id'],
          :msisdn_list => ["1#{@caller_id}"],
          :coor_type => "DECIMAL",
          :location_method => LOCAID_CONFIG['location_method'],
          :sync_type => "SYN",
          :overage => "1"
      }
    end

    result_hash = strip_locaid_return(response, :get_locations_x_response)
    if result_hash.has_key?(:error)
      raise TropoExceptions::ExternalError
    end

    yield result_hash[:location_response].is_a?(Array) ?
        result_hash[:location_response][0] :
        result_hash[:location_response]

  rescue Savon::Error, Timeout::Error => e
    logger.error e
    yield nil
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文