Class: Spree::Dash::Jirafe

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/spree/dash/jirafe.rb

Class Method Summary collapse

Class Method Details

.register(store) ⇒ Object



12
13
14
15
16
# File 'lib/spree/dash/jirafe.rb', line 12

def register(store)
  validate_required_keys! store
  store = register_application(store)
  store = synchronize_resources(store)
end

.register_application(store) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spree/dash/jirafe.rb', line 26

def register_application(store)
  return if store[:app_id].present? && store[:app_token].present?

  options = {
    :body => {
      :name => store[:name],
      :url => store[:url]
    }
  }
  response = post '/applications', options
  if response.code == 200
    store[:app_id] = response['app_id']
    store[:app_token] = response['token']
    return store
  elsif response.code == 503
    raise JirafeUnavailable, Spree.t(:'dash.jirafe.currently_unavailable')
  else
    raise JirafeException, 'unable to create jirafe application'
  end
end

.synchronize_resources(store) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spree/dash/jirafe.rb', line 47

def synchronize_resources(store)
  return unless store.has_key?(:app_id) and store.has_key?(:app_token)

  options = {
    :headers => { 'Content-type' => 'application/json' },
    :query => { :token => store[:app_token] },
    :body => {
      :sites => [{ :description => store[:name],
                :url => store[:url],
                :currency => store[:currency],
                :timezone => store[:time_zone],
                :external_id => 1,
                :site_id => store[:site_id] }],
      :platform_type => 'spree',
      :users => users_hash
    }.to_json
  }
  response = post "/applications/#{store[:app_id]}/resources", options
  raise JirafeException, 'unable to synchronize store' unless response.code == 200 &&
                                                              response['sites'].present? &&
                                                              response['users'].present?
  store[:site_id] = response["sites"].first["site_id"]
  store[:site_token] = response["users"].first["token"]
  store
end

.users_hashObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/spree/dash/jirafe.rb', line 73

def users_hash
  users = []
  Spree.user_class.includes(:spree_roles).where("spree_roles.name = 'admin'").each do |user|
    users << {
      :email => user.email,
      :first_name => 'Spree',
      :last_name => 'User'
    }
  end
  users
end

.validate_required_keys!(store) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/spree/dash/jirafe.rb', line 18

def validate_required_keys!(store)
  [:first_name, :url, :email, :currency, :time_zone, :name].each do |key|
    unless store[key].present?
      raise JirafeException, "#{key.to_s.titleize} is required"
    end
  end
end