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.



6
7
8
# File 'lib/rocketfuel_api/service.rb', line 6

def initialize(connection)
  @connection = connection
end

Instance Method Details

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rocketfuel_api/service.rb', line 49

def create(route_params = {}, body = {})
  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



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

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



43
44
45
46
47
# File 'lib/rocketfuel_api/service.rb', line 43

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

  parse_response(response)
end

#nameObject



17
18
19
20
21
22
# File 'lib/rocketfuel_api/service.rb', line 17

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

#parse_response(response) ⇒ Object



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

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(RocketfuelApi::NotImplemented, format('Unknown response type %s.', response.class))
  end
end

#resource_classObject



10
11
12
13
14
15
# File 'lib/rocketfuel_api/service.rb', line 10

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



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

def update(id, route_params = {}, body = {})
  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



24
25
26
27
28
29
30
31
32
33
# File 'lib/rocketfuel_api/service.rb', line 24

def uri_suffix
  @@endpoints ||= YAML.load_file(
    RocketfuelApi.root.join('lib', 'config', 'endpoints_for_services.yaml')
  )

  endpoint = @@endpoints['service'][name]

  endpoint || raise(RocketfuelApi::NotImplemented,
    format('No endpoint for service %s available.', name))
end