Skip to content
This repository has been archived by the owner on Jun 25, 2019. It is now read-only.

Commit

Permalink
Update to upload news article.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyf committed Mar 15, 2011
1 parent bdb7cdc commit c6ccad8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -3,7 +3,9 @@ source "http://rubygems.org"

gem 'libxml-ruby', "1.1.4"
gem 'activerecord', '3.0.5'
gem 'activesupport', '3.0.5'
gem 'hpricot', '0.8.4'
gem 'highline'
gem 'rest-client'
gem 'mime-types'
gem 'ruby-debug'
7 changes: 5 additions & 2 deletions Gemfile.lock
Expand Up @@ -14,25 +14,28 @@ GEM
arel (2.0.9)
builder (2.1.2)
columnize (0.3.2)
highline (1.6.1)
hpricot (0.8.4)
i18n (0.5.0)
libxml-ruby (1.1.4)
linecache (0.43)
mime-types (1.16)
rest-client (1.4.2)
rest-client (1.6.1)
mime-types (>= 1.16)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
tzinfo (0.3.24)
tzinfo (0.3.25)

PLATFORMS
ruby

DEPENDENCIES
activerecord (= 3.0.5)
activesupport (= 3.0.5)
highline
hpricot (= 0.8.4)
libxml-ruby (= 1.1.4)
mime-types
Expand Down
5 changes: 3 additions & 2 deletions lib/update_content.rb
Expand Up @@ -8,11 +8,12 @@
Dir.glob(File.join(File.dirname(__FILE__), "../storage/serializations/reviews/**/*.yml")).each do |filename|
file_info = YAML.load_file(filename)

file_info['transformed_content'] = file_info['content'].dup
# Update images
file_info['images'].each do |image_path|
if target_config = asset_transformation_config[image_path]
if path = (target_config[:conductor_path] || target_config['conductor_path'])
file_info['transformed_content'] ||= file_info['content']
file_info['transformed_content'] ||= file_info['content'].dup
file_info['transformed_content'].gsub!(/(["|'])#{image_path}(["|'])/, '\1' << path << '\2')
end
end
Expand All @@ -22,7 +23,7 @@
file_info['links'].each do |link_path|
if target_config = link_transformation_config[link_path]
if path = (target_config[:target] || target_config['target'])
file_info['transformed_content'] ||= file_info['content']
file_info['transformed_content'] ||= file_info['content'].dup
file_info['transformed_content'].gsub!(/(["|'])#{link_path}(["|'])/, '\1' << path << '\2')
end
end
Expand Down
18 changes: 14 additions & 4 deletions lib/upload_assets.rb
Expand Up @@ -4,9 +4,19 @@
require 'rest_client'
require 'fileutils'

@username = ''
@password = ''
@host = 'cstaging.nd.edu'
@highline = HighLine.new

def net_id
@net_id ||= @highline.ask(@highline.color("Net ID: ", :black, :on_yellow))
end

def password
@password ||= @highline.ask(@highline.color("Password: ", :black, :on_yellow)) { |q| q.echo = "*" }
end
net_id
password
@host = 'localhost:3000'
@protocol = 'http'

config_file = File.join(File.dirname(__FILE__), "../storage/serializations/transformed-images.yml")
config = YAML.load_file(config_file)
Expand All @@ -16,7 +26,7 @@
upload_filename = File.join(File.dirname(__FILE__), '../tmp/', File.basename(filename).sub(/\Acounter-\d+---/,''))
FileUtils.cp(filename, upload_filename)
begin
RestClient.post("https://#{@username}:#{@password}@#{@host}/admin/assets", {"asset" => { "file" => File.new(File.expand_path(upload_filename)), 'tag' => 'imported' }})
RestClient.post("#{@protocol}://#{@net_id}:#{@password}@#{@host}/admin/assets", {"asset" => { "file" => File.new(File.expand_path(upload_filename)), 'tag' => 'imported' }})
rescue RestClient::Found => e
uri = URI.parse(e.response.headers[:location])
path = uri.path.sub(/\/admin\/assets\//)
Expand Down
62 changes: 51 additions & 11 deletions lib/upload_news.rb
@@ -1,17 +1,57 @@
#!/usr/bin/env ruby
require 'yaml'
require 'active_support/core_ext/hash'
require 'rest_client'
@username = ''
@password = ''
@host = 'cstaging.nd.edu'
require 'highline'

@highline = HighLine.new

def net_id
@net_id ||= @highline.ask(@highline.color("Net ID: ", :black, :on_yellow))
end

def password
@password ||= @highline.ask(@highline.color("Password: ", :black, :on_yellow)) { |q| q.echo = "*" }
end
net_id
password
@host = 'localhost:3000'
@protocol = 'http'

Dir.glob(File.join(File.dirname(__FILE__), "../storage/serializations/reviews/**/*.yml")).each do |filename|
attributes = YAML.load_file(filename)
begin
RestClient.post("https://#{@username}:#{@password}@#{@host}/admin/news", {"news" => { 'content' => attributes['content']})
rescue RestClient::Found => e
uri = URI.parse(e.response.headers[:location])
path = uri.path.sub(/\/admin\/assets\//)
attributes[:conductor_path] = File.join('/', path, File.basename(upload_filename))
attributes = YAML.load_file(filename).stringify_keys
if attributes['conductor_path'].nil?
catalog_id = attributes['catalog_id'].to_s.strip
if catalog_id =~ /\A(\d\d\d\d)\.(\d\d)\.(\d\d)\Z/
published_at = Time.local($1,$2,$3)
end
params = {}
params['publish'] = '1'
params['news'] = {}
params['news']['content'] = attributes['content'].strip
params['news']['author_id'] = 'other'
params['news']['title'] = attributes['review_title'].strip
params['news']['metum_attributes'] = { 'keys' => [], 'data' => []}
params['news']['published_at'] = published_at
params['news']['custom_author_name'] = attributes['authors']
params['news']['metum_attributes']['keys'] << 'edition'
params['news']['metum_attributes']['data'] << catalog_id
params['news']['metum_attributes']['keys'] << 'reviewers'
params['news']['metum_attributes']['data'] << attributes['reviewer']
params['news']['metum_attributes']['keys'] << 'bibliography'
params['news']['metum_attributes']['data'] << attributes['bibliography']
params['news']['metum_attributes']['keys'] << 'authors'
params['news']['metum_attributes']['data'] << attributes['authors']
begin
RestClient.post("#{@protocol}://#{@net_id}:#{@password}@#{@host}/admin/news", params)
rescue RestClient::Found => e
uri = URI.parse(e.response.headers[:location])
path = uri.path
attributes['conductor_path'] = File.join('/', path)
File.open(filename, 'w+') do |file|
file.puts YAML.dump(attributes)
end
end
end
end
break
end

0 comments on commit c6ccad8

Please sign in to comment.