Class: Sraas::Consumer

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sraas/consumer.rb

Instance Method Summary collapse

Instance Method Details

#add_template_deck(name = nil, fingerprint: nil) ⇒ Object

This needs to return boolean since this is called in has_sraas#add_default_deck which is expecting true or false from this method.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/sraas/consumer.rb', line 203

def add_template_deck(name=nil, fingerprint: nil)
  # At least one of either name or fingerprint has to be provided.
  fail(Sraas::DeckNotSpecified) if name.nil? && fingerprint.nil?

  # Try all search patterns with name and fingerprint
  # in case one of them is incorrect, but the other correct
  matching_decks = Sraas.template_decks({name_start: name, fingerprint_start: fingerprint})
  matching_decks = Sraas.template_decks({fingerprint_start: fingerprint}) unless matching_decks.any?
  matching_decks = Sraas.template_decks({name_start: name}) unless matching_decks.any?
  return false unless matching_decks.any?

  deck_uuid = matching_decks.last['id']
  url = "#{Sraas.template_decks_endpoint}/#{deck_uuid}/add_to_consumer"
  payload = { consumer_id: [self.consumer_uuid] }
  # TODO: Handle a request for a deck that doesn't exist elegantly.
  # Right now this dies on the patch call:
  data = Sraas.api_resource(:patch, url, payload)
  data.code == 200
end

#available_to_learnObject



17
18
19
# File 'lib/sraas/consumer.rb', line 17

def available_to_learn
  info['available_to_learn']
end

#available_to_reviewObject



9
10
11
# File 'lib/sraas/consumer.rb', line 9

def available_to_review
  info['available_to_review']
end

#bonus_limitObject



86
87
88
# File 'lib/sraas/consumer.rb', line 86

def bonus_limit
  info['bonus_limit']
end

#card(card_uuid) ⇒ Object

One card accessor. This return card “full” json TODO: make this return OpenStruct.



132
133
134
# File 'lib/sraas/consumer.rb', line 132

def card(card_uuid)
  parse_json { get("#{Sraas.cards_endpoint}/#{card_uuid}") }
end

#card_children(card_uuid) ⇒ Object

Children cards accessor. This returns card children “index” json



137
138
139
# File 'lib/sraas/consumer.rb', line 137

def card_children(card_uuid)
  parse_json { get("#{Sraas.cards_endpoint}/#{card_uuid}/children") }
end

#card_parents(card_uuid) ⇒ Object

Parents cards accessor. This returns card parents “index” json



142
143
144
# File 'lib/sraas/consumer.rb', line 142

def card_parents(card_uuid)
  parse_json { get("#{Sraas.cards_endpoint}/#{card_uuid}/parents") }
end

#cards(card_uuids) ⇒ Object

Bulk accessor



108
109
110
# File 'lib/sraas/consumer.rb', line 108

def cards(card_uuids)
  parse_json { get("#{Sraas.cards_endpoint}/bulk?#{{ids: card_uuids}.to_query}") }
end

#cards_by_starsObject



43
44
45
# File 'lib/sraas/consumer.rb', line 43

def cards_by_stars
  info['cards_by_stars']
end

#cards_remainingObject



31
32
33
# File 'lib/sraas/consumer.rb', line 31

def cards_remaining
  info['lesson_remaining_cards']
end

#cards_with_accuracy(filter_params: {}, offset: 0, limit: 100) ⇒ Object

Card#index_with_accuracy accessor. filter_params can be by_kind and by_lesson e.g. current_user.sraas.cards_with_accuracy(filter_params: “Radical”, by_lesson: 1)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sraas/consumer.rb', line 115

def cards_with_accuracy(filter_params: {}, offset: 0, limit: 100)
  cards = []
  remainder = limit
  while remainder > 0 do
    paging = { limit: remainder, offset: offset} #even if limit(remainder) > 100, it will be 100 on sraas side.
    params = filter_params.merge(paging)
    result = JSON.parse(Sraas.api_resource(:get, "#{consumer_url}/cards/index_with_accuracy", params))
    break if result.empty?
    cards << result
    remainder -= 100
    offset += 100
  end
  cards.flatten
end

#consumer_urlObject

This is a public method to provide to views parsing JSON in JavaScript



229
230
231
# File 'lib/sraas/consumer.rb', line 229

def consumer_url
  "#{Sraas.consumers_endpoint}/#{self.consumer_uuid}"
end

#current_lessonObject



39
40
41
# File 'lib/sraas/consumer.rb', line 39

def current_lesson
  info['current_lesson']
end

#daily_limitObject



74
75
76
# File 'lib/sraas/consumer.rb', line 74

def daily_limit
  info['daily_limit']
end

#first_learn_atObject



51
52
53
# File 'lib/sraas/consumer.rb', line 51

def first_learn_at
  info['first_learn_at']
end

#first_review_atObject



55
56
57
# File 'lib/sraas/consumer.rb', line 55

def first_review_at
  info['first_review_at']
end

#infoObject



90
91
92
93
94
95
# File 'lib/sraas/consumer.rb', line 90

def info
  # Only caching for lifetime of a single request since this object may get
  # called many times in a view...
  return @_info if defined?(@_info)
  @_info = consumer
end

#learn_countObject

Renamed them to make method name same as sraas. ane they will be removed in the future



22
23
24
25
# File 'lib/sraas/consumer.rb', line 22

def learn_count
  puts "DEPRECATION WARNING: THIS WILL BE REMOVED IN THE FUTURE. PLEASE USE available_to_learn"
  available_to_learn
