sraas 0.2.6 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cec7776f444f2e21badf296554f2a25d5234019
4
- data.tar.gz: cc3e070e3e4a05d55156f37f9ea9d6f279d5a1d1
3
+ metadata.gz: e0d3d3ecb8c25690c58ba8cc3f9eb258d47f63b8
4
+ data.tar.gz: 3324dd49bb861f7cfe71d72f5813205c075dfe75
5
5
  SHA512:
6
- metadata.gz: da6f4bf03e8dfc166ce09aa4e1ef6baabbb1c421ab753033213bb5ca099a249835920f81e698e67aa58716a7dc931347351cd40a1220324755f7d5c15d10397b
7
- data.tar.gz: 6ea49851946eb4623d543ffc727ee9d8ff5c0ed9d2bf33ad59e523c74e5ebe1c1ad402bced2203679d004561fd6ebe5c88e54d2aec0d7ba0f5fa9898ac2aa376
6
+ metadata.gz: b41000df97f948d433cb9f3cc7947d716d92eb0d6a6e33307e145bcfc25d89b93522a8445c7dde6b1d9931f1cea9185d295d67f81a81027dd6e92b0f58838837
7
+ data.tar.gz: 743b18a1bf3557017576be183cb32f36a24b379c6972dcb4567f2a0c1fe7cf39f4c310a9495164be69f5868e1d85d2e421e3535a5a9321c2d7e7136840c7d65f
data/README.md CHANGED
@@ -23,8 +23,13 @@ $ rake sraas:install
23
23
 
24
24
  Add your api key in `config/initializers/sraas.rb`
25
25
  ```ruby
26
- # API key
27
- config.api_key = <API KEY>
26
+ Sraas.configure do |config|
27
+ # Sraas provider URL
28
+ config.url = ENV["SRAAS_URL"] || "https://www.sraas.io"
29
+
30
+ # API key
31
+ config.api_key = Rails.application.secrets.sraas_api_key
32
+ end
28
33
  ```
29
34
 
30
35
  Run migrations
data/bin/sraas CHANGED
@@ -27,7 +27,12 @@ module Sraas
27
27
 
28
28
  class SraasSectet < Hanami::CLI::Command
29
29
  def use_credentials
30
- Sraas.configuration.api_key = File.read(CRED)
30
+ if File.exist?(CRED)
31
+ Sraas.configuration.api_key = File.read(CRED)
32
+ else
33
+ Commands::Login.new(command_name: 'login').call
34
+ use_credentials
35
+ end
31
36
  end
32
37
  end
33
38
 
@@ -53,7 +58,7 @@ module Sraas
53
58
 
54
59
  example [SecureRandom.uuid]
55
60
 
56
- def call(uuid: , **)
61
+ def call(uuid:, **)
57
62
  response = fetch_info(uuid)
58
63
  text = "Info for Consumer #{uuid}. "
59
64
  text += "Learns available: #{response['available_to_learn']} "
@@ -115,12 +120,12 @@ module Sraas
115
120
  def map_card(card)
116
121
  Card.new(
117
122
  quiz_id: response['quiz_id'],
118
- id: card['card_id'],
119
- front: card['front'],
123
+ id: card['card_id'],
124
+ front: card['front'],
120
125
  meaning: card['learn_tabs'][0]['body'],
121
- prompt: card['prompt'],
126
+ prompt: card['prompt'],
122
127
  answers: card['answer'],
123
- kind: card['card_type']
128
+ kind: card['card_type'],
124
129
  )
125
130
  end
126
131
  end
@@ -143,7 +148,84 @@ module Sraas
143
148
  end
144
149
  end
145
150
  end
151
+ end
152
+ end
153
+
154
+ module Learn
155
+ class Consumer < Info::Consumer
156
+ desc 'Allows learning new things'
157
+
158
+ argument :uuid, desc: 'Consumer UUID'
159
+
160
+ example [SecureRandom.uuid]
161
+
162
+ def call(uuid:, **)
163
+ use_credentials
164
+ response = JSON.parse(consumer_learn(uuid))
165
+ response['cards'].each do |card|
166
+ card = OpenStruct.new({
167
+ id: card['uuid'],
168
+ kind: card['kind'],
169
+ back: card['back'],
170
+ tabs: card['learn_tabs'],
171
+ })
172
+ print card_header(card.kind).red
173
+ card_body(Nokogiri::HTML(card.back.gsub('<br>', "\n")).text)
174
+ print card_footer.red
175
+ puts card_tabs(card.tabs)
176
+ print close.red
177
+ STDIN.gets
178
+ print "\r" + ("\e[A\e[K"*(13+format(card.tabs).size))
179
+ end
180
+ end
146
181
 
