OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / provider_status.rb
index 0c71768..e95f979 100644 (file)
@@ -1,12 +1,17 @@
 class ProviderStatus < ActiveRecord::Base
   has_one :provider
   
-#  validates :token
+  validates :token, :format => {:with => /\a[a-zA-Z0-9]+\z/, :allow_blank => true}
   validates :receive_hour1, :numericality => {:allow_blank => true}
   validates :receive_hour2, :numericality => {:allow_blank => true}
 #  validates :received_at
 #  validates :stopped_at
   
+  before_save do |r|
+    r.token = r.token.gsub(/\s/, '') if r.token
+    r.token = nil if r.token.blank?
+  end
+  
   def supply_default
   end
   
@@ -72,6 +77,14 @@ class ProviderStatus < ActiveRecord::Base
     ProviderStatus.find(:all, opt)
   end
   
+  def self.approve_list page = 1, page_size = self.default_page_size
+    opt = {}
+    opt.merge!(ProviderStatus.list_opt)
+    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
+    opt.merge!({:conditions => ['provider_statuses.token is not null'], :order => 'providers.name'})
+    ProviderStatus.find(:all, opt)
+  end
+  
   def self.list_opt
     {:include => {:provider => {}} }
   end
@@ -104,4 +117,105 @@ class ProviderStatus < ActiveRecord::Base
     {:include => {:provider => {}} }
   end
   
+  def stop
+    self.token = nil
+    self.save
+  end
+  
+  def ymd_to_time ymd_str
+    date = nil
+    if ymd_str.blank?
+      date = if self.received_at
+        self.received_at
+      else
+        nil
+      end
+    else
+      begin
+        date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
+      rescue
+        date = nil
+      end
+    end
+    date
+  end
+  
+  def export_url action, date
+    u = self.provider.export_url action
+    prm = '?auth_token=' + self.token
+    prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
+    u = URI.join(u, prm)
+    u.to_s
+  end
+  
+  def export_from_provider url
+    res = nil
+    begin
+      json = RestClient.get url
+      res = JSON.parse json
+    rescue
+    end
+    res
+  end
+  
+  def export_by action, ymd
+    t = ymd_to_time ymd
+    url = export_url action, t
+    export_from_provider(url)
+  end
+  
+  def licenses_import date
+    licenses = self.export_by('licenses_export', date)
+    if  licenses
+      ProviderLicense.import self.provider.id, licenses
+    else
+      LicenseImportResult.new nil
+    end
+  end
+  
+  def artists_import date
+    artists = self.export_by('artists_export', date)
+    if artists
+      ProviderArtist.import self.provider.id, artists
+    else
+      ArtistImportResult.new nil
+    end
+  end
+  
+  def original_pictures_import date
+    original_pictures = self.export_by('original_pictures_export', date)
+    pictures = self.export_by('pictures_export', date)
+    if original_pictures and pictures
+      ProviderOriginalPicture.import self.provider.id, original_pictures, pictures
+    else
+      OriginalPictureImportResult.new nil
+    end
+  end
+  
+  def import date
+    import_result = nil
+    ProviderStatus.transaction do
+      import_result = self.licenses_import date
+      raise ActiveRecord::Rollback unless import_result.success?
+      import_result = self.artists_import date
+      raise ActiveRecord::Rollback unless import_result.success?
+      import_result = self.original_pictures_import date
+      raise ActiveRecord::Rollback unless import_result.success?
+      self.received_at = Time.now
+      self.save
+    end
+    import_result
+  end
+  
+  def self.import_all date = nil
+    failures = {}
+    ProviderStatus.approve_list.each do |provider_status|
+      import_result = provider_status.import date
+      next if import_result.success?
+      failures[provider_status.provider.name] = import_result
+    end
+    failures.each do |name, import_result|
+      puts name
+    end
+  end
 end