sraas 0.2.15 → 0.3.0

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: 8c84427806dfb1071fd9c71d0ca8cb5d45b65a95
4
- data.tar.gz: 7cbe469cc7a2ca08663eab5b6eb8c4ec9a599fbc
3
+ metadata.gz: 5535a413f64093a0d029eb47a9cbcb4bc179f984
4
+ data.tar.gz: 753c6a3829882d916a06860be38ab9dc998ffcc2
5
5
  SHA512:
6
- metadata.gz: dccff92b0b9a51c4ecfad6bed55bb35c87c790cef5971ee06b96981d6c3ce92f7b1f5e9ace06843f01614ad4a0c91f03aeca58c2872f38f544fc9144ebe1f1e7
7
- data.tar.gz: d89e0af70d6ef41055535686027d58f50185a9a6a639b527ce50b977356e8fe83668301dcf4c91c5878326810eb5b4ee10719e85820eb3a3b39208c0936fba79
6
+ metadata.gz: a786a111a9f168134f01d368c645548842d5d79f402c99109233d9e005acef24d55b2c17b0d462b7c2a9e7938b5f877bf9f0f501db3cb4f53bc2a2aef67437cc
7
+ data.tar.gz: 7812c4976ee15f0002b8dec076e41e659174eabf43458b7206060a577285cc4b89a78cdca527721ef1f06ab5ebb426361ddcf41f07bd83a61803a78b3c623504
data/lib/sraas.rb CHANGED
@@ -8,6 +8,7 @@ require 'sraas/configuration'
8
8
  module Sraas
9
9
  NoAPIKey = Class.new(StandardError)
10
10
  DeckNotFound = Class.new(StandardError)
11
+ DeckNotSpecified = Class.new(StandardError)
11
12
 
12
13
  class << self
13
14
  def configuration
@@ -38,8 +39,11 @@ module Sraas
38
39
  @cache = consumers.inject({}) { |a, v| k = v['id']; a[k] = v; a }
39
40
  end
40
41
 
41
- def template_decks
42
- JSON.parse(api_resource(:get, template_decks_endpoint))
42
+ # this will return all template_decks without params.(expecting to search with name_start and fingerprint_start)
43
+ # params can be fingerprint: if you want exact match or fingerprint_start: if you want beginning match...
44
+ # e.g. {name_start: "Pandanese", fingerprint_start: "abcd"} or {name: "Pandanese v1.8"}
45
+ def template_decks(params={})
46
+ JSON.parse(api_resource(:get, template_decks_endpoint, params))
43
47
  end
44
48
 
45
49
  def template_deck(id)
@@ -47,11 +51,13 @@ module Sraas
47
51
  end
48
52
 
49
53
  # This returns template cards "index" json
50
- def template_cards(template_deck_id)
51
- JSON.parse(Sraas.api_resource(:get, "#{self.template_decks_endpoint}/#{template_deck_id}/template_cards"))
54
+ def template_cards(template_deck_id:, offset: 0, limit: 100)
55
+ paging = { limit: limit, offset: offset }.to_query
56
+ JSON.parse(Sraas.api_resource(:get, "#{self.template_decks_endpoint}/#{template_deck_id}/template_cards", paging))
52
57
  end
53
58
 
54
59
  # This returns template card "full" json
60
+ # TODO: make this return OpenStruct when we update consumer#card
55
61
  def template_card(template_card_id)
56
62
  JSON.parse(Sraas.api_resource(:get, "#{self.template_cards_endpoint}/#{template_card_id}"))
57
63
  end
@@ -70,9 +76,10 @@ module Sraas
70
76
  api_resource(:get, consumers_endpoint)
71
77
  end
72
78
 
79
+ # TODO: accept not only title and kind. sraas needs to use ransack for it.
73
80
  def search_card(consumer_uuid, title, kind)
74
- query = "?title=#{title}&kind=#{kind}"
75
- JSON.parse(api_resource(:get, card_search_endpoint(consumer_uuid) + query))
81
+ params = {title: title, kind: kind}
82
+ JSON.parse(api_resource(:get, card_search_endpoint(consumer_uuid), params))
76
83
  end
77
84
 
78
85
  def update_card(card_uuid, params = {})
@@ -1,11 +1,12 @@
1
1
  module Sraas
2
2
  class Configuration
3
- attr_accessor :url, :api_key, :default_deck
3
+ attr_accessor :url, :api_key, :default_deck_name, :default_deck_fingerprint
4
4
 
5
5
  def initialize
6
- @url = 'https://www.sraas.io'
7
- @api_key = nil
8
- @default_deck = nil
6
+ @url = 'https://www.sraas.io'
7
+ @api_key = nil
8
+ @default_deck_name = nil
9
+ @default_deck_fingerprint = nil
9
10
  end
10
11
  end
11
12
  end
@@ -80,6 +80,7 @@ module Sraas
80
80
  end
81
81
 
82
82
  # One card accessor. This return card "full" json
83
+ # TODO: make this return OpenStruct.
83
84
  def card(card_uuid)
84
85
  parse_json { get("#{Sraas.cards_endpoint}/#{card_uuid}") }
85
86
  end
@@ -124,8 +125,18 @@ module Sraas
124
125
  OpenStruct.new(progress: template_lesson['progress'], template_cards: template_lesson['template_cards'])
125
126
  end
126
127
 
