Class: DataCatalog::Base

Inherits:
Mash
  • Object
show all
Defined in:
lib/base.rb

Class Method Summary collapse

Class Method Details

._first(response) ⇒ Object



60
61
62
63
# File 'lib/base.rb', line 60

def self._first(response)
  item = response['members'][0]
  item.blank? ? nil : new(item)
end

.check_status(response) ⇒ Object

protected



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/base.rb', line 22

def self.check_status(response)
  case response.code
  when 400 then error(BadRequest,          response)
  when 401 then error(Unauthorized,        response)
  when 403 then error(Forbidden,           response)
  when 404 then error(NotFound,            response)
  when 409 then error(Conflict,            response)
  when 500 then error(InternalServerError, response)
  end
  response
end

.cursor(uri, query_hash) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/base.rb', line 34

def self.cursor(uri, query_hash)
  Cursor.new({
    :klass      => self,
    :query_hash => query_hash,
    :response   => http_get(uri, :query => query_hash),
    :uri        => uri,
  })
end

.error(exception_class, response) ⇒ Object



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

def self.error(exception_class, response)
  e = exception_class.new
  e.response_body = response.body
  parsed_response_body = begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    nil
  end
  if parsed_response_body
    e.parsed_response_body = parsed_response_body
    if parsed_response_body.is_a?(Hash) && parsed_response_body["errors"]
      e.errors = parsed_response_body["errors"]
    end
  end
  raise e
end

.filterize(arg) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/base.rb', line 77

def self.filterize(arg)
  if arg.is_a?(Hash)
    filterize_hash(arg)
  elsif arg.is_a?(String)
    arg
  else
    raise ArgumentError
  end
end

.filterize_hash(conditions) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/base.rb', line 87

def self.filterize_hash(conditions)
  filtered_conditions = conditions.map do |k, v|
    "#{k}" + if v.is_a?(Regexp)
      %(:"#{v.source}")
    elsif v.is_a?(Integer) || v.is_a?(TrueClass) || v.is_a?(FalseClass)
      %(=#{v})
    elsif v.is_a?(Array)
      %(=#{v.join(',')})
    else
      %(="#{v}")
    end
  end
  filtered_conditions.join(" and ")
end

.http_delete(path, options = {}) ⇒ Object



4
5
6
# File 'lib/base.rb', line 4

def self.http_delete(path, options={})
  check_status(Connection.delete(path, options))
end

.http_get(path, options = {}) ⇒ Object



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

def self.http_get(path, options={})
  check_status(Connection.get(path, options))
end

.http_post(path, options = {}) ⇒ Object



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

def self.http_post(path, options={})
  check_status(Connection.post(path, options))
end

.http_put(path, options = {}) ⇒ Object



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

def self.http_put(path, options={})
  check_status(Connection.put(path, options))
end

.one(response) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/base.rb', line 65

def self.one(response)
  begin
    return true if response && response.code == 204
  rescue NoMethodError
  end
  response.blank? ? nil : new(response)
end

.query_hash(conditions) ⇒ Object



73
74
75
# File 'lib/base.rb', line 73

def self.query_hash(conditions)
  conditions == {} ? {} : { :filter => filterize(conditions) }
end