Class: JiraRest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jirarest/client.rb

Defined Under Namespace

Classes: Response, Token

Constant Summary collapse

DEFAULT_FIELDS =
'key,summary'
MAXRESULTS =
'300'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jira_host, username = nil, password = nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/jirarest/client.rb', line 12

def initialize(jira_host, username=nil, password=nil)
  jira_url = "#{jira_host}/rest/api/latest/"
  generate jira_url, username, password
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/jirarest/client.rb', line 7

def token
  @token
end

Class Method Details

.construct_url(endpoint, params) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/jirarest/client.rb', line 37

def self.construct_url(endpoint, params)
  p "Query: #{params.jql}" unless params.jql.nil?
  params.fields = DEFAULT_FIELDS if (params.fields.nil? or params.fields.to_s.empty?)
  params.maxresults = MAXRESULTS if (params.maxresults.nil? or params.maxresults.to_s.empty?)

  endpoint + '?' + encode_www_form(params.to_h.delete_if { |k, v| v.nil? }).to_s
end

.encode_www_form(enum) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jirarest/client.rb', line 45

def self.encode_www_form(enum)

  enum.map do |k, v|
    if v.nil?
      URI.encode_www_form_component(k)
    elsif v.respond_to?(:to_ary)
      v.to_ary.map do |w|
        str = URI.encode_www_form_component(k)
        unless w.nil?
          str << '='
          str << w
        end
      end.join('&')
    else
      str = URI.encode_www_form_component(k)
      str << '='
      str << v.to_s
    end
  end.join('&')
end

.get(url, header) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/jirarest/client.rb', line 27

def self.get(url, header)
  begin
    p "Url: #{url}"
    HTTParty.get(url, header)
  rescue Exception::ArgumentError => e
    p e + "/n #{url}"
    return false
  end
end

.handle_response(response, key = nil, parsed_response = false) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jirarest/client.rb', line 67

def self.handle_response(response, key=nil, parsed_response=false)
  raise ArgumentError, 'Response is not a HTTParty::Response' unless response.class == HTTParty::Response
  case response.code
    when 200
      parsed = key.nil? ? response.parsed_response : response.parsed_response[key]
      if parsed_response
        return Response.new(true, parse_response(parsed), nil)
      else
        return Response.new(true, parsed, nil)
      end

    when (400..499)
      msg = response['errorMessages'].nil? ? response.code : response['errorMessages'].join("\n")
      return Response.new(false, response.response.message, msg)
    else
      return Response.new(false, nil, 'fail')
  end
end

.parse_response(response, fields = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jirarest/client.rb', line 86

def self.parse_response(response, fields=nil)
  jira_tickets = []
  if response.has_key? 'issues'
    response['issues'].each do |tickets|
      if !tickets['key'].nil? and !tickets['fields']['summary'].nil?
        jira_tickets << [tickets['key'], tickets['fields']['summary'], tickets['self']]
      end
    end
  elsif !fields.nil?
    sss = ""
     fields.split(',').each { |field|
        p field if response[field]
        sss << response[field]  if response[field]

     }
      p sss
  else
      if !response['key'].nil? and !response['fields']['summary'].nil?
        jira_tickets << [response['key'], response['fields']['summary'], response['self'], response['description']]
      end
  end
  return jira_tickets.sort_by { |x| x.first }
end

Instance Method Details

#generate(url, username = nil, password = nil) ⇒ Object



17
18
19
20
21
# File 'lib/jirarest/client.rb', line 17

def generate(url, username=nil, password=nil)
  header = {'Content-Type' => 'application/json'}
  header['Authorization'] = "Basic #{Base64.strict_encode64(username+':'+password)}" unless username.nil? and password.nil?
  @token = Token.new(url, header)
end

#searchObject



23
24
25
# File 'lib/jirarest/client.rb', line 23

def search
  JiraRest::Search.new(self)
end