Class: Yapt::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params = {}, method = :get) ⇒ Request

Returns a new instance of Request.



7
8
9
# File 'lib/yapt/request.rb', line 7

def initialize(path, params = {}, method = :get)
  @method, @path, @params = method, path, params
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



6
7
8
# File 'lib/yapt/request.rb', line 6

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/yapt/request.rb', line 6

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/yapt/request.rb', line 6

def path
  @path
end

Class Method Details

.base_urlObject



40
41
42
43
# File 'lib/yapt/request.rb', line 40

def self.base_url
  "https://www.pivotaltracker.com/
   services/v5/projects/#{Yapt.project_id}".gsub(/\s+/,'')
end

Instance Method Details

#request(method, payload) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yapt/request.rb', line 15

def request(method, payload)
  options = {
      "X-TrackerToken" => Yapt.api_token,
      "Content-Type" => "application/json"
    }
  response_handling = ->(response, request, result, &block) {
    case response.code
    when 200
      JSON.parse(response.to_s)
    else
      puts "Non-200 response!"
      puts response.to_s
    end
  }
  if method == :get
    RestClient.get(url, options.merge(params: payload), &response_handling)
  else
    RestClient.send(method, url, payload, options, &response_handling)
  end
end

#resultObject



11
12
13
# File 'lib/yapt/request.rb', line 11

def result
  request(method, params)
end

#urlObject



36
37
38
# File 'lib/yapt/request.rb', line 36

def url
  "#{self.class.base_url}/#{path}"
end