Class: YouTrack::Client::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/you_track/client/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.



136
137
138
139
140
141
# File 'lib/you_track/client/mock.rb', line 136

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

  set_current_user
end

Instance Attribute Details

#last_requestObject

Returns the value of attribute last_request.



124
125
126
# File 'lib/you_track/client/mock.rb', line 124

def last_request
  @last_request
end

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/you_track/client/mock.rb', line 2

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



2
3
4
# File 'lib/you_track/client/mock.rb', line 2

def username
  @username
end

Class Method Details

.dataObject



4
5
6
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
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/you_track/client/mock.rb', line 4

def self.data
  @data ||= Hash.new { |h,k|
    h[k] = {
      :issues                => {},
      :comments              => {},
      :users                 => {},
      :projects              => {},
      :project_custom_fields => {},
      :bundles => {
        "States" => {
          "type"   => "state",
          "name"   => "States",
          "values" => [
            {"resolved" => "false", "value" => "Submitted"},
            {"resolved" => "false", "value" => "Open"},
            {"resolved" => "false", "value" => "In Progress"},
            {"resolved" => "false", "value" => "To be discussed"},
            {"resolved" => "false", "value" => "Reopened"},
            {"resolved" => "true",  "value" => "Can't Reproduce"},
            {"resolved" => "true",  "value" => "Duplicate"},
            {"resolved" => "true",  "value" => "Fixed"},
            {"resolved" => "true",  "value" => "Won't fix"},
            {"resolved" => "true",  "value" => "Incomplete"},
            {"resolved" => "true",  "value" => "Obsolete"},
            {"resolved" => "true",  "value" => "Verified"},
            {"resolved" => "false", "value" => "New"}
          ]
        }
      },
      :custom_fields => {
        "Type"              => {
          "name"               => "Type",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "enum[1]",
          "attachBundlePolicy" => 0,
          "defaultBundle"      => "Types",
        },
        "Priority"          => {
          "name"               => "Priority",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "enum[1]",
          "attachBundlePolicy" => 0,
          "defaultBundle"      => "Priorties",
        },
        "State"             => {
          "name"               => "State",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "state[1]",
          "attachBundlePolicy" => 0,
          "defaultBundle"      => "States",
        },
        "Subsystem"         => {
          "name"               => "Subsystem",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "ownedField[1]",
          "attachBundlePolicy" => 1,
          "defaultBundle"      => "Subsystems",
        },
        "Fixed in build"    => {
          "name"               => "Fixed in build",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "build[1]",
          "attachBundlePolicy" => 1,
          "defaultBundle"      => "Builds",
        },
        "Assignee"          => {
          "name"             => "Assignee",
          "isPrivate"        => false,
          "visibleByDefault" => true,
          "autoAttached"     => true,
          "type"             => "user[1]",
        },
        "Affected versions" => {
          "name"               => "Affected versions",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "version[*]",
          "attachBundlePolicy" => 1,
          "defaultBundle"      => "Versions",
        },
        "Fix versions"      => {
          "name"               => "Fix Versions",
          "isPrivate"          => false,
          "visibleByDefault"   => true,
          "autoAttached"       => true,
          "type"               => "version[*]",
          "attachBundlePolicy" => 1,
          "defaultBundle"      => "Versions",
        },
        "Due Date"          => {
          "name"             => "Due Date",
          "isPrivate"        => false,
          "visibleByDefault" => true,
          "autoAttached"     => true,
          "type"             => "date",
        },
      }
    }
  }
end

.reset!Object



116
117
118
# File 'lib/you_track/client/mock.rb', line 116

def self.reset!
  data.clear
end

Instance Method Details

#current_userObject



152
153
154
# File 'lib/you_track/client/mock.rb', line 152

def current_user
  @current_user ||= users.current
end

#dataObject



120
121
122
# File 'lib/you_track/client/mock.rb', line 120

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

#response(options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/you_track/client/mock.rb', line 156

def response(options={})
  body                 = options[:response_body] || options[:body]
  method               = options[:method]        || :get
  params               = options[:params]
  self.last_request    = options[:request_body]
  status               = options[:status]        || 200

  path = options[:path]
  url  = options[:url] || url_for(path, query: params)

  request_headers  = {"Accept"       => "application/xml"}
  response_headers = {"Content-Type" => "application/xml"}.merge(options[:response_headers] || {})

  # request phase
  # * :method - :get, :post, ...
  # * :url    - URI for the current request; also contains GET parameters
  # * :body   - POST parameters for :post/:put requests
  # * :request_headers

  # response phase
  # * :status - HTTP response status code, such as 200
  # * :body   - the response body
  # * :response_headers
  env = Faraday::Env.from(
    :method           => method,
    :url              => URI.parse(url),
    :body             => body,
    :request_headers  => request_headers,
    :response_headers => response_headers,
    :status           => status,
  )

  Faraday::Response::RaiseError.new.on_complete(env) ||
    Faraday::Response.new(env)
end

#set_current_userObject



143
144
145
146
147
148
149
150
# File 'lib/you_track/client/mock.rb', line 143

def set_current_user
  self.data[:users][username] ||= {
    "email"                => "#{username}@example.org",
    "login"                => username,
    "full_name"            => username,
    "last_created_project" => SecureRandom.hex(2),
  }
end

#url_for(path, options = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/you_track/client/mock.rb', line 126

def url_for(path, options={})
  URI.parse(
    File.join(self.url.to_s, "/rest", URI.escape(path.to_s))
  ).tap do |uri|
    if query = options[:query]
      uri.query = Faraday::NestedParamsEncoder.encode(query)
    end
  end.to_s
end