iTSangar
4/3/2015 - 5:36 AM

api_helpers.rb

require 'unirest'
require 'multi_json'

class APIHelpers

  def survey_job
    response = Unirest.get('http://localhost/~iTSangar/api/v2/surveys/status/')
    body_get = symbolize(response.body)
    json_response = Hash.new

    if body_get[:total_active].nonzero?
      json_response[:message] = 'nothing to modify'
      body_get[:rows].map do |survey|
        today = Time.now.strftime('%Y-%m-%d')
        survey_date = Time.parse(survey[:finished_at]).strftime('%Y-%m-%d')
        if survey_date < today
          json_response[:message] = 'something has been modified'
          post = Unirest.post("http://localhost/~iTSangar/api/v2/surveys/status/#{survey[:id]}")
          body_post = symbolize(post.body)
          result = { id: survey[:id], code_post: post.code, error_post: body_post[:error], message_post: body_post[:message] }
          if json_response.has_key?(:surveys)
            json_response[:surveys] << result
          else
            json_response[:surveys] = Array.new
            json_response[:surveys] << result
          end
        end
      end
    else
      json_response[:message] = 'no active surveys'
    end

    MultiJson.dump(json_response)
  end
//return from GET in http://localhost/~iTSangar/api/v2/surveys/status/

{
    "error": false,
    "total_active": 10,
    "rows": [
        {
            "id": 1,
            "status": 0,
            "finished_at": "2015-04-27 00:00:00"
        },
        {
            "id": 2,
            "status": 0,
            "finished_at": "2015-04-27 00:00:00"
        },
        {
            "id": 3,
            "status": 0,
            "finished_at": "2015-04-02 00:00:00"
        },
        {
            "id": 7,
            "status": 0,
            "finished_at": "2022-12-22 00:00:00"
        },
        {
            "id": 8,
            "status": 0,
            "finished_at": "2014-05-22 00:00:00"
        },
        {
            "id": 9,
            "status": 0,
            "finished_at": "2014-05-22 00:00:00"
        },
        {
            "id": 10,
            "status": 0,
            "finished_at": "2014-05-22 00:00:00"
        },
        {
            "id": 11,
            "status": 0,
            "finished_at": "2014-05-22 00:00:00"
        },
        {
            "id": 12,
            "status": 0,
            "finished_at": "2014-05-22 00:00:00"
        },
        {
            "id": 13,
            "status": 0,
            "finished_at": "2014-05-22 00:00:00"
        }
    ]
}

//--------------------------------------------------------------------------------------------------------------------
//return from POST in http://localhost/~iTSangar/api/v2/surveys/status/:id

{
    "error": false,
    "message": "Survey finalized"
}

//--------------------------------------------------------------------------------------------------------------------
//return from method call survey_job

{
  message: "no active surveys"
}

//or

{
  message: "nothing to modify"
}

//or

{
  message: "something has been modified",
  surveys: [
    {
      id: 3,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    },
    {
      id: 8,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    },
    {
      id: 9,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    },
    {
      id: 10,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    },
    {
      id: 11,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    },
    {
      id: 12,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    },
    {
      id: 13,
      code_post: 200,
      error_post: false,
      message_post: "Survey finalized"
    }
  ]
}