talking to bettervideo

This commit is contained in:
Tyrel Souza 2014-04-30 13:57:58 -04:00
parent 21490ba03f
commit 6e06426db7
3 changed files with 56 additions and 7 deletions

View File

@ -42,8 +42,8 @@ class CampaignsController < ApplicationController
if @campaign.update(campaign_params)
if params[:send]
client = BetterVideo.new(:url => "xxx")
bv_response = client.addVideo(campaign: @campaign)
client = BetterVideo.new #(:url => "xxx")
bv_response = client.addVideo(campaign: @campaign.to_bettervideo_format)
if bv_response[:success]
notice = bv_response[:message]
@campaign.update({sent: true})

View File

@ -1,12 +1,43 @@
require 'net/http'
require 'uri'
require 'json'
class BetterVideo
def initialize(options={})
@addvideo_url = options.fetch(:url, "http://servicestest.bettervideo.com/orders/MDS.OrdersWS.svc/addVideo")
end
def addVideo(campaign)
response = Hash.new
response[:success] = true
response[:message] = "Submitted to better video."
response
uri = URI.parse(@addvideo_url)
header = {}#'Content-Type' => 'text/json'}
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
private
def valid_json? json_
JSON.parse(json_)
return true
rescue JSON::ParserError
return false
end
end

View File

@ -1,3 +1,5 @@
require 'json'
class Campaign < ActiveRecord::Base
has_many :videolistings, :dependent => :delete_all
accepts_nested_attributes_for :videolistings,:reject_if => :all_blank, :allow_destroy => true
@ -12,6 +14,15 @@ class Campaign < ActiveRecord::Base
def set_defaults
self.clientid = 720
end
def to_builder
Jbuilder.new do |json|
json.partial! 'campaigns/campaign', campaign: self
end
end
def has_at_least_one_videolisting
errors.add(:base, 'Must add at least one Videolisting') if self.videolistings.blank?
end
@ -23,6 +34,13 @@ class Campaign < ActiveRecord::Base
0
end
def to_bettervideo_format
campaign = self.as_json(include: :videolistings)
campaigns = {campaigns: [campaign,]}
campaigns.to_json
end
# def send_to_better_video
# if self.sent_changed? && self.sent? == true
#