• new pages with sleep
    require 'csv'
    BASE_URL = 'https://gurucomm.freshsales.io/leads'
    app_token = ENV['FRESHSALES_APP_TOKEN']
    headers = {
      'Authorization': "Token token=#{app_token}"
    }
    pages =  1..72
    pages.each do |page|
      params = {
        include: 'lead_stage,owner,creater,updater,territory,source,lead_reason,campaign,lead_score_information,emails',
        page: page,
        perPage: '25',
        segment_id: '3000844033',
        sort: 'created_at',
        sort_type: 'desc'
      }
      request_args = {
        payload: params,
        headers: headers,
        url: BASE_URL,
        method: :get
      }
      response = RestClient::Request.execute(request_args)
      leads = JSON.parse(response).dig('leads')
      output_path = '/Users/guru/Desktop/no-serviceability.csv'
      CSV.open(output_path, 'ab') do |csv|
        leads.each do |lead|
          custom_field = lead['custom_field']
          csv << [
            lead['id'],
            lead['email'],
            lead['mobile_number'],
            lead['first_name'],
            lead['last_name'],
            custom_field['cf_latitude'],
            custom_field['cf_longitude'],
            custom_field['cf_comment'],
            lead['created_at'],
            lead['updated_at'],
            lead['medium']
          ]
        end
      end
      sleep 0.4
    end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment