Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/adcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Adcloud
autoload :Entity, "adcloud/entity"
autoload :ExceptionRaiser, "adcloud/exception_raiser"
autoload :MediaFile, "adcloud/media_file"
autoload :EndlessPages, "adcloud/endless_pages"
autoload :Product, "adcloud/product"
autoload :Report, "adcloud/report"
autoload :ReportEntry, "adcloud/report_entry"
Expand Down
2 changes: 1 addition & 1 deletion lib/adcloud/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def valid?
end

# @return [Object] The entity with the unique identifier
def find_by_name(name)
def self.find_by_name(name)
result = connection.get("campaigns/find_by_name", name: name)
result["items"].map { |raw_campaign| self.new(raw_campaign) }
end
Expand Down
11 changes: 9 additions & 2 deletions lib/adcloud/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,40 @@ def authentication_token
end

def connection(auth = true)
Adcloud::logger.debug "Connection Url: #{url}"

auth_header = auth && { :Authorization => "Bearer #{authentication_token}" } || {}
connection ||= Faraday.new(:url => url, :headers => {}.merge(auth_header)) do |faraday|
faraday.request :url_encoded # form-encode POST params
# log requests to STDOUT
faraday.response :logger if Adcloud.config.debug
faraday.use ResponseErrorHandler
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
# log requests to STDOUT
faraday.response :logger# if Adcloud.config.debug
faraday.response :json, :content_type => /\bjson$/
end
end

def post(path, params = {})
Adcloud::logger.debug "POST Url: #{url} #{path}"
Adcloud::logger.debug "POST params: #{params}"
response = connection.post path, params
response.body
end

def get(path, params = {})
Adcloud::logger.debug "GET Url: #{url} #{path}"
response = connection.get path, params
response.body
end

def put(path, params = {})
Adcloud::logger.debug "PUT Url: #{url} #{path}"
response = connection.put path, params
response.body
end

def delete(path, params = {})
Adcloud::logger.debug "DELETE Url: #{url} #{path}"
response = connection.delete path, params
response.body
end
Expand Down
62 changes: 62 additions & 0 deletions lib/adcloud/endless_pages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module EndlessPages
extend ActiveSupport::Concern

module ClassMethods

def endless_pages(opts = {}, &block)
@opts = opts
perform(&block)
end

def conn
@opts[:connection] ||= Adcloud::Connection.new
end

def endpoint
@opts[:endpoint_url] ||= self.endpoint_url
raise "You must either pass endpoint_url into endless_pages OR define a class method .endpoint_url" unless @opts[:endpoint_url]
@opts[:endpoint_url]
end

def params
@opts[:params] ||= {}
end

def perform(&block)
paged_items = []

page = params[:page] ||= 1
page = 1 if page == 0

total_pages = 1
retry_count = 0
page_result = params[:page_result] ||= []

begin
raw_result = conn.get(endpoint, params)
total_pages = raw_result['_meta']['total_pages']

page_result = self.new(raw_result) # your service class needs to include Virtus

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should virtus than not be enforced somehow? Maybe throw an exception if its not included?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how this is enforced in ruby-land

# if a block is provided then start returning results right away
page_result.items.each do |item|
block.call(item)
end if block

paged_items += page_result.items
page += 1

rescue => ex
if retry_count <= 5
retry
else
Adcloud.logger.error { "API Exception #{ex}" }
raise ApiError, "Connection failed"
end
retry_count += 1
end while page < total_pages

paged_items
end

end
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some tests would be nice...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are trying to get this out the door. tests are a nice to have... ie there are none at the moment.. but should be.. but not this sprint

32 changes: 10 additions & 22 deletions lib/adcloud/report.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Adcloud
class Report
include Virtus
include EndlessPages

attribute :_meta, Hash
attribute :items, Array[Adcloud::ReportEntry]
Expand All @@ -13,34 +14,21 @@ def find_by_date(date)
return self.new(result)
end

def find_all_by_date(date)
paged_items = []
page = 0
total_pages = 1
retry_count = 0
page_result = nil
begin
begin
page += 1
raw_result = connection.get(self.api_endpoint, { filter: { date: date.to_s }, :page => page, :per_page => Entity::MAX_PER_PAGE, new_backend: true })
total_pages = raw_result['_meta']['total_pages']
page_result = self.new(raw_result)
paged_items += page_result.items
rescue => ex
retry if retry_count < 5
end
end while page < total_pages
page_result.items = paged_items
page_result
def find_all_by_date(date, booking_ids=[])
params = { filter: { date: date.to_s }, :per_page => Entity::MAX_PER_PAGE, new_backend: true }

if booking_ids
params[:filter][:booking_id] = booking_ids
params[:new_backend] = false
end

endless_pages(endpoint_url: self.api_endpoint, params: params)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this call not look like:

endless_pages({endpoint_url: self.api_endpoint, params: params})

because the first argument from endless_pages should be an hash, or not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no.. its called a splat.. ie.. custom_method(name_a=1, name_b=4, monkies={}) in the def custom_method(opts={})

that opts hash is a representation of all the keyword arguments pased in.. its like **kwargs in python

end

def api_endpoint
@api_endpoint ||= self.name.demodulize.tableize
end

def connection
@connection ||= Connection.new
end
end

# Define this after class methods are added
Expand Down
12 changes: 12 additions & 0 deletions lib/adcloud/report_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ReportEntry
attribute :product_id, Integer
attribute :ad_id, Integer
attribute :campaign_id, Integer
attribute :booking_id, Integer
attribute :page_type, Integer
attribute :ad_position, Integer
attribute :ad_count, Integer
Expand Down Expand Up @@ -44,5 +45,16 @@ class ReportEntry
attribute :language, String
attribute :country, String
attribute :currency, String

def campaign_id
"""
in the case of using v2 reporting system which is being depreciated
it is necessary to convert booking_id to the new campaign_id value
this method is here to ensure all services get the correct value
"""
@campaign_id || @booking_id
end
end


end
2 changes: 1 addition & 1 deletion lib/adcloud/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Adcloud
VERSION = '0.7.6.0'
VERSION = '0.7.6.2'
end
⌂ Proxy