Class: BlogImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/blog_importer.rb

Constant Summary collapse

DEFAULT_API_ADDRESS =
"http://launchpadlab.com/api/posts.json"

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlObject (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_jsonObject

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_dbObject



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")
    author_first_name = post["author"]["first_name"]
    author_last_name = post["author"]["last_name"]
    author_full_name = "#{author_first_name} #{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 = author_name_to_member[author_full_name]
    author = Fuel::Author.find_by_email(member[:email])
    post["author_id"] = author.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_memberObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/blog_importer.rb', line 45

def author_name_to_member
  @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_authorsObject



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 create_authors
  [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
    author = Fuel::Author.find_by_email(member["email"])
    next if author.present?
    image_path = member[:image_path].dup
    member.delete(:image_path)
    member.delete(:image_alt)
    new_author = Fuel::Author.new(member)

    avatar_location = "#{Rails.root}/app/assets/images/#{image_path}"
    puts avatar_location
    file = File.open(avatar_location)
    new_author.avatar = file
    file.close

    puts "creating new author..."
    new_author.save!
    puts "Author #{Fuel::Author.count} created."
  end
end

#pullObject



15
16
17
18
19
20
21
22
# File 'lib/blog_importer.rb', line 15

def pull
  create_authors
  open(api_url) do |f|
    json_string = f.read
    @parsed_json = JSON.parse(json_string)
  end
  add_posts_to_db
end