Class: Onering::API::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/onering/api.rb

Direct Known Subclasses

Auth, Devices

Constant Summary collapse

DEFAULT_BASE =
"https://onering"
DEFAULT_PATH =
"/api"
DEFAULT_OPTIONS_FILE =
[
  "./cli.yml",
  "~/.onering/cli.yml",
  "/etc/onering/cli.yml"
]
DEFAULT_CLIENT_PEM =
[
  "./client.pem",
  "~/.onering/client.pem",
  "/etc/onering/client.pem"
]

Class Method Summary collapse

Class Method Details

.connect(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/onering/api.rb', line 32

def connect(options={})
# list all existing config files from least specific to most
  @_configfiles = ([options[:config]] + DEFAULT_OPTIONS_FILE).compact.select{|i|
    (File.exists?(File.expand_path(i)) rescue false)
  }.reverse

# merge all config files with more-specific values overriding less-specific ones
  @_config = {}
  @_configfiles.each do |i|
    c = YAML.load(File.read(File.expand_path(i))) rescue {}
    @_config.deep_merge!(c)
  end

  if options[:host].is_a?(URI)
    @_uri = options[:host]
  elsif options[:host].is_a?(String)
    @_uri = Addressable::URI.parse("#{options[:host]}/#{DEFAULT_PATH}")
  else
    @_uri = Addressable::URI.parse("#{@_config['url'] || DEFAULT_BASE}/#{@_config['apiroot'] || DEFAULT_PATH}")
  end

  if @_uri
    @_pemfile = ([options[:pemfile], @_config['pemfile']]+DEFAULT_CLIENT_PEM).compact.select{|i|
      (File.exists?(File.expand_path(i)) rescue false)
    }.first

    if @_pemfile
      @_pem = File.read(File.expand_path(@_pemfile))
      @_http = Net::HTTP.new(@_uri.host, (@_uri.port || 443))
      @_http.use_ssl = true
      @_http.cert = OpenSSL::X509::Certificate.new(@_pem)
      @_http.key = OpenSSL::PKey::RSA.new(@_pem)
      @_http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
end

.echo(obj) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/onering/api.rb', line 125

def echo(obj)
  if obj.is_a?(Array)
    obj.each do |i|
      puts i
    end
  end
end

.make_filter(filter) ⇒ Object



119
120
121
122
123
# File 'lib/onering/api.rb', line 119

def make_filter(filter)
  filter = filter.collect{|k,v| "#{k}/#{v}" } if filter.is_a?(Hash)
  filter = filter.collect{|i| i.sub(':','/') }.join("/") if filter.is_a?(Array)
  filter
end

.request(endpoint, options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/onering/api.rb', line 69

def request(endpoint, options={})
  options = @_config.merge(options)
  options[:method] = options[:method].to_s.downcase.to_sym
  request = nil

  uri = Addressable::URI.parse("#{@_uri.to_s}/#{endpoint}")
  uri.query_values = options[:fields] if options[:fields]

  raise Errors::NotConnected unless @_http

  #STDERR.puts "DEBUG: Request #{options[:method].to_s.upcase} #{uri.normalize.to_s}"

  case options[:method]
  when :post
    request = Net::HTTP::Post.new(uri.request_uri)
    request.body = options[:data].to_json if options[:data]

  when :delete
    request = Net::HTTP::Delete.new(uri.request_uri)

  else
    request = Net::HTTP::Get.new(uri.request_uri)

  end

  response = @_http.request(request)

  if response.code.to_i >= 400
    rv = JSON.load(response.body) unless response.body.empty?

    if rv['errors']
      msg = "#{rv['errors']['type']}: #{rv['errors']['message']}"
    end

    if response.code.to_i >= 500
      raise Errors::ServerError.new("HTTP #{response.code}: #{msg}")
    else
      raise Errors::ClientError.new("HTTP #{response.code}: #{msg}")
    end
  else
    if response['Content-Type'] == 'application/json'
      rv = (response.body.empty? ? nil : JSON.load(response.body))
    else
      rv = response.body
    end
  end

  rv
end