127
- def template_decks
128
- Sraas.template_decks
128
+ # TODO: accept not only title and kind. sraas needs to use ransack for it.
129
+ # This search template_card with title and kind.
130
+ def search_template_card(title, kind)
131
+ template_deck_id = template_deck['id']
132
+ url = "#{Sraas.template_decks_endpoint}/#{template_deck_id}/template_cards/search"
133
+ params = {title: title, kind: kind}
134
+ JSON.parse(Sraas.api_resource(:get, url, params))
135
+ end
136
+
137
+ # return all template_decks without name and fingerprint
138
+ def template_decks(params={})
139
+ Sraas.template_decks(params)
129
140
  end
130
141
 
131
142
  def unlock_lesson(lesson = 1)
@@ -133,9 +144,20 @@ module Sraas
133
144
  data.code == 200
134
145
  end
135
146
 
136
- def add_template_deck_by_name(deck_name)
137
- deck_uuid = detect_deck_by_name(deck_name).dig('id')
138
- return false unless deck_uuid
147
+ # This needs to return boolean since this is called in has_sraas#add_default_deck which
148
+ # is expecting true or false from this method.
149
+ def add_template_deck(name=nil, fingerprint: nil)
150
+ # At least one of either name or fingerprint has to be provided.
151
+ fail(Sraas::DeckNotSpecified) if name.nil? && fingerprint.nil?
152
+
153
+ # Try all search patterns with name and fingerprint
154
+ # in case one of them is incorrect, but the other correct
155
+ matching_decks = Sraas.template_decks({name_start: name, fingerprint_start: fingerprint})
156
+ matching_decks = Sraas.template_decks({fingerprint_start: fingerprint}) unless matching_decks.any?
157
+ matching_decks = Sraas.template_decks({name_start: name}) unless matching_decks.any?
158
+ return false unless matching_decks.any?
159
+
160
+ deck_uuid = matching_decks.last['id']
139
161
  url = "#{Sraas.template_decks_endpoint}/#{deck_uuid}/add_to_consumer"
140
162
  payload = { consumer_id: [self.consumer_uuid] }
141
163
  # TODO: Handle a request for a deck that doesn't exist elegantly.
@@ -156,10 +178,6 @@ module Sraas
156
178
  Sraas.sraas_consumer_info(consumer_url)
157
179
  end
158
180
 
159
- def detect_deck_by_name(deck_name)
160
- template_decks.detect { |deck| deck['name'] == deck_name } || {}
161
- end
162
-
163
181
  def get(url)
164
182
  Sraas.api_resource(:get, url)
165
183
  end
@@ -1,19 +1,25 @@
1
1
  module Sraas
2
2
  module HasSraas
3
3
  module ClassMethods
4
- def has_sraas(deck: nil)
4
+ def has_sraas(name: nil, fingerprint: nil, deck: nil)
5
+ if deck # v1.0 Syntax
6
+ puts "[DEPRECATION WARNING] Since GEM 0.3.0, semantics changed for has_sraas. You probably want to say has_sraas name: ..."
7
+ name ||= deck # Be nice for now...
8
+ end
5
9
  has_one :sraas, as: :sraas_consumerable, class_name: 'Sraas::Consumer'
6
10
 
7
11
  after_create :configure_sraas_consumer!
8
12
 
9
- self.sraas_default_deck = deck || Sraas.configuration.default_deck
13
+ self.sraas_default_deck_name = name || Sraas.configuration.default_deck_name
14
+ self.sraas_default_deck_fingerprint = fingerprint || Sraas.configuration.default_deck_fingerprint
10
15
  end
11
16
  end
12
17
 
13
18
  module LocalInstanceMethods
14
19
  def self.included(base)
15
20
  base.extend ClassMethods
16
- base.cattr_accessor(:sraas_default_deck)
21
+ base.cattr_accessor(:sraas_default_deck_name)
22
+ base.cattr_accessor(:sraas_default_deck_fingerprint)
17
23
  end
18
24
 
19
25
  def configure_sraas_consumer!
@@ -28,16 +34,25 @@ module Sraas
28
34
  private
29
35
 
30
36
  def add_deck
31
- return true if specified_deck.nil?
32
- specified_deck && add_default_deck
37
+ return true unless specified_deck?
38
+ add_default_deck
33
39
  end
34
40
 
35
41
  def add_default_deck
36
- sraas.add_template_deck_by_name(specified_deck)
42
+ sraas.add_template_deck(specified_deck_name, fingerprint: specified_deck_fingerprint)
37
43
  end
38
44
 
39
- def specified_deck
40
- self.class.sraas_default_deck
45
+ # This returns nil or non-nil if deck is specified with name or fingerprint
46
+ def specified_deck?
47
+ self.class.sraas_default_deck_name || self.class.sraas_default_deck_fingerprint
48
+ end
49
+
50
+ def specified_deck_name
51
+ self.class.sraas_default_deck_name
52
+ end
53
+
54
+ def specified_deck_fingerprint
55
+ self.class.sraas_default_deck_fingerprint
41
56
  end
42
57
 
43
58
  def new_consumer
data/lib/sraas/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sraas
2
- VERSION = '0.2.15'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -5,6 +5,8 @@ Sraas.configure do |config|
5
5
  # API key
6
6
  config.api_key = nil # 'your-api-key'
7
7
 
8
- # Default deck
9
- config.default_deck = nil
8
+ # Default deck name
9
+ config.default_deck_name = nil
10
+ # Default deck fingerptint
11
+ config.default_deck_fingerprint = nil
10
12
  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.15
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Siegel