Module: Manageo

Defined in:
lib/manageo.rb,
lib/manageo/version.rb

Defined Under Namespace

Modules: Company, Subscription Classes: Error, NotFound

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.connectionObject



40
41
42
# File 'lib/manageo.rb', line 40

def connection
  @connection ||= Excon.new(url, headers: {'Ocp-Apim-Subscription-Key' => key})
end

.default_urlObject



12
13
14
# File 'lib/manageo.rb', line 12

def default_url
  'https://api.manageo.com'
end

.env_keyObject



28
29
30
# File 'lib/manageo.rb', line 28

def env_key
  ENV['MANAGEO_KEY']
end

.env_urlObject



16
17
18
# File 'lib/manageo.rb', line 16

def env_url
  ENV['MANAGEO_URL']
end

.get(path, params = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/manageo.rb', line 44

def get(path, params = {})
  response = connection.get(path: File.join('mcompany-api', path))

  case response.status
  when 200, 201
    parse_response response
  when 404
    raise NotFound
  else
    raise "Manageo API returned #{response.status} with #{response.body}"
  end

end

.keyObject



32
33
34
# File 'lib/manageo.rb', line 32

def key
  @key ||= env_key
end

.key=(new_key) ⇒ Object



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

def key=(new_key)
  @key = new_key
end

.parse_response(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/manageo.rb', line 58

def parse_response(response)
  parsed = JSON.parse(response.body)

  case parsed
  when Array
    parsed.collect { |item| OpenStruct.new item }
  when Hash
    if parsed.length == 1
      parsed.values.first
    else
      OpenStruct.new parsed
    end
  end
end

.urlObject



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

def url
  @url ||= env_url || default_url
end

.url=(new_url) ⇒ Object



24
25
26
# File 'lib/manageo.rb', line 24

def url=(new_url)
  @url = new_url
end