reso 0.1.3.0 → 0.1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40d73be58419384a1c36b0cf68410db55bc95abb
4
- data.tar.gz: d73d8c8395ce5c5829ad20d9f2f5b416a584107c
3
+ metadata.gz: b45456599ff2b14aec9e246637f1e2968a82d97f
4
+ data.tar.gz: f3ec499d115470a13d3e367c8cf28294cfd5c984
5
5
  SHA512:
6
- metadata.gz: 1c98555ad280f777976132930c82db9bc6d7bbfcc8f917443c524d9130c8fd9961c44623c15c15c0a2abb6562935b59a4b6550bdd8330531d1370fe813b4be49
7
- data.tar.gz: 3cfe39b4c852971963c92e76d9d34678e52a21a8022b93b7c3ee97f58bd53c50f4999819886ab33d17bbc85c7214aa084f00be0897d02998dd101811f3f2746f
6
+ metadata.gz: 75d3ff3bdb12d2948adb1506f6d4c46bfa5c7ba0c21e1f8524897e55d678174fe785c5d817fc02133a2cab2955a02a50c0fa1fe163dc081ae3bada94c781ad3d
7
+ data.tar.gz: abf3cb025a5d8fee6ce83b02fe8bd235cdfd7828fcaf7b150f3f8a5673433f8a169dc89c7b56bdd14c7c3697329857579e54a86e7308481fd1ca9217513b5530
@@ -1,5 +1,6 @@
1
1
  class Import < ActiveRecord::Base
2
2
 
3
+ require 'nokogiri'
3
4
  require 'open-uri'
4
5
  require 'open_uri_redirections'
5
6
 
@@ -34,4 +35,101 @@ class Import < ActiveRecord::Base
34
35
  self.source_data_modified.eql? self.source_url_last_modified ? false : true
35
36
  end
36
37
 
38
+ def run_import
39
+ if self.status == 'active'
40
+ unless self.new_source_data_exists?
41
+ self.update_attribute(:status, :running)
42
+ source_data_modified = self.source_url_last_modified
43
+ l, count, found_listing_keys, stream = 0, 0, [], ''
44
+ open_tag, close_tag = get_open_and_closing_tag_for self.repeating_element
45
+
46
+ # Grab a file to work with
47
+ filepath = download_feed_to_import self
48
+ filepath = uncompress_and_return_new_filepath(filepath) if filepath.split('.').last.downcase == 'gz'
49
+
50
+ # Grab the XML header to avoid namespace errors later
51
+ xml_header = get_xml_header filepath, self.repeating_element
52
+
53
+ start_time = Time.now
54
+ import_result = ImportResult.create(import: self, start_time: start_time)
55
+ File.foreach(filepath) do |line|
56
+ stream += line
57
+ while (from_here = stream.index(open_tag)) && (to_there = stream.index(close_tag))
58
+ xml = stream[from_here..to_there + (close_tag.length-1)]
59
+ doc = Nokogiri::XML([xml_header, xml].join).remove_namespaces!
60
+ found_listing_keys << create_queued_listing_and_return_listing_key(doc, self)
61
+ stream.gsub!(xml, '')
62
+ end
63
+ end
64
+ end_time = Time.now
65
+ removed_listing_keys = self.remove_listings_not_present(found_listing_keys)
66
+ import_result.assign_attributes({
67
+ end_time: end_time,
68
+ found_listing_keys: found_listing_keys,
69
+ removed_listing_keys: removed_listing_keys.inspect
70
+ })
71
+ import_result.save
72
+ self.update_attribute(:status, :active)
73
+ self.update_attribute(:source_data_modified, source_data_modified)
74
+ File.delete(filepath)
75
+ end
76
+ end
77
+ end
78
+
79
+ def download_feed_to_import import
80
+ filename = import.source_url.split('/').last
81
+ filepath = Rails.root.join('tmp', filename).to_s
82
+ File.delete(filepath) if File.file? filepath
83
+ open(filepath, 'wb') do |file|
84
+ file << open(import.source_url,
85
+ http_basic_authentication: [import.source_user, import.source_pass],
86
+ allow_redirections: :all
87
+ ).read
88
+ end
89
+ filepath
90
+ end
91
+
92
+ def get_open_and_closing_tag_for repeating_element
93
+ ApplicationController.helpers.content_tag(repeating_element, "\n").split
94
+ end
95
+
96
+ def get_xml_header filepath, repeating_element
97
+ stream = ''
98
+ open_tag = get_open_and_closing_tag_for(repeating_element).first
99
+ File.foreach(filepath) do |line|
100
+ stream += line
101
+ pos = stream.index(open_tag)
102
+ return stream[0..pos-1] if pos
103
+ end
104
+ nil # Just in cases
105
+ end
106
+
107
+ def create_queued_listing_and_return_listing_key doc, import
108
+ begin
109
+ doc.css(import.repeating_element).each do |o|
110
+ listing_data = {}
111
+ Hash.from_xml(o.to_xml)[import.repeating_element].each_pair{|key, value| listing_data[key] = value }
112
+ queued_listing = QueuedListing.new(import: import, listing_data: listing_data)
113
+ queued_listing.save
114
+ return Mapper::unique_identifier(queued_listing)
115
+ end
116
+ rescue Exception => e
117
+ puts e.inspect
118
+ exit if Rails.env.development?
119
+ return nil
120
+ end
121
+ end
122
+
123
+ def uncompress_and_return_new_filepath filepath
124
+ output_path = [filepath, '.xml'].join
125
+ File.delete(output_path) if File.file? output_path
126
+ Zlib::GzipReader.open(filepath) do |gz|
127
+ File.open(output_path, "w") do |g|
128
+ IO.copy_stream(gz, g)
129
+ end
130
+ end
131
+ File.delete(filepath)
132
+ output_path
133
+ end
134
+
37
135
  end
@@ -1,3 +1,3 @@
1
1
  module Reso
2
- VERSION = "0.1.3.0"
2
+ VERSION = "0.1.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3.0
4
+ version: 0.1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Edlund