Class: RocketfuelApi::Service

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

Defined Under Namespace

Classes: AdvertisementAdAssignment, AdvertisementCard, AssignedFlight, Campaign, CampaignPixel, Company, Flight, LineItem, Pixel, PixelSearch, Reporting, ReportingDimension, ReportingMetric, SubNetwork, Tactic, ThridPartyPixelSearch

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Service

Returns a new instance of Service.



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

def initialize(connection)
  @connection = connection
end

Instance Method Details

#create(route_params = {}, body = {}) ⇒ Object



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

def create(route_params = {}, body = {})
  raise(RocketfuelApi::NotImplemented, 'Service is read-only.') if @read_only

  body = { uri_name => body }
  route = @connection.build_url(uri_suffix, route_params)
  response = @connection.post(route, body)

  if response.status != 200
    raise RocketfuelApi::BadRequest, response.inspect
  end

  parse_response(response.body)
end

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



30
31
32
33
34
35
36
# File 'lib/rocketfuel_api/service.rb', line 30

def get(id, params = {})
  params = params.merge("#{name}_id" => id)

  response = @connection.get(uri_suffix, params).body

  parse_response(response)
end

#get_all(params = {}) ⇒ Object



38
39
40
41
42
# File 'lib/rocketfuel_api/service.rb', line 38

def get_all(params = {})
  response = @connection.get(uri_suffix, params).body

  parse_response(response)
end

#nameObject



15
16
17
18
19
20
# File 'lib/rocketfuel_api/service.rb', line 15

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

#parse_response(response) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rocketfuel_api/service.rb', line 72

def parse_response(response)
  case response
  when Array
    response.map do |json|
      resource_class.new(json, self)
    end
  when Hash
    resource_class.new(response, self)
  else
    raise format("Can't parse response, it's a %s", response.class)
  end
end

#plural_nameObject



22
23
24
# File 'lib/rocketfuel_api/service.rb', line 22

def plural_name
  name + 's'
end

#resource_classObject



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

def resource_class
  @resource_class ||= begin
    resource_name = name.capitalize.gsub(/(_(.))/) { |_c| Regexp.last_match(2).upcase }
    RocketfuelApi::Resource.const_get(resource_name)
  end
end

#update(id, route_params = {}, body = {}) ⇒ Object

Raises:

  • (AppnexusApi::NotImplemented)


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

def update(id, route_params = {}, body = {})
  raise(AppnexusApi::NotImplemented, 'Service is read-only.') if @read_only

  body = { uri_name => body }
  route = @connection.build_url(uri_suffix, route_params.merge('id' => id))
  response = @connection.put(route, body)

  if response.status != 200
    raise RocketfuelApi::BadRequest, response.inspect
  end

  parse_response(response.body)
end

#uri_suffixObject



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

def uri_suffix
  plural_name
end