{"action":"created","comment":{"url":"https://api.github.com/repos/olimart/yamail/pulls/comments/26861256","id":26861256,"diff_hunk":"@@ -0,0 +1,76 @@\n+module ListImporter\n+  require 'roo'\n+  COLUMN_SEPARATORS = [\",\", \";\", \"\\t\", \":\", \"|\", \" \"]\n+\n+  def import(file)\n+    @col_sep = find_col_sep(file)\n+    parsed_file = open_file(file)\n+    first_row = parsed_file.row(1)\n+    header = []\n+    @email_cell = find_email_cell(first_row)\n+\n+    if @email_cell.present?\n+      # file without header\n+      (1..first_row.size).each do |i|\n+        if @email_cell == i - 1\n+          header << 'email'\n+        else\n+          header << \"field_#{i}\"\n+        end\n+      end\n+      process(header, parsed_file, 1)\n+    else\n+      header = first_row\n+      @email_cell = find_email_cell(parsed_file.row(2))\n+      header[@email_cell] = 'email'\n+      process(header, parsed_file, 2)\n+    end\n+\n+  rescue\n+    raise StandardError, \"Invalid file format: #{file.original_filename}\"\n+  end\n+\n+  def process(header, parsed_file, first_data_row)\n+    rows = []\n+    (first_data_row..parsed_file.last_row).each do |i|\n+      rows << Hash[[header, parsed_file.row(i)].transpose]\n+    end\n+    rows\n+  end\n+\n+  def find_email_cell(first_row)\n+    first_row.each_with_index do |cell, i|\n+      if cell =~ /@/\n+        @email_cell = i\n+      end\n+    end\n+    @email_cell\n+  end\n+\n+  def open_file(file)\n+    case File.extname(file.original_filename)\n+      when \".csv\" then\n+        begin\n+          Roo::CSV.new(file.path, csv_options: {quote_char: \"\\\"\", col_sep: @col_sep})\n+        rescue CSV::MalformedCSVError\n+          raise \"Invalid file format: #{file.original_filename}\"\n+        end\n+      when \".xlsx\" then\n+        Roo::Excelx.new(file.path)\n+      when \".ods\" then","path":"app/services/list_importer.rb","position":60,"original_position":60,"commit_id":"bc8ec16743499d9efe821435ab133ae22d6e70f6","original_commit_id":"bc8ec16743499d9efe821435ab133ae22d6e70f6","user":{"login":"codeguardci","id":9791874,"avatar_url":"https://avatars.githubusercontent.com/u/9791874?v=3","gravatar_id":"","url":"https://api.github.com/users/codeguardci","html_url":"https://github.com/codeguardci","followers_url":"https://api.github.com/users/codeguardci/followers","following_url":"https://api.github.com/users/codeguardci/following{/other_user}","gists_url":"https://api.github.com/users/codeguardci/gists{/gist_id}","starred_url":"https://api.github.com/users/codeguardci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codeguardci/subscriptions","organizations_url":"https://api.github.com/users/codeguardci/orgs","repos_url":"https://api.github.com/users/codeguardci/repos","events_url":"https://api.github.com/users/codeguardci/events{/privacy}","received_events_url":"https://api.github.com/users/codeguardci/received_events","type":"User","site_admin":false},"body":"Indent `when` as deep as `case`.","created_at":"2015-03-20T17:56:24Z","updated_at":"2015-03-20T17:56:24Z","html_url":"https://github.com/olimart/yamail/pull/4#discussion_r26861256","pull_request_url":"https://api.github.com/repos/olimart/yamail/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/olimart/yamail/pulls/comments/26861256"},"html":{"href":"https://github.com/olimart/yamail/pull/4#discussion_r26861256"},"pull_request":{"href":"https://api.github.com/repos/olimart/yamail/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/olimart/yamail/pulls/4","id":31634624,"html_url":"https://github.com/olimart/yamail/pull/4","diff_url":"https://github.com/olimart/yamail/pull/4.diff","patch_url":"https://github.com/olimart/yamail/pull/4.patch","issue_url":"https://api.github.com/repos/olimart/yamail/issues/4","number":4,"state":"open","locked":false,"title":"Sending email","user":{"login":"croko","id":333912,"avatar_url":"https://avatars.githubusercontent.com/u/333912?v=3","gravatar_id":"","url":"https://api.github.com/users/croko","html_url":"https://github.com/croko","followers_url":"https://api.github.com/users/croko/followers","following_url":"https://api.github.com/users/croko/following{/other_user}","gists_url":"https://api.github.com/users/croko/gists{/gist_id}","starred_url":"https://api.github.com/users/croko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/croko/subscriptions","organizations_url":"https://api.github.com/users/croko/orgs","repos_url":"https://api.github.com/users/croko/repos","events_url":"https://api.github.com/users/croko/events{/privacy}","received_events_url":"https://api.github.com/users/croko/received_events","type":"User","site_admin":false},"body":"May be need some cosmetic changes","created_at":"2015-03-20T17:31:16Z","updated_at":"2015-03-20T17:56:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/olimart/yamail/pulls/4/commits","review_comments_url":"https://api.github.com/repos/olimart/yamail/pulls/4/comments","review_comment_url":"https://api.github.com/repos/olimart/yamail/pulls/comments{/number}","comments_url":"https://api.github.com/repos/olimart/yamail/issues/4/comments","statuses_url":"https://api.github.com/repos/olimart/yamail/statuses/bc8ec16743499d9efe821435ab133ae22d6e70f6","head":{"label":"olimart:sending_email","ref":"sending_email","sha":"bc8ec16743499d9efe821435ab133ae22d6e70f6","user":{"login":"olimart","id":547754,"avatar_url":"https://avatars.githubusercontent.com/u/547754?v=3","gravatar_id":"","url":"https://api.github.com/users/olimart","html_url":"https://github.com/olimart","followers_url":"https://api.github.com/users/olimart/followers","following_url":"https://api.github.com/users/olimart/following{/other_user}","gists_url":"https://api.github.com/users/olimart/gists{/gist_id}","starred_url":"https://api.github.com/users/olimart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olimart/subscriptions","organizations_url":"https://api.github.com/users/olimart/orgs","repos_url":"https://api.github.com/users/olimart/repos","events_url":"https://api.github.com/users/olimart/events{/privacy}","received_events_url":"https://api.github.com/users/olimart/received_events","type":"User","site_admin":false},"repo":{"id":30719187,"name":"yamail","full_name":"olimart/yamail","owner":{"login":"olimart","id":547754,"avatar_url":"https://avatars.githubusercontent.com/u/547754?v=3","gravatar_id":"","url":"https://api.github.com/users/olimart","html_url":"https://github.com/olimart","followers_url":"https://api.github.com/users/olimart/followers","following_url":"https://api.github.com/users/olimart/following{/other_user}","gists_url":"https://api.github.com/users/olimart/gists{/gist_id}","starred_url":"https://api.github.com/users/olimart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olimart/subscriptions","organizations_url":"https://api.github.com/users/olimart/orgs","repos_url":"https://api.github.com/users/olimart/repos","events_url":"https://api.github.com/users/olimart/events{/privacy}","received_events_url":"https://api.github.com/users/olimart/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/olimart/yamail","description":"","fork":false,"url":"https://api.github.com/repos/olimart/yamail","forks_url":"https://api.github.com/repos/olimart/yamail/forks","keys_url":"https://api.github.com/repos/olimart/yamail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/olimart/yamail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/olimart/yamail/teams","hooks_url":"https://api.github.com/repos/olimart/yamail/hooks","issue_events_url":"https://api.github.com/repos/olimart/yamail/issues/events{/number}","events_url":"https://api.github.com/repos/olimart/yamail/events","assignees_url":"https://api.github.com/repos/olimart/yamail/assignees{/user}","branches_url":"https://api.github.com/repos/olimart/yamail/branches{/branch}","tags_url":"https://api.github.com/repos/olimart/yamail/tags","blobs_url":"https://api.github.com/repos/olimart/yamail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/olimart/yamail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/olimart/yamail/git/refs{/sha}","trees_url":"https://api.github.com/repos/olimart/yamail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/olimart/yamail/statuses/{sha}","languages_url":"https://api.github.com/repos/olimart/yamail/languages","stargazers_url":"https://api.github.com/repos/olimart/yamail/stargazers","contributors_url":"https://api.github.com/repos/olimart/yamail/contributors","subscribers_url":"https://api.github.com/repos/olimart/yamail/subscribers","subscription_url":"https://api.github.com/repos/olimart/yamail/subscription","commits_url":"https://api.github.com/repos/olimart/yamail/commits{/sha}","git_commits_url":"https://api.github.com/repos/olimart/yamail/git/commits{/sha}","comments_url":"https://api.github.com/repos/olimart/yamail/comments{/number}","issue_comment_url":"https://api.github.com/repos/olimart/yamail/issues/comments{/number}","contents_url":"https://api.github.com/repos/olimart/yamail/contents/{+path}","compare_url":"https://api.github.com/repos/olimart/yamail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/olimart/yamail/merges","archive_url":"https://api.github.com/repos/olimart/yamail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/olimart/yamail/downloads","issues_url":"https://api.github.com/repos/olimart/yamail/issues{/number}","pulls_url":"https://api.github.com/repos/olimart/yamail/pulls{/number}","milestones_url":"https://api.github.com/repos/olimart/yamail/milestones{/number}","notifications_url":"https://api.github.com/repos/olimart/yamail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/olimart/yamail/labels{/name}","releases_url":"https://api.github.com/repos/olimart/yamail/releases{/id}","created_at":"2015-02-12T19:20:58Z","updated_at":"2015-02-24T06:07:43Z","pushed_at":"2015-03-20T17:31:17Z","git_url":"git://github.com/olimart/yamail.git","ssh_url":"git@github.com:olimart/yamail.git","clone_url":"https://github.com/olimart/yamail.git","svn_url":"https://github.com/olimart/yamail","homepage":null,"size":376,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":0,"default_branch":"master"}},"base":{"label":"olimart:master","ref":"master","sha":"4cece0b4658cf95a5df91a832099b6c4891d4f49","user":{"login":"olimart","id":547754,"avatar_url":"https://avatars.githubusercontent.com/u/547754?v=3","gravatar_id":"","url":"https://api.github.com/users/olimart","html_url":"https://github.com/olimart","followers_url":"https://api.github.com/users/olimart/followers","following_url":"https://api.github.com/users/olimart/following{/other_user}","gists_url":"https://api.github.com/users/olimart/gists{/gist_id}","starred_url":"https://api.github.com/users/olimart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olimart/subscriptions","organizations_url":"https://api.github.com/users/olimart/orgs","repos_url":"https://api.github.com/users/olimart/repos","events_url":"https://api.github.com/users/olimart/events{/privacy}","received_events_url":"https://api.github.com/users/olimart/received_events","type":"User","site_admin":false},"repo":{"id":30719187,"name":"yamail","full_name":"olimart/yamail","owner":{"login":"olimart","id":547754,"avatar_url":"https://avatars.githubusercontent.com/u/547754?v=3","gravatar_id":"","url":"https://api.github.com/users/olimart","html_url":"https://github.com/olimart","followers_url":"https://api.github.com/users/olimart/followers","following_url":"https://api.github.com/users/olimart/following{/other_user}","gists_url":"https://api.github.com/users/olimart/gists{/gist_id}","starred_url":"https://api.github.com/users/olimart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olimart/subscriptions","organizations_url":"https://api.github.com/users/olimart/orgs","repos_url":"https://api.github.com/users/olimart/repos","events_url":"https://api.github.com/users/olimart/events{/privacy}","received_events_url":"https://api.github.com/users/olimart/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/olimart/yamail","description":"","fork":false,"url":"https://api.github.com/repos/olimart/yamail","forks_url":"https://api.github.com/repos/olimart/yamail/forks","keys_url":"https://api.github.com/repos/olimart/yamail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/olimart/yamail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/olimart/yamail/teams","hooks_url":"https://api.github.com/repos/olimart/yamail/hooks","issue_events_url":"https://api.github.com/repos/olimart/yamail/issues/events{/number}","events_url":"https://api.github.com/repos/olimart/yamail/events","assignees_url":"https://api.github.com/repos/olimart/yamail/assignees{/user}","branches_url":"https://api.github.com/repos/olimart/yamail/branches{/branch}","tags_url":"https://api.github.com/repos/olimart/yamail/tags","blobs_url":"https://api.github.com/repos/olimart/yamail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/olimart/yamail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/olimart/yamail/git/refs{/sha}","trees_url":"https://api.github.com/repos/olimart/yamail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/olimart/yamail/statuses/{sha}","languages_url":"https://api.github.com/repos/olimart/yamail/languages","stargazers_url":"https://api.github.com/repos/olimart/yamail/stargazers","contributors_url":"https://api.github.com/repos/olimart/yamail/contributors","subscribers_url":"https://api.github.com/repos/olimart/yamail/subscribers","subscription_url":"https://api.github.com/repos/olimart/yamail/subscription","commits_url":"https://api.github.com/repos/olimart/yamail/commits{/sha}","git_commits_url":"https://api.github.com/repos/olimart/yamail/git/commits{/sha}","comments_url":"https://api.github.com/repos/olimart/yamail/comments{/number}","issue_comment_url":"https://api.github.com/repos/olimart/yamail/issues/comments{/number}","contents_url":"https://api.github.com/repos/olimart/yamail/contents/{+path}","compare_url":"https://api.github.com/repos/olimart/yamail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/olimart/yamail/merges","archive_url":"https://api.github.com/repos/olimart/yamail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/olimart/yamail/downloads","issues_url":"https://api.github.com/repos/olimart/yamail/issues{/number}","pulls_url":"https://api.github.com/repos/olimart/yamail/pulls{/number}","milestones_url":"https://api.github.com/repos/olimart/yamail/milestones{/number}","notifications_url":"https://api.github.com/repos/olimart/yamail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/olimart/yamail/labels{/name}","releases_url":"https://api.github.com/repos/olimart/yamail/releases{/id}","created_at":"2015-02-12T19:20:58Z","updated_at":"2015-02-24T06:07:43Z","pushed_at":"2015-03-20T17:31:17Z","git_url":"git://github.com/olimart/yamail.git","ssh_url":"git@github.com:olimart/yamail.git","clone_url":"https://github.com/olimart/yamail.git","svn_url":"https://github.com/olimart/yamail","homepage":null,"size":376,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":4,"forks":0,"open_issues":4,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/olimart/yamail/pulls/4"},"html":{"href":"https://github.com/olimart/yamail/pull/4"},"issue":{"href":"https://api.github.com/repos/olimart/yamail/issues/4"},"comments":{"href":"https://api.github.com/repos/olimart/yamail/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/olimart/yamail/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/olimart/yamail/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/olimart/yamail/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/olimart/yamail/statuses/bc8ec16743499d9efe821435ab133ae22d6e70f6"}}}}