end

#learned_anything?Boolean

Nicer accessors for binary

Returns:

  • (Boolean)


60
61
62
# File 'lib/sraas/consumer.rb', line 60

def learned_anything?
  !info['first_learn_at'].nil?
end

#learned_everything?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sraas/consumer.rb', line 47

def learned_everything?
  info['learned_everything']
end

#lesson(lesson_id) ⇒ Object

Get all cards “index” json for the lesson and progress



102
103
104
105
# File 'lib/sraas/consumer.rb', line 102

def lesson(lesson_id)
  lesson = parse_json { get("#{consumer_url}/lessons/#{lesson_id}") }
  OpenStruct.new(progress: lesson['progress'], cards: lesson['cards'])
end

#lesson_progressObject



35
36
37
# File 'lib/sraas/consumer.rb', line 35

def lesson_progress
  info['lesson_progress']
end

#lessonsObject



97
98
99
# File 'lib/sraas/consumer.rb', line 97

def lessons
  parse_json { get("#{consumer_url}/lessons") }
end

#next_review_atObject



68
69
70
71
72
# File 'lib/sraas/consumer.rb', line 68

def next_review_at
  Time.parse(info['next_review_at'])
rescue StandardError
  'N/A'
end

#progressObject

return consumer’s progress including review results, review_accuracy



224
225
226
# File 'lib/sraas/consumer.rb', line 224

def progress
  parse_json { get("#{Sraas.consumer_stats_endpoint}/#{self.consumer_uuid}") }
end

#promo_limitObject



78
79
80
# File 'lib/sraas/consumer.rb', line 78

def promo_limit
  info['promo_limit']
end

#promo_limit_ends_atObject



82
83
84
# File 'lib/sraas/consumer.rb', line 82

def promo_limit_ends_at
  info['promo_limit_ends_at']
end

#report(correct_ids, incorrect_ids) ⇒ Object



154
155
156
157
# File 'lib/sraas/consumer.rb', line 154

def report(correct_ids, incorrect_ids)
  payload = {correct: correct_ids, incorrect: incorrect_ids}
  parse_json { Sraas.api_resource(:post, "#{consumer_url}/report", payload) }
end

#review_countObject



26
27
28
29
# File 'lib/sraas/consumer.rb', line 26

def review_count
  puts "DEPRECATION WARNING: THIS WILL BE REMOVED IN THE FUTURE. PLEASE USE available_to_review"
  available_to_review
end

#reviewed_anything?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sraas/consumer.rb', line 64

def reviewed_anything?
  !info['first_review_at'].nil?
end

#search_card(title, kind) ⇒ Object



146
147
148
# File 'lib/sraas/consumer.rb', line 146

def search_card(title, kind)
  Sraas.search_card(self.consumer_uuid, title, kind)
end

#search_template_card(title, kind) ⇒ Object

TODO: accept not only title and kind. sraas needs to use ransack for it. This search template_card with title and kind.



186
187
188
189
# File 'lib/sraas/consumer.rb', line 186

def search_template_card(title, kind)
  template_deck_id = template_deck['id']
  Sraas.search_template_card(template_deck_id, title, kind)
end

#template_cards(filter_params: {}, offset: 0, limit: 100) ⇒ Object

template_cards accessor spcified by consumer Assume consumers/:consumer_id/decks has only one deck current_user.sraas.template_cards(filter_params: 1, by_kind: ‘Radical’)



171
172
173
174
# File 'lib/sraas/consumer.rb', line 171

def template_cards(filter_params: {}, offset: 0, limit: 100)
  template_deck_id = template_deck['id']
  Sraas.template_cards(template_deck_id, filter_params: filter_params, offset: offset, limit: limit)
end

#template_deckObject

template_deck accessor. specified by consumer Assume consumers/:consumer_id/decks has only one deck even though sraas can have more than one decks…



162
163
164
165
166
# File 'lib/sraas/consumer.rb', line 162

def template_deck
  decks = parse_json { get("#{consumer_url}/decks") }
  template_deck_id =  decks.first['template_deck_id']
  Sraas.template_deck(template_deck_id)
end

#template_decks(params = {}) ⇒ Object

return all template_decks without name and fingerprint



192
193
194
# File 'lib/sraas/consumer.rb', line 192

def template_decks(params={})
  Sraas.template_decks(params)
end

#template_lesson(lesson) ⇒ Object

This returns template_cards selected with lesson number. and progress: 0



178
179
180
181
182
# File 'lib/sraas/consumer.rb', line 178

def template_lesson(lesson)
  template_deck_id = template_deck['id']
  template_lesson = parse_json { get("#{Sraas.template_decks_endpoint}/#{template_deck_id}/template_lessons/#{lesson}") }
  OpenStruct.new(progress: template_lesson['progress'], template_cards: template_lesson['template_cards'])
end

#unavailable_to_reviewObject



13
14
15
# File 'lib/sraas/consumer.rb', line 13

def unavailable_to_review
  info['unavailable_to_review']
end

#unlock_lesson(lesson = 1) ⇒ Object



196
197
198
199
# File 'lib/sraas/consumer.rb', line 196

def unlock_lesson(lesson = 1)
  data = get("#{consumer_url}/unlock_lesson?lesson=#{lesson}")
  data.code == 200
end

#update_card(card_uuid, params = {}) ⇒ Object



150
151
152
# File 'lib/sraas/consumer.rb', line 150

def update_card(card_uuid, params = {})
  Sraas.update_card(card_uuid, params)
end