182
+ def card_header(kind)
183
+ <<~HEREDOC
184
+ ________________________________________________________________
185
+ | |
186
+ | #{kind} |
187
+ |________________________________________________________________|
188
+ | |
189
+ | |
190
+ HEREDOC
191
+ end
192
+
193
+ def card_footer
194
+ <<~HEREDOC
195
+ | |
196
+ |________________________________________________________________|
197
+ | |
198
+ HEREDOC
199
+ end
200
+
201
+ def close
202
+ <<~HEREDOC
203
+ |________________________________________________________________|
204
+ HEREDOC
205
+ end
206
+
207
+ def card_body(text)
208
+ radical, meaing = text.split("\n")
209
+ printf("%s%s%s\n", '|'.red, radical.center(63).green,'|'.red)
210
+ printf("%s%s%s\n", '|'.red, meaing.center(64).green,'|'.red)
211
+ end
212
+
213
+ def card_tabs(tabs)
214
+ format(tabs)
215
+ .map{|line| "%s%s%s" % ['|'.red, line.center(64).blue,'|'.red]}
216
+ .join("\n")
217
+ end
218
+
219
+ def format(tabs)
220
+ tabs
221
+ .map{|tab| "#{tab['title']}: #{Nokogiri::HTML(tab['body']).text}"}
222
+ .join("\n")
223
+ .scan(/\S.{1,60}(?!\S)/)
224
+ end
225
+
226
+ def consumer_learn(uuid)
227
+ Sraas.api_resource(:get, "#{consumer_url(uuid)}/learn")
228
+ end
147
229
  end
148
230
  end
149
231
 
@@ -164,10 +246,15 @@ module Sraas
164
246
  prefix.register 'consumer', Info::Consumer, aliases: ['c', '-c']
165
247
  end
166
248
 
249
+ register 'learn' do |prefix|
250
+ prefix.register 'consumer', Learn::Consumer, aliases: ['c', '-c']
251
+ end
252
+
167
253
  register 'review' do |prefix|
168
254
  prefix.register 'consumer', Review::Consumer, aliases: ['c', '-c']
169
255
  end
170
256
  end
171
257
  end
172
258
  end
259
+
173
260
  Hanami::CLI.new(Sraas::CLI::Commands).call
@@ -2,6 +2,7 @@ require 'rest_client'
2
2
  require 'sraas/consumer' if defined?(ActiveRecord::Base)
3
3
  require 'sraas/railtie' if defined?(Rails)
4
4
  require 'sraas/has_sraas' if defined?(Rails)
5
+ require 'sraas/template_card'
5
6
  require 'sraas/configuration'
6
7
 
7
8
  module Sraas
@@ -37,6 +38,10 @@ module Sraas
37
38
  @cache = consumers.inject({}) { |a, v| k = v['id']; a[k] = v; a }
38
39
  end
39
40
 
41
+ def template_decks
42
+ JSON.parse(api_resource(:get, template_decks_endpoint))
43
+ end
44
+
40
45
  def consumers
41
46
  api_resource(:get, consumers_endpoint)
42
47
  end
@@ -53,6 +58,10 @@ module Sraas
53
58
  "#{configuration.url}/cards"
54
59
  end
55
60
 
61
+ def card_search_endpoint(consumer_id)
62
+ "#{configuration.url}/consumers/#{consumer_id}/cards/search"
63
+ end
64
+
56
65
  def template_decks_endpoint
57
66
  "#{configuration.url}/template_decks"
58
67
  end
@@ -1,10 +1,11 @@
1
1
  module Sraas
2
2
  class Configuration
3
- attr_accessor :url, :api_key
3
+ attr_accessor :url, :api_key, :default_deck
4
4
 
5
5
  def initialize
6
- @url = 'https://www.sraas.io'
7
- @api_key = nil
6
+ @url = 'https://www.sraas.io'
7
+ @api_key = nil
8
+ @default_deck = nil
8
9
  end
9
10
  end
10
11
  end
@@ -44,7 +44,7 @@ module Sraas
44
44
 
45
45
  # Nicer accessors for binary
46
46
  def learned_anything?
47
- info['first_learn_at']
47
+ info['learned_anything']
48
48
  end
49
49
 
50
50
  def reviewed_anything?
@@ -73,8 +73,18 @@ module Sraas
73
73
  OpenStruct.new(progress: lesson['progress'], cards: lesson['cards'])
74
74
  end
75
75
 
76
+ # Bulk accessor
77
+ def cards(card_uuids)
78
+ parse_json { get("#{Sraas.cards_endpoint}/bulk?#{{ids: card_uuids}.to_query}") }
79
+ end
80
+
81
+ # One card accessor
76
82
  def card(card_uuid)
