Class: HumanityApi

Inherits:
Object
  • Object
show all
Defined in:
lib/humanity_api.rb,
lib/humanity_api/version.rb

Constant Summary collapse

HUMANITY_BASE =
"https://www.humanity.shiftplanning.com/api/"
VALID_REQUEST_METHODS =
%w(GET CREATE UPDATE DELETE)
VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHumanityApi

Returns a new instance of HumanityApi.



13
14
15
16
17
18
# File 'lib/humanity_api.rb', line 13

def initialize
  @token = ENV["HUMANITY_TOKEN"]
  @key = ENV["HUMANITY_KEY"]
  @errors = []
  @output_format = "json"
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/humanity_api.rb', line 8

def key
  @key
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/humanity_api.rb', line 8

def token
  @token
end

Instance Method Details

#build_request_params(data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/humanity_api.rb', line 39

def build_request_params(data)
  data[:token] ||=  @token
  data[:key]   ||=  @key

  request_params = data.dup
  request_params.reject! { |data_key, data_value| data_key == :token || data_key == :key || data_key == :output_format }
  request_params[:method] = data[:method] || "GET"
  request_params[:method].upcase!

  body = {
    :token    => data[:token],
    :key      => data[:key],
    :request  => request_params,
    :output   => @output_format
  }

  check_params_for_errors(body)
  return body
end

#check_params_for_errors(body) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/humanity_api.rb', line 24

def check_params_for_errors(body)
  key_error = "Invalid data packet: please provide a Humanity API key."
  module_error = "Invalid data packet: please provide the Humanity module." 

  @errors << key_error if !body[:key]
  @errors << module_error if !body[:request][:module]
end

#errors?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/humanity_api.rb', line 20

def errors?
  @errors.count > 0
end

#make_request(body) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/humanity_api.rb', line 59

def make_request(body)
  return unless @errors.count == 0

  uri = URI(HUMANITY_BASE)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.path)
  request_params = JSON.generate(body)
  request.set_form_data({"data" => request_params})
  response = http.request(request)

  case response.code.to_i
  when 200 || 201
    JSON.parse(response.body)
  else
    @errors << "Request failed: #{response.value}"
    begin
      JSON.parse(response)
    rescue error
      { error: error }
    end
  end
end

#parse_humanity_response(response) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/humanity_api.rb', line 84

def parse_humanity_response(response)
  if errors?
    { "error" => @errors.join(" ") }
  elsif response["status"] != 1
    response
  else
    response["data"]
  end
end

#request_humanity_data(data) ⇒ Object



32
33
34
35
36
37
# File 'lib/humanity_api.rb', line 32

def request_humanity_data(data)
  @errors = []
  request_params = build_request_params(data)
  humanity_response = make_request(request_params)
  parse_humanity_response(humanity_response)
end