Class: AppnexusApi::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/appnexusapi/service.rb

Constant Summary collapse

DEFAULT_NUMBER_OF_ELEMENTS =
100

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Service

Returns a new instance of Service.



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

def initialize(connection)
  @connection = connection
end

Instance Method Details

#create(attributes = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/appnexusapi/service.rb', line 64

def create(attributes={})
  raise(AppnexusApi::NotImplemented, "Service is read-only.") if @read_only
  attributes = { name => attributes }
  response = @connection.post(uri_suffix, attributes).body['response']
  if response['error_id']
    response.delete('dbg')
    raise AppnexusApi::BadRequest.new(response.inspect)
  end
  parse_response(response).first
end

#delete(id) ⇒ Object



86
87
88
89
# File 'lib/appnexusapi/service.rb', line 86

def delete(id)
  raise(AppnexusApi::NotImplemented, "Service is read-only.") if @read_only
  @connection.delete([uri_suffix, id].join('/')).body['response']
end

#get(params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appnexusapi/service.rb', line 38

def get(params={})
  return_response = params.delete(:return_response) || false
  params = {
    "num_elements" => DEFAULT_NUMBER_OF_ELEMENTS,
    "start_element" => 0
  }.merge(params)
  response = @connection.get(uri_suffix, params).body['response']
  if return_response
    response
  else
    parse_response(response)
  end
end

#get_all(params = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/appnexusapi/service.rb', line 52

def get_all(params = {})
  responses = []
  last_responses = get(params)

  while last_responses.size > 0
    responses += last_responses
    last_responses = get(params.merge('start_element' => responses.size))
  end

  responses
end

#nameObject



8
9
10
11
12
13
# File 'lib/appnexusapi/service.rb', line 8

def name
  @name ||= begin
    str = self.class.name.split("::").last.gsub("Service", "")
    str.gsub(/(.)([A-Z])/, '\1_\2').downcase
  end
end

#parse_response(response) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/appnexusapi/service.rb', line 91

def parse_response(response)
  case key = resource_name(response)
  when plural_name, plural_uri_name
    response[key].map do |json|
      resource_class.new(json, self, response['dbg'])
    end
  when name, uri_name
    [resource_class.new(response[key], self, response['dbg'])]
  end
end

#plural_nameObject



15
16
17
# File 'lib/appnexusapi/service.rb', line 15

def plural_name
  name + 's'
end

#plural_uri_nameObject



30
31
32
# File 'lib/appnexusapi/service.rb', line 30

def plural_uri_name
  uri_name + 's'
end

#resource_classObject



19
20
21
22
23
24
# File 'lib/appnexusapi/service.rb', line 19

def resource_class
  @resource_class ||= begin
    resource_name = name.capitalize.gsub(/(_(.))/) { |c| $2.upcase }
    AppnexusApi.const_get(resource_name + "Resource")
  end
end

#resource_name(response) ⇒ Object



102
103
104
# File 'lib/appnexusapi/service.rb', line 102

def resource_name(response)
  [plural_name, plural_uri_name, name, uri_name].detect { |n| response.key?(n) }
end

#update(id, attributes = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/appnexusapi/service.rb', line 75

def update(id, attributes={})
  raise(AppnexusApi::NotImplemented, "Service is read-only.") if @read_only
  attributes = { name => attributes }
  response = @connection.put([uri_suffix, id].join('/'), attributes).body['response']
  if response['error_id']
    response.delete('dbg')
    raise AppnexusApi::BadRequest.new(response.inspect)
  end
  parse_response(response).first
end

#uri_nameObject



26
27
28
# File 'lib/appnexusapi/service.rb', line 26

def uri_name
  name.gsub('_', '-')
end

#uri_suffixObject



34
35
36
# File 'lib/appnexusapi/service.rb', line 34

def uri_suffix
  uri_name
end