77
- parse_json { get("#{Sraas.cards_endpoint}/#{card_uuid}") }
83
+ parse_json { get("#{Sraas.cards_endpoint}/cards/#{card_uuid}") }
84
+ end
85
+
86
+ def search_card(consumer_id, title, kind)
87
+ parse_json { get("#{Sraas.card_search_endpoint(consumer_id)}?title=#{title}&kind=#{kind}") }
78
88
  end
79
89
 
80
90
  def report(correct_ids, incorrect_ids)
@@ -83,7 +93,7 @@ module Sraas
83
93
  end
84
94
 
85
95
  def template_decks
86
- parse_json { get(Sraas.template_decks_endpoint) }
96
+ Sraas.template_decks
87
97
  end
88
98
 
89
99
  def unlock_lesson(lesson = 1)
@@ -107,6 +117,11 @@ module Sraas
107
117
  "#{Sraas.consumers_endpoint}/#{consumer_uuid}"
108
118
  end
109
119
 
120
+ def update_card(card_uuid, params = {})
121
+ data = patch("#{Sraas.cards_endpoint}/#{card_uuid}", params)
122
+ data.code == 200
123
+ end
124
+
110
125
  private
111
126
 
112
127
  def consumer
@@ -122,6 +137,10 @@ module Sraas
122
137
  Sraas.api_resource(:get, url)
123
138
  end
124
139
 
140
+ def patch(url, payload)
141
+ Sraas.api_resource(:patch, url, payload)
142
+ end
143
+
125
144
  def parse_json(&block)
126
145
  JSON.parse(block.call) if block_given?
127
146
  end
@@ -2,11 +2,11 @@ module Sraas
2
2
  module HasSraas
3
3
  module ClassMethods
4
4
  def has_sraas(deck: nil)
5
- has_one :sraas, as: :sraas_consumerable, class_name: Sraas::Consumer
5
+ has_one :sraas, as: :sraas_consumerable, class_name: 'Sraas::Consumer'
6
6
 
7
7
  after_create :configure_sraas_consumer!
8
8
 
9
- self.sraas_default_deck = deck
9
+ self.sraas_default_deck = deck || Sraas.configuration.default_deck
10
10
  end
11
11
  end
12
12
 
@@ -0,0 +1,29 @@
1
+ module Sraas
2
+ class TemplateCard
3
+ class << self
4
+ def index(offset: 1, limit: 1)
5
+ paging = { limit: limit, offset: offset }.to_query
6
+ JSON.parse(Sraas.api_resource(:get, "#{template_cards_url}?#{paging}"))
7
+ end
8
+
9
+ def get(id)
10
+ JSON.parse(Sraas.api_resource(:get, "#{template_cards_url}/#{id}"))
11
+ end
12
+
13
+ private
14
+
15
+ def template_cards_url
16
+ "#{Sraas.template_decks_endpoint}/#{deck_uuid}/template_cards"
17
+ end
18
+
19
+ def deck_uuid
20
+ @deck_uuid ||= detect_deck_by_name.dig('id')
21
+ end
22
+
23
+ def detect_deck_by_name
24
+ deck_name = Sraas.configuration.default_deck
25
+ Sraas.template_decks.detect { |deck| deck['name'] == deck_name } || {}
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Sraas
2
- VERSION = '0.2.6'
2
+ VERSION = '0.2.10'
3
3
  end
@@ -1,7 +1,10 @@
1
1
  Sraas.configure do |config|
2
2
  # Sraas provider URL
3
- config.url = 'https://www.sraas.io'
3
+ config.url = 'https://www.sraas.io'
4
4
 
5
5
  # API key
6
- config.api_key = nil # 'your-api-key'
6
+ config.api_key = nil # 'your-api-key'
7
+
8
+ # Default deck
9
+ config.default_deck = nil
7
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sraas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Siegel
@@ -166,6 +166,7 @@ files:
166
166
  - lib/sraas/consumer.rb
167
167
  - lib/sraas/has_sraas.rb
168
168
  - lib/sraas/railtie.rb
169
+ - lib/sraas/template_card.rb
169
170
  - lib/sraas/version.rb
170
171
  - lib/tasks/sraas_tasks.rake
171
172
  - lib/tasks/templates/initializers/sraas.rb
@@ -179,9 +180,9 @@ require_paths:
179
180
  - lib
180
181
  required_ruby_version: !ruby/object:Gem::Requirement
181
182
  requirements:
182
- - - ">="
183
+ - - "~>"
183
184
  - !ruby/object:Gem::Version
184
- version: '0'
185
+ version: '2.3'
185
186
  required_rubygems_version: !ruby/object:Gem::Requirement
186
187
  requirements:
187
188
  - - ">="