talking to bettervideo
This commit is contained in:
parent
21490ba03f
commit
6e06426db7
@ -42,8 +42,8 @@ class CampaignsController < ApplicationController
|
|||||||
|
|
||||||
if @campaign.update(campaign_params)
|
if @campaign.update(campaign_params)
|
||||||
if params[:send]
|
if params[:send]
|
||||||
client = BetterVideo.new(:url => "xxx")
|
client = BetterVideo.new #(:url => "xxx")
|
||||||
bv_response = client.addVideo(campaign: @campaign)
|
bv_response = client.addVideo(campaign: @campaign.to_bettervideo_format)
|
||||||
if bv_response[:success]
|
if bv_response[:success]
|
||||||
notice = bv_response[:message]
|
notice = bv_response[:message]
|
||||||
@campaign.update({sent: true})
|
@campaign.update({sent: true})
|
||||||
|
@ -1,12 +1,43 @@
|
|||||||
|
require 'net/http'
|
||||||
|
require 'uri'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
class BetterVideo
|
class BetterVideo
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
@addvideo_url = options.fetch(:url, "http://servicestest.bettervideo.com/orders/MDS.OrdersWS.svc/addVideo")
|
@addvideo_url = options.fetch(:url, "http://servicestest.bettervideo.com/orders/MDS.OrdersWS.svc/addVideo")
|
||||||
end
|
end
|
||||||
|
|
||||||
def addVideo(campaign)
|
def addVideo(campaign)
|
||||||
response = Hash.new
|
uri = URI.parse(@addvideo_url)
|
||||||
response[:success] = true
|
header = {}#'Content-Type' => 'text/json'}
|
||||||
response[:message] = "Submitted to better video."
|
|
||||||
response
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
|
request = Net::HTTP::Post.new(uri.request_uri, header)
|
||||||
|
request.body = campaign[:campaign]
|
||||||
|
response = http.request(request)
|
||||||
|
|
||||||
|
resp = JSON.parse(response.body)
|
||||||
|
puts resp
|
||||||
|
|
||||||
|
if resp["success"]
|
||||||
|
resp[:success] = true
|
||||||
|
resp[:message] = "Successfully submitted to BetterVideo, Please wait."
|
||||||
|
else
|
||||||
|
resp[:message] = resp["errormessage"]
|
||||||
|
end
|
||||||
|
# "{\"errorcode\":1,\"errormessage\":\"There was an error parsing the supplied JSON.\",\"listingcode\":null,\"success\":false,\"videocode\":null}"
|
||||||
|
|
||||||
|
resp
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
def valid_json? json_
|
||||||
|
JSON.parse(json_)
|
||||||
|
return true
|
||||||
|
rescue JSON::ParserError
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require 'json'
|
||||||
|
|
||||||
class Campaign < ActiveRecord::Base
|
class Campaign < ActiveRecord::Base
|
||||||
has_many :videolistings, :dependent => :delete_all
|
has_many :videolistings, :dependent => :delete_all
|
||||||
accepts_nested_attributes_for :videolistings,:reject_if => :all_blank, :allow_destroy => true
|
accepts_nested_attributes_for :videolistings,:reject_if => :all_blank, :allow_destroy => true
|
||||||
@ -12,6 +14,15 @@ class Campaign < ActiveRecord::Base
|
|||||||
def set_defaults
|
def set_defaults
|
||||||
self.clientid = 720
|
self.clientid = 720
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_builder
|
||||||
|
Jbuilder.new do |json|
|
||||||
|
json.partial! 'campaigns/campaign', campaign: self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def has_at_least_one_videolisting
|
def has_at_least_one_videolisting
|
||||||
errors.add(:base, 'Must add at least one Videolisting') if self.videolistings.blank?
|
errors.add(:base, 'Must add at least one Videolisting') if self.videolistings.blank?
|
||||||
end
|
end
|
||||||
@ -23,6 +34,13 @@ class Campaign < ActiveRecord::Base
|
|||||||
0
|
0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def to_bettervideo_format
|
||||||
|
campaign = self.as_json(include: :videolistings)
|
||||||
|
campaigns = {campaigns: [campaign,]}
|
||||||
|
campaigns.to_json
|
||||||
|
end
|
||||||
|
|
||||||
# def send_to_better_video
|
# def send_to_better_video
|
||||||
# if self.sent_changed? && self.sent? == true
|
# if self.sent_changed? && self.sent? == true
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user