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



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

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

.register_application(store) ⇒ Object

Raises:



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

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
  raise JirafeException, 'unable to create jirafe application' unless response.code == 200 &&
                                                                      response['app_id'].present? &&
                                                                      response['token'].present?
  store[:app_id] = response['app_id']
  store[:app_token] = response['token']
  store
end

.synchronize_resources(store) ⇒ Object

Raises:



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

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



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spree/dash/jirafe.rb', line 69

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



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

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