Class: Veyor::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/veyor/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, args, body, options, verbose) ⇒ Request

Returns a new instance of Request.



75
76
77
78
79
80
81
# File 'lib/veyor/request.rb', line 75

def initialize(route, args, body, options, verbose)
  self.route   = route
  self.args    = args
  self.body    = body
  self.options = options
  self.verbose = verbose
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



70
71
72
# File 'lib/veyor/request.rb', line 70

def args
  @args
end

#bodyObject

Returns the value of attribute body.



71
72
73
# File 'lib/veyor/request.rb', line 71

def body
  @body
end

#optionsObject

Returns the value of attribute options.



72
73
74
# File 'lib/veyor/request.rb', line 72

def options
  @options
end

#routeObject

Returns the value of attribute route.



69
70
71
# File 'lib/veyor/request.rb', line 69

def route
  @route
end

#verboseObject

Returns the value of attribute verbose.



73
74
75
# File 'lib/veyor/request.rb', line 73

def verbose
  @verbose
end

Instance Method Details

#_veyor_delete(route) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/veyor/request.rb', line 133

def _veyor_delete(route)
  tok = Veyor.
  if tok.nil?
    raise 'could not find env var APPVEYOR_API_TOKEN; please set it'
  end
  conn = get_conn
  conn.delete do |req|
    req.url '/api/' + route
    req.headers["Authorization"] = "Bearer " + tok
  end
end

#_veyor_get(route, opts) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/veyor/request.rb', line 108

def _veyor_get(route, opts)
  tok = Veyor.
  conn = get_conn
  conn.get do |req|
    req.url '/api/' + route
    req.params = opts
    req.headers["Content-Type"] = "application/json"
    req.headers["Authorization"] = "Bearer " + tok if tok
  end
end

#_veyor_post(route, body) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/veyor/request.rb', line 119

def _veyor_post(route, body)
  tok = Veyor.
  if tok.nil?
    raise 'could not find env var APPVEYOR_API_TOKEN; please set it'
  end
  conn = get_conn
  conn.post do |req|
    req.url '/api/' + route
    req.body = MultiJson.dump(body)
    req.headers["Content-Type"] = "application/json"
    req.headers["Authorization"] = "Bearer " + tok
  end
end

#deleteObject



163
164
165
# File 'lib/veyor/request.rb', line 163

def delete
  return _veyor_delete(self.route).status
end

#getObject



153
154
155
156
# File 'lib/veyor/request.rb', line 153

def get
  res = _veyor_get(self.route, self.args)
  return parse_result(res)
end

#get_connObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/veyor/request.rb', line 83

def get_conn
  # if args.nil?
  #   args = {}
  # end

  if self.verbose
    conn = Faraday.new(:url => Veyor.base_url, :request => self.options) do |f|
      f.request :url_encoded
      f.response :logger
      f.use CustomErrors
      f.adapter  Faraday.default_adapter
    end
  else
    conn = Faraday.new(:url => Veyor.base_url, :request => self.options) do |f|
      f.request :url_encoded
      f.use CustomErrors
      f.adapter  Faraday.default_adapter
    end
  end

  conn.headers[:user_agent] = make_ua
  conn.headers["X-USER-AGENT"] = make_ua
  return conn
end

#parse_result(z) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/veyor/request.rb', line 145

def parse_result(z)
  if z.headers['content-type'].match('json').nil?
    return z.body
  else
    return MultiJson.load(z.body)
  end
end

#postObject



158
159
160
161
# File 'lib/veyor/request.rb', line 158

def post
  res = _veyor_post(self.route, self.body)
  return parse_result(res)
end