Class: BookmeterScraper::Bookmeter

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

Constant Summary collapse

DEFAULT_CONFIG_PATH =
'./config.yml'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent = nil) ⇒ Bookmeter

Returns a new instance of Bookmeter.


26
27
28
29
30
31
# File 'lib/bookmeter_scraper/bookmeter.rb', line 26

def initialize(agent = nil)
  @agent          = agent.nil? ? Agent.new : agent
  @scraper        = Scraper.new(@agent)
  @logged_in      = false
  @log_in_user_id = nil
end

Instance Attribute Details

#log_in_user_idObject (readonly)

Returns the value of attribute log_in_user_id


8
9
10
# File 'lib/bookmeter_scraper/bookmeter.rb', line 8

def 
  @log_in_user_id
end

Class Method Details

.log_in(mail = nil, password = nil) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bookmeter_scraper/bookmeter.rb', line 12

def (mail = nil, password = nil)
  Bookmeter.new.tap do |bookmeter|
    if block_given?
      config = Configuration.new
      yield config
      bookmeter.(config.mail, config.password)
    else
      bookmeter.(mail, password)
    end
  end
end

Instance Method Details

#followers(user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

94
95
96
97
# File 'lib/bookmeter_scraper/bookmeter.rb', line 94

def followers(user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  @scraper.fetch_followers(user_id)
end

#followings(user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

89
90
91
92
# File 'lib/bookmeter_scraper/bookmeter.rb', line 89

def followings(user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  @scraper.fetch_followings(user_id)
end

#log_in(mail = nil, password = nil) ⇒ Object

Raises:


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bookmeter_scraper/bookmeter.rb', line 33

def (mail = nil, password = nil)
  raise BookmeterError if @agent.nil?

  configuration = if block_given?
                    Configuration.new.tap { |config| yield config }
                  elsif mail.nil? && password.nil?
                    Configuration.new(DEFAULT_CONFIG_PATH)
                  else
                    Configuration.new.tap do |config|
                      config.mail     = mail
                      config.password = password
                    end
                  end

  @log_in_user_id = @agent.(configuration)
  @logged_in      = !@log_in_user_id.nil?
end

#logged_in?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/bookmeter_scraper/bookmeter.rb', line 51

def logged_in?
  @logged_in
end

#profile(user_id) ⇒ Object

Raises:

  • (ArgumentError)

55
56
57
58
# File 'lib/bookmeter_scraper/bookmeter.rb', line 55

def profile(user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  @scraper.fetch_profile(user_id)
end

#read_books(user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

60
61
62
63
# File 'lib/bookmeter_scraper/bookmeter.rb', line 60

def read_books(user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  fetch_books(user_id, :read_books_uri)
end

#read_books_in(year, month, user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

65
66
67
68
69
70
71
72
# File 'lib/bookmeter_scraper/bookmeter.rb', line 65

def read_books_in(year, month, user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX

  date = Time.local(year, month)
  books = @scraper.fetch_read_books(user_id, date)
  books.each { |b| yield b } if block_given?
  books.to_a
end

#reading_books(user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

74
75
76
77
# File 'lib/bookmeter_scraper/bookmeter.rb', line 74

def reading_books(user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  fetch_books(user_id, :reading_books_uri)
end

#tsundoku(user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

79
80
81
82
# File 'lib/bookmeter_scraper/bookmeter.rb', line 79

def tsundoku(user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  fetch_books(user_id, :tsundoku_uri)
end

#wish_list(user_id = @log_in_user_id) ⇒ Object

Raises:

  • (ArgumentError)

84
85
86
87
# File 'lib/bookmeter_scraper/bookmeter.rb', line 84

def wish_list(user_id = @log_in_user_id)
  raise ArgumentError unless user_id =~ USER_ID_REGEX
  fetch_books(user_id, :wish_list_uri)
end