使用Spaceship :: Tunes时出现导轨错误

时间:2016-10-20 14:19:14

标签: ruby-on-rails ruby fastlane

在我正在运行的rails应用程序中:

54    def itunes_all_apps
55      begin
56        Spaceship::Tunes.login(params[:itunes_username], params[:itunes_password])
57        apps = Spaceship::Tunes::Application.all
58        render json: apps.to_json, status: 200
59      rescue => e
60        render json: {error: e}.to_json, status: 500
61      end
62    end

它返回状态500错误,每次都没有其他信息。

然而,如果我略微改变这一点,例如获得团队(注意,来自Spaceship,而不是Spaceship :: Tunes),这可以正常工作:

def itunes_all_apps
  begin
    spaceship = Spaceship.login(params[:itunes_username], params[:itunes_password])
    teams = spaceship.teams
    render json: teams.to_json, status: 200
  rescue => e
    render json: {error: e}.to_json, status: 500
  end
end

我没有使用任何快速文件或配置或任何东西。只需通过api调用传递用户名和密码,然后尝试回复。我是rails的新手,因此可能是我提供的Spaceship示例的实现。

使用宇宙飞船0.36.1宝石(最新)

我通过文档倾注无济于事。抓住我做错的任何线索。

http://www.rubydoc.info/gems/spaceship/Spaceship/Tunes

https://github.com/fastlane/fastlane/blob/master/spaceship/docs/iTunesConnect.md

有人建议我在irb中运行这两个命令,我做了,他们工作得很完美!

Spaceship::Tunes.login('myAppleId', 'myPassword')
Spaceship::Tunes::Application.all

因此,它不是iTunes帐户问题或凭据问题(因为它在irb中工作),路由问题(因为我在上面使用相同的路由运行两个rails方法)或params问题(因为我运行了两个rails方法上面有相同的参数名称。)

我真的很感激任何建议。感谢。

修改

评论开始,救援和出错时,堆栈跟踪如下:

2016-10-24T17:47:34.974650+00:00 app[web.1]: Started POST "/api/v1/users/13/itunes_all_apps" for 162.237.102.13 at 2016-10-24 17:47:34 +0000
2016-10-24T17:47:34.977478+00:00 app[web.1]: Processing by Api::V1::UsersController#itunes_all_apps as JSON
2016-10-24T17:47:34.977521+00:00 app[web.1]:   Parameters: {"itunes_username"=>"myCorrectUsername", "itunes_password"=>"[FILTERED]", "team_id"=>"myCorrectTeamId", "id"=>"13", "user"=>{}}
2016-10-24T17:47:35.629629+00:00 heroku[router]: at=info method=POST path="/api/v1/users/13/itunes_all_apps" host=myHerokuApp.herokuapp.com request_id=002d906d-354e-4633-8b54-71aa5181e3a7 fwd="161.237.102.13" dyno=web.1 connect=2ms service=657ms status=500 bytes=259
2016-10-24T17:47:35.619597+00:00 app[web.1]: Completed 500 Internal Server Error in 642ms (ActiveRecord: 0.0ms)
2016-10-24T17:47:35.620430+00:00 app[web.1]: 
2016-10-24T17:47:35.620432+00:00 app[web.1]: IOError (not opened for reading):
2016-10-24T17:47:35.620434+00:00 app[web.1]: 
2016-10-24T17:47:35.620433+00:00 app[web.1]:   app/controllers/api/v1/users_controller.rb:58:in `itunes_all_apps'

1 个答案:

答案 0 :(得分:2)

似乎Spaceship::Fastlane::Application没有实现as_json方法,默认的as_json触及一些IO对象,不能表示为json。

我的建议是创建JSON序列化程序。您可以使用active_model-serializer,但如果您不想仅为一个对象创建依赖项,则可以创建自己的序列化程序。

class SpaceshipApplicationSerializer
  attr_reader :spaceship_applications

  def initialize(spaceship_applications)
    @spaceship_applications = spaceship_applications
  end

  def as_json(options = {})
    spaceship_applications.each_with_object([]) do |spaceship_application, memo|
      memo << object_as_json(spaceship_application)
    end
  end

  def object_as_json(object)
    attributes.each_with_object({}) do |attribute, memo|
      memo[attribute] = object.send(attribute)
    end
  end

  def attributes
    [
      :apple_id,
      :name,
      :vendor_id,
      :bundle_id,
      :last_modified,
      :issues_count,
      :app_icon_preview_url
    ]
  end
end

#在你的控制器中

def itunes_all_apps
  begin
    Spaceship::Tunes.login(params[:itunes_username], params[:itunes_password])
    apps = Spaceship::Tunes::Application.all
    render json: SpaceshipApplicationSerializer.new(apps).to_json, status: 200
  rescue => e
    render json: {error: e}.to_json, status: 500
  end
end

修改

是的,这些类返回一个数组,但是数组中的实际对象与json不能很好地匹配。很难说问题是否与库有关 - 一方面Spaceship::Tunes::Application没有返回正确的json表示是一个缺失的特性,但如果to_json引发异常(类响应的方法) - 然后我会说那是一个错误。

创建自己的序列化程序以按照您希望的方式构建json表示 - 是一种常见的模式。