Class: Happi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/happi/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
# File 'lib/happi/client.rb', line 20

def initialize(options = {})
  options.each do |key, value|
    config.send("#{key}=", value)
  end
end

Class Method Details

.configObject



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

def self.config
  @global_config ||= Happi::Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



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

def self.configure
  yield config
end

Instance Method Details

#call(method, url, params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/happi/client.rb', line 50

def call(method, url, params)
  if config.log_level == :debug
    logger.debug("Happi: #{method.upcase} #{config.host}#{url}, #{params}")
  else
    logger.info("Happi: #{method.upcase} #{config.host}#{url}")
  end

  response = connection.send(method, url, params)
  raise_error(response) if errors[response.status]
  response
end

#configObject



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

def config
  @config ||= self.class.config.dup
end

#connectionObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/happi/client.rb', line 96

def connection
  @connection ||= Faraday.new(config.host) do |f|
    # Set OAuth2 Authorization header
    if config.oauth_token.present?
      token_type = config.token_type.presence || 'Bearer'
      f.headers['Authorization'] = "#{token_type} #{config.oauth_token}"
    end

    # Responses are parsed regardless of use_json, matching the unconditional
    # FaradayMiddleware::ParseJson of 0.6.0. use_json only selects the request
    # encoding - JSON body vs multipart/url-encoded.
    f.response :json, content_type: 'application/json'

    if self.config.use_json
      f.request :json
    else
      f.request :multipart
      f.request :url_encoded
    end

    f.adapter :net_http
  end
end

#default_loggerObject



76
77
78
79
80
81
82
# File 'lib/happi/client.rb', line 76

def default_logger
  if defined?(Rails)
    Rails.try(:logger) || Logger.new(STDOUT)
  else
    Logger.new(STDOUT)
  end
end

#delete(resource, params = {}) ⇒ Object



31
32
33
34
# File 'lib/happi/client.rb', line 31

def delete(resource, params = {})
  call(:delete, url(resource), param_check(params))
      .body.with_indifferent_access
end

#errorsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/happi/client.rb', line 120

def errors
  @errors ||= {
    400 => Happi::Error::BadRequest,
    401 => Happi::Error::Unauthorized,
    403 => Happi::Error::Forbidden,
    404 => Happi::Error::NotFound,
    406 => Happi::Error::NotAcceptable,
    408 => Happi::Error::RequestTimeout,
    422 => Happi::Error::UnprocessableEntity,
    429 => Happi::Error::TooManyRequests,
    500 => Happi::Error::InternalServerError,
    502 => Happi::Error::BadGateway,
    503 => Happi::Error::ServiceUnavailable,
    504 => Happi::Error::GatewayTimeout,
  }
end

#get(resource, params = {}) ⇒ Object



26
27
28
29
# File 'lib/happi/client.rb', line 26

def get(resource, params = {})
  call(:get, url(resource), param_check(params))
      .body.with_indifferent_access
end

#loggerObject



72
73
74
# File 'lib/happi/client.rb', line 72

def logger
  @logger ||= default_logger
end

#param_check(params) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/happi/client.rb', line 84

def param_check(params)
  Hash[params.map do |key, value|
    if value.is_a? Hash
      [key, param_check(value)]
    elsif value.respond_to?(:multipart)
      [key, value.multipart]
    else
      [key, value]
    end
  end]
end

#patch(resource, params = {}) ⇒ Object



36
37
38
39
# File 'lib/happi/client.rb', line 36

def patch(resource, params = {})
  call(:patch, url(resource), param_check(params))
      .body.with_indifferent_access
end

#post(resource, params = {}) ⇒ Object



41
42
43
44
# File 'lib/happi/client.rb', line 41

def post(resource, params = {})
  call(:post, url(resource), param_check(params))
      .body.with_indifferent_access
end

#raise_error(response) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/happi/client.rb', line 62

def raise_error(response)
  if response.body['errors']
    message = response.body['errors']
  else
    message = response.body
  end

  fail errors[response.status].new(message, response)
end

#url(resource) ⇒ Object



46
47
48
# File 'lib/happi/client.rb', line 46

def url(resource)
  "/api/#{config.version}/#{resource}"
end