Class: Dev::TargetProcess

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/target_process.rb,
lib/firespring_dev_commands/target_process/team.rb,
lib/firespring_dev_commands/target_process/time.rb,
lib/firespring_dev_commands/target_process/user.rb,
lib/firespring_dev_commands/target_process/query.rb,
lib/firespring_dev_commands/target_process/project.rb,
lib/firespring_dev_commands/target_process/release.rb,
lib/firespring_dev_commands/target_process/user_story.rb,
lib/firespring_dev_commands/target_process/team_assignment.rb,
lib/firespring_dev_commands/target_process/user_story_history.rb

Overview

Class for querying target process data from their api

Defined Under Namespace

Classes: Config, Project, Query, Release, Team, TeamAssignment, Time, User, UserStory, UserStoryHistory

Constant Summary collapse

CONFIG_FILE =

The config file to try to load credentials from

"#{Dir.home}/.env.tp".freeze
TP_USERNAME =

The text of the username variable key

'TP_USERNAME'.freeze
TP_PASSWORD =

The text of the password variable key

'TP_PASSWORD'.freeze
TP_URL =

The text of the url variable key

'TP_URL'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username: self.class.config.username, password: self.class.config.password, url: self.class.config.url) ⇒ TargetProcess

Initialize a new target process client using the given inputs



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/firespring_dev_commands/target_process.rb', line 47

def initialize(username: self.class.config.username, password: self.class.config.password, url: self.class.config.url)
  raise 'username is required' if username.to_s.strip.empty?
  raise 'password is required' if password.to_s.strip.empty?
  raise 'url is required' if url.to_s.strip.empty?

  @username = username
  @password = password
  @auth = Base64.strict_encode64("#{@username}:#{@password}")
  @url = url
  uri = URI.parse(@url)
  @client = Net::HTTP.new(uri.host, uri.port)
  @client.use_ssl = true
  @client.verify_mode = OpenSSL::SSL::VERIFY_PEER
  @client.set_debug_output(LOG) if self.class.config.http_debug
  @headers = {
    'authorization' => "Basic #{auth}",
    'content-type' => 'application/json',
    'accept' => 'application/json'
  }
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



44
45
46
# File 'lib/firespring_dev_commands/target_process.rb', line 44

def auth
  @auth
end

#clientObject

Returns the value of attribute client.



44
45
46
# File 'lib/firespring_dev_commands/target_process.rb', line 44

def client
  @client
end

#headersObject

Returns the value of attribute headers.



44
45
46
# File 'lib/firespring_dev_commands/target_process.rb', line 44

def headers
  @headers
end

#passwordObject

Returns the value of attribute password.



44
45
46
# File 'lib/firespring_dev_commands/target_process.rb', line 44

def password
  @password
end

#urlObject

Returns the value of attribute url.



44
45
46
# File 'lib/firespring_dev_commands/target_process.rb', line 44

def url
  @url
end

#usernameObject

Returns the value of attribute username.



44
45
46
# File 'lib/firespring_dev_commands/target_process.rb', line 44

def username
  @username
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Instantiates a new top level config object if one hasn’t already been created Yields that config object to any given block Returns the resulting config object

Yields:



34
35
36
37
38
# File 'lib/firespring_dev_commands/target_process.rb', line 34

def config
  @config ||= Config.new
  yield(@config) if block_given?
  @config
end

Instance Method Details

#get(path, query) ⇒ Object

Perform a get request to the given path using the given query Call the given block (if present) with each piece of data Return all pieces of data



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/firespring_dev_commands/target_process.rb', line 131

def get(path, query, &)
  return [] if query.empty?

  query_string = query.generate
  url = "/api/v1/#{path}"
  url << "?#{URI.encode_www_form(query_string)}" unless query_string.empty?

  response = client.request_get(url, headers)
  raise "Error querying #{url} [#{query_string}]: #{response.inspect}" unless response.response.is_a?(Net::HTTPSuccess)

  parsed_response = JSON.parse(response.body)
  return parsed_response unless parsed_response.key?('Items')

  parsed_response['Items'].each(&)

  while parsed_response['Next']
    next_query_string = URI(parsed_response['Next']).query
    next_url = "/api/v1/#{path}"
    next_url << "?#{next_query_string}" unless query_string.empty?
    response = client.request_get(next_url, headers)
    raise "Error querying #{next_url} [#{next_query_string}]: #{response.inspect}" unless response.response.is_a?(Net::HTTPSuccess)

    parsed_response = JSON.parse(response.body)
    return parsed_response unless parsed_response.key?('Items')

    parsed_response['Items'].each(&)
  end

  nil
end

#releases(query) ⇒ Object

Perform a query to the release api path Call the given block (if present) with each release Return all releases



71
72
73
74
75
76
77
78
# File 'lib/firespring_dev_commands/target_process.rb', line 71

def releases(query, &)
  [].tap do |ary|
    get(Release::PATH, query) do |result|
      ary << Release.new(result)
      yield ary.last if block_given?
    end
  end
end

#team_assignments(query) ⇒ Object

Perform a query to the team assignments api path Call the given block (if present) with each team assignment Return all team assignments



107
108
109
110
111
112
113
114
# File 'lib/firespring_dev_commands/target_process.rb', line 107

def team_assignments(query, &)
  [].tap do |ary|
    get(TeamAssignment::PATH, query) do |result|
      ary << TeamAssignment.new(result)
      yield ary.last if block_given?
    end
  end
end

#times(query) ⇒ Object

Perform a query to the time api path Call the given block (if present) with each time Return all times



119
120
121
122
123
124
125
126
# File 'lib/firespring_dev_commands/target_process.rb', line 119

def times(query, &)
  [].tap do |ary|
    get(Time::PATH, query) do |result|
      ary << Time.new(result)
      yield ary.last if block_given?
    end
  end
end

#user_stories(query) ⇒ Object

Perform a query to the user story api path Call the given block (if present) with each user story Return all user stories



83
84
85
86
87
88
89
90
# File 'lib/firespring_dev_commands/target_process.rb', line 83

def user_stories(query, &)
  [].tap do |ary|
    get(UserStory::PATH, query) do |result|
      ary << UserStory.new(result)
      yield ary.last if block_given?
    end
  end
end

#user_story_histories(query) ⇒ Object

Perform a query to the user story history api path Call the given block (if present) with each user story history Return all user stories



95
96
97
98
99
100
101
102
# File 'lib/firespring_dev_commands/target_process.rb', line 95

def user_story_histories(query, &)
  [].tap do |ary|
    get(UserStoryHistory::PATH, query) do |result|
      ary << UserStoryHistory.new(result)
      yield ary.last if block_given?
    end
  end
end