Class: BlogImporter
- Inherits:
-
Object
- Object
- BlogImporter
- Defined in:
- lib/blog_importer.rb
Constant Summary collapse
- DEFAULT_API_ADDRESS =
"http://launchpadlab.com/api/posts.json"
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#parsed_json ⇒ Object
Returns the value of attribute parsed_json.
Instance Method Summary collapse
- #add_posts_to_db ⇒ Object
- #author_name_to_member ⇒ Object
- #create_authors ⇒ Object
-
#initialize(args = {}) ⇒ BlogImporter
constructor
A new instance of BlogImporter.
- #pull ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ BlogImporter
Returns a new instance of BlogImporter.
11 12 13 |
# File 'lib/blog_importer.rb', line 11 def initialize(args = {}) @api_url = args.fetch(:api_url, DEFAULT_API_ADDRESS) end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
8 9 10 |
# File 'lib/blog_importer.rb', line 8 def api_url @api_url end |
#parsed_json ⇒ Object
Returns the value of attribute parsed_json.
9 10 11 |
# File 'lib/blog_importer.rb', line 9 def parsed_json @parsed_json end |
Instance Method Details
#add_posts_to_db ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/blog_importer.rb', line 24 def add_posts_to_db parsed_json.each do |post| puts "Importing #{post['title']}" post.delete("id") = post["author"]["first_name"] = post["author"]["last_name"] = "#{} #{}" image_url = post["prioritized_featured_image_url"].present? ? post["prioritized_featured_image_url"].dup : nil post.delete("prioritized_featured_image_url") member = [] = Fuel::Author.find_by_email(member[:email]) post["author_id"] = .id post.delete("author") new_post = Fuel::Post.new(post) new_post.featured_image = URI.parse(image_url) if image_url.present? new_post.published_at = post["created_at"] if new_post.published puts new_post.published_at new_post.save end end |
#author_name_to_member ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/blog_importer.rb', line 45 def @key_to_member ||= { "Brendan Hennessy" => Fuel::Author.find_by_email("[email protected]"), "Tom Cullen" => Fuel::Author.find_by_email("[email protected]"), "Ryan Francis" => Fuel::Author.find_by_email("[email protected]"), "Scott Weisman" => Fuel::Author.find_by_email("[email protected]"), "Kurt Cunningham" => Fuel::Author.find_by_email("[email protected]"), "Katie Astrauskas" => Fuel::Author.find_by_email("[email protected]"), "Monique Marchwiany" => Fuel::Author.find_by_email("[email protected]"), "Jack Miller" => Fuel::Author.find_by_email("[email protected]"), "Dave Corwin" => Fuel::Author.find_by_email("[email protected]") } end |
#create_authors ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/blog_importer.rb', line 60 def [ExampleAuthor::BRENDAN, ExampleAuthor::TOM, ExampleAuthor::RYAN, ExampleAuthor::SCOTT, ExampleAuthor::KURT, ExampleAuthor::DAVE, ExampleAuthor::KATIE, ExampleAuthor::MONIQUE, ExampleAuthor::JACK].each do |member_const| member = member_const.dup = Fuel::Author.find_by_email(member["email"]) next if .present? image_path = member[:image_path].dup member.delete(:image_path) member.delete(:image_alt) = Fuel::Author.new(member) avatar_location = "#{Rails.root}/app/assets/images/#{image_path}" puts avatar_location file = File.open(avatar_location) .avatar = file file.close puts "creating new author..." .save! puts "Author #{Fuel::Author.count} created." end end |
#pull ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/blog_importer.rb', line 15 def pull open(api_url) do |f| json_string = f.read @parsed_json = JSON.parse(json_string) end add_posts_to_db end |