Class: Zendesk2::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/zendesk2/mock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zendesk2/mock.rb', line 62

def initialize(options = {})
  @url                 = options[:url]
  @path                = URI.parse(url).path
  @username = options[:username]
  @password = options[:password]
  @token               = options[:token]
  @jwt_token           = options[:jwt_token]

  @current_user ||= data[:users].values.find do |u|
    @username == u['name']
  end || create_user(
    'user' => { 'email' => @username, 'name' => @username }
  ).body['user']

  @current_user_identity ||= data[:identities].values.first
end

Instance Attribute Details

#jwt_tokenObject (readonly)

Returns the value of attribute jwt_token.



3
4
5
# File 'lib/zendesk2/mock.rb', line 3

def jwt_token
  @jwt_token
end

#last_requestObject

Returns the value of attribute last_request.



4
5
6
# File 'lib/zendesk2/mock.rb', line 4

def last_request
  @last_request
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/zendesk2/mock.rb', line 3

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/zendesk2/mock.rb', line 3

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



3
4
5
# File 'lib/zendesk2/mock.rb', line 3

def username
  @username
end

Class Method Details

.dataObject

rubocop:disable Metrics/BlockLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zendesk2/mock.rb', line 7

def self.data
  @data ||= Hash.new do |h, k|
    h[k] = {
      brands: {},
      categories: {},
      forums: {},
      groups: {},
      help_center_access_policies: {},
      help_center_articles: {},
      help_center_categories: {},
      help_center_posts: {},
      help_center_sections: {},
      help_center_subscriptions: {},
      help_center_topics: {},
      help_center_translations: {},
      identities: {},
      memberships: {},
      organizations: {},
      ticket_audits: {},
      ticket_comments: {},
      ticket_fields: {},
      ticket_forms: {},
      ticket_metrics: {},
      tickets: {},
      topic_comments: {},
      topics: {},
      user_fields: {},
      users: {},
      views: {},
    }
  end
end

.resetObject



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

def self.reset
  data.clear
end

.serial_idObject



40
41
42
43
44
# File 'lib/zendesk2/mock.rb', line 40

def self.serial_id
  @current_id ||= 0
  @current_id += 1
  @current_id
end

Instance Method Details

#current_userHash

Lazily re-seeds data after reset

Returns:

  • (Hash)

    current user response



81
82
83
84
85
86
# File 'lib/zendesk2/mock.rb', line 81

def current_user
  data[:users][@current_user['id']]               ||= @current_user
  data[:identities][@current_user_identity['id']] ||= @current_user_identity

  @current_user
end

#dataObject



46
47
48
# File 'lib/zendesk2/mock.rb', line 46

def data
  self.class.data[@url]
end

#resetObject



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

def reset
  data.clear
end

#serial_idObject



58
59
60
# File 'lib/zendesk2/mock.rb', line 58

def serial_id
  self.class.serial_id
end