Class: CombineHashtags::Profile

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

Overview

Profile class to instantiate a user's profile and load their IG content from JSON stored locally

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Profile

loads json files to create profile and posts



14
15
16
17
# File 'lib/combine_hashtags/profile.rb', line 14

def initialize(file_path)
  @posts = []
  update(file_path) 
end

Instance Attribute Details

#postsObject

Returns the value of attribute posts.



11
12
13
# File 'lib/combine_hashtags/profile.rb', line 11

def posts
  @posts
end

Instance Method Details

#destroyObject

remove existing profile data



47
48
49
50
51
# File 'lib/combine_hashtags/profile.rb', line 47

def destroy
  puts "removing current files"
  string = "#{ENV["STORAGE_PATH"]}*.json" 
  `rm #{string}`
end

#load_fileObject



19
20
21
22
23
24
25
26
# File 'lib/combine_hashtags/profile.rb', line 19

def load_file
  if File.file?(@file_path)
    file = File.read(@file_path)
    @json_data = JSON.parse(file)
  else
    puts "file #{@file_path} not found"
  end
end

#load_postsObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/combine_hashtags/profile.rb', line 28

def load_posts
  @json_data["data"].map do |post|
    post_url = post["permalink"]
    img_url = post["media_url"]
    caption = post["caption"] || ""
    timestamp = post["timestamp"]

    @posts << CombineHashtags::Post.new(post_url, img_url, caption, timestamp)
  end
end

#update(file_path) ⇒ Object

update profile with remaining json files



40
41
42
43
44
# File 'lib/combine_hashtags/profile.rb', line 40

def update(file_path)
  @file_path = file_path
  load_file
  load_posts
end