Module: Goodreads::Shelves

Included in:
Client
Defined in:
lib/goodreads/client/shelves.rb

Instance Method Summary collapse

Instance Method Details

#add_to_shelf(book_id, shelf_name, options = {}) ⇒ Object

Add book to a user’s shelf

Returns the user’s review for the book, which includes all its current shelves



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/goodreads/client/shelves.rb', line 58

def add_to_shelf(book_id, shelf_name, options = {})
  options = options.merge(book_id: book_id, name: shelf_name, v: 2)
  data = oauth_request_method(:post, "/shelf/add_to_shelf.xml", options)

  # when a book is on a single shelf it is a single hash
  shelves = data["my_review"]["shelves"]["shelf"]
  shelves = [shelves] unless shelves.instance_of?(Array)
  shelves = shelves.map do |s|
    Hashie::Mash.new({
      id:             s["id"].to_i,
      name:           s["name"],
      exclusive:      s["exclusive"] == "true",
      sortable:       s["sortable"] == "true",
    })
  end

  Hashie::Mash.new(
    id: data["my_review"]["id"].to_i,
    book_id: data["my_review"]["book_id"].to_i,

    rating: data["my_review"]["rating"].to_i,
    body: data["my_review"]["body"],
    body_raw: data["my_review"]["body_raw"],
    spoiler: data["my_review"]["spoiler_flag"] == "true",

    shelves: shelves,

    read_at: data["my_review"]["read_at"],
    started_at: data["my_review"]["started_at"],
    date_added: data["my_review"]["date_added"],
    updated_at: data["my_review"]["updated_at"],
  )
end

#shelf(user_id, shelf_name, options = {}) ⇒ Object

Get books from a user’s shelf



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/goodreads/client/shelves.rb', line 35

def shelf(user_id, shelf_name, options = {})
  options = options.merge(shelf: shelf_name, v: 2)
  data = request("/review/list/#{user_id}.xml", options)
  reviews = data["reviews"]["review"]

  books = []
  unless reviews.nil?
    # one-book results come back as a single hash
    reviews = [reviews] unless reviews.instance_of?(Array)
    books = reviews.map { |e| Hashie::Mash.new(e) }
  end

  Hashie::Mash.new(
    start: data["reviews"]["start"].to_i,
    end: data["reviews"]["end"].to_i,
    total: data["reviews"]["total"].to_i,
    books: books
  )
end

#shelves(user_id, options = {}) ⇒ Object

Lists shelves for a user



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/goodreads/client/shelves.rb', line 4

def shelves(user_id, options = {})
  options = options.merge(user_id: user_id, v: 2)
  data = request("/shelf/list.xml", options)
  shelves = data["shelves"]

  shelves = data["shelves"]["user_shelf"].map do |s|
    Hashie::Mash.new({
      id:             s["id"],
      name:           s["name"],
      book_count:     s["book_count"],
      exclusive:      s["exclusive_flag"],
      description:    s["description"],
      sort:           s["sort"],
      order:          s["order"],
      per_page:       s["per_page"],
      display_fields: s["display_fields"],
      featured:       s["featured"],
      recommend_for:  s["recommend_for"],
      sticky:         s["sticky"],
    })
  end

  Hashie::Mash.new(
    start:   data["shelves"]["start"].to_i,
    end:     data["shelves"]["end"].to_i,
    total:   data["shelves"]["total"].to_i,
    shelves: shelves
  )
end