Module: Sraas

Defined in:
lib/sraas.rb,
lib/sraas/railtie.rb,
lib/sraas/version.rb,
lib/sraas/consumer.rb,
lib/sraas/has_sraas.rb,
lib/sraas/configuration.rb,
lib/sraas/template_card.rb

Defined Under Namespace

Modules: HasSraas Classes: Configuration, Consumer, Railtie, TemplateCard

Constant Summary collapse

NoAPIKey =
Class.new(StandardError)
DeckNotFound =
Class.new(StandardError)
DeckNotSpecified =
Class.new(StandardError)
VERSION =
'0.5.3'

Class Method Summary collapse

Class Method Details

.api_resource(http_method, url, payload = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/sraas.rb', line 156

def api_resource(http_method, url, payload = {})
  RestClient::Request
    .execute(
      method: http_method,
      url: url,
      payload: payload,
      headers: api_header
    )
end

.bulk_consumers_endpointObject



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

def bulk_consumers_endpoint
  "#{configuration.url}/consumers/bulk"
end

.card_search_endpoint(consumer_uuid) ⇒ Object



140
141
142
# File 'lib/sraas.rb', line 140

def card_search_endpoint(consumer_uuid)
  "#{configuration.url}/consumers/#{consumer_uuid}/cards/search"
end

.cards_endpointObject



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

def cards_endpoint
  "#{configuration.url}/cards"
end

.configurationObject



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

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



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

def configure
  yield(configuration)
end

.consumer_stats_endpointObject



152
153
154
# File 'lib/sraas.rb', line 152

def consumer_stats_endpoint
  "#{configuration.url}/consumer_stats"
end

.consumersObject



113
114
115
# File 'lib/sraas.rb', line 113

def consumers
  api_resource(:get, consumers_endpoint)
end

.consumers_endpointObject



128
129
130
# File 'lib/sraas.rb', line 128

def consumers_endpoint
  "#{configuration.url}/consumers"
end

.default_template_deck_idObject

This gets the deck id from Sraas for the default deck (which is specified by name)



89
90
91
92
93
# File 'lib/sraas.rb', line 89

def default_template_deck_id
  return @default_template_deck_id if @default_template_deck_id
  template_deck = self.template_decks({name_start: configuration.default_deck_name}).last
  @default_template_deck_id = template_deck['id'] if template_deck
end

.search_card(consumer_uuid, title, kind) ⇒ Object

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



118
119
120
121
# File 'lib/sraas.rb', line 118

def search_card(consumer_uuid, title, kind)
  params = {title: title, kind: kind}
  JSON.parse(api_resource(:get, card_search_endpoint(consumer_uuid), params))
end

.search_template_card(template_deck_id = self.default_template_deck_id, 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.



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

def search_template_card(template_deck_id=self.default_template_deck_id, title, kind)
  url = "#{Sraas.template_decks_endpoint}/#{template_deck_id}/template_cards/search"
  params = {title: title, kind: kind}
  JSON.parse(Sraas.api_resource(:get, url, params))
end

.sraas_consumer_create(email, tags = [], time_zone = 'UTC') ⇒ Object



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

def sraas_consumer_create(email, tags = [], time_zone = 'UTC')
  payload = { consumer: { email: email, tags: tags.join(','), time_zone: time_zone } }
  JSON.parse(api_resource(:post, consumers_endpoint, payload))
end

.sraas_consumer_info(consumer_url) ⇒ Object



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

def sraas_consumer_info(consumer_url)
  JSON.parse(api_resource(:get, consumer_url))
end

.sraas_consumer_update_time_zone(consumer_url, time_zone) ⇒ Object



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

def sraas_consumer_update_time_zone(consumer_url, time_zone)
  payload = { consumer: { time_zone: time_zone } }
  JSON.parse(api_resource(:patch, consumer_url, payload))
end

.sraas_consumers_cache(consumer_ids) ⇒ Object

This function exists so an admin-like UI can query many consumer IDs at once and then access them without creating dozens of requests.



39
40
41
42
43
44
45
# File 'lib/sraas.rb', line 39

def sraas_consumers_cache(consumer_ids)
  payload = { consumer_uuids: consumer_ids }
  data = api_resource(:post, bulk_consumers_endpoint, payload)
  consumers = JSON.parse(data)
  @cached_at = Time.now
  @cache = consumers.inject({}) { |a, v| k = v['id']; a[k] = v; a }
end

.sum_template_cards_field_group_by_lesson(template_deck_id:, field:, kind: nil, lesson: nil) ⇒ Object

This returns sum of the field in template_card grouped by lesson.



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

def sum_template_cards_field_group_by_lesson(template_deck_id:, field:, kind: nil, lesson: nil)
  payload = {field: field, kind: kind, lesson: lesson}
  JSON.parse(api_resource(:get, "#{template_decks_endpoint}/#{template_deck_id}/sum_field_group_by_lesson", payload))
end

.template_card(template_card_id) ⇒ Object

This returns template card “full” json TODO: make this return OpenStruct when we update consumer#card



84
85
86
# File 'lib/sraas.rb', line 84

def template_card(template_card_id)
  JSON.parse(Sraas.api_resource(:get, "#{self.template_cards_endpoint}/#{template_card_id}"))
end

.template_card_children(template_card_id) ⇒ Object

This returns template card’s children’s “index” json



104
105
106
# File 'lib/sraas.rb', line 104

def template_card_children(template_card_id)
  JSON.parse(Sraas.api_resource(:get, "#{self.template_cards_endpoint}/#{template_card_id}/children"))
end

.template_card_parents(template_card_id) ⇒ Object

This returns template card’s parent’s “index” json



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

def template_card_parents(template_card_id)
  JSON.parse(Sraas.api_resource(:get, "#{self.template_cards_endpoint}/#{template_card_id}/parents"))
end

.template_cards(template_deck_id = self.default_template_deck_id, filter_params: {}, offset: 0, limit: 100) ⇒ Object

This returns template cards “index” json if limit is > 100, do paging here. filter_params can be by_kind and by_lesson e.g. Sraas.template_cards(1234, filter_params: “Radical”, by_lesson: 1)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sraas.rb', line 67

def template_cards(template_deck_id=self.default_template_deck_id, filter_params: {}, offset: 0, limit: 100)
  template_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, "#{self.template_decks_endpoint}/#{template_deck_id}/template_cards", params))
    break if result.empty?
    template_cards << result
    remainder -= 100
    offset += 100
  end
  template_cards.flatten
end

.template_cards_endpointObject



148
149
150
# File 'lib/sraas.rb', line 148

def template_cards_endpoint
  "#{configuration.url}/template_cards"
end

.template_deck(id) ⇒ Object



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

def template_deck(id)
  JSON.parse(api_resource(:get, "#{template_decks_endpoint}/#{id}"))
end

.template_decks(params = {}) ⇒ Object

this will return all template_decks without params.(expecting to search with name_start and fingerprint_start) params can be fingerprint: if you want exact match or fingerprint_start: if you want beginning match… e.g. “Pandanese”, fingerprint_start: “abcd” or “Pandanese v1.8”



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

def template_decks(params={})
  JSON.parse(api_resource(:get, template_decks_endpoint, params))
end

.template_decks_endpointObject



144
145
146
# File 'lib/sraas.rb', line 144

def template_decks_endpoint
  "#{configuration.url}/template_decks"
end

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



123
124
125
126
# File 'lib/sraas.rb', line 123

def update_card(card_uuid, params = {})
  response = api_resource(:patch, cards_endpoint + "/#{card_uuid}", params)
  response.code == 200
end