lib/two_performant.rb in 2Performant-0.0.8 vs lib/two_performant.rb in 2Performant-0.1.0

- old
+ new

@@ -1,12 +1,13 @@ require 'httparty' +require 'json' require 'two_performant/oauth' class TwoPerformant include HTTParty - format :xml - headers 'Content-Type' => 'text/xml' + format :json + headers 'Content-Type' => 'text/json' attr_accessor :user, :pass, :host, :version, :auth_type, :oauth, :oauth_request def initialize(auth_type, auth_obj, host) if auth_type == :simple @@ -25,17 +26,17 @@ # ======= # Users # ======= def user_show(user_id) - self.hook("/users/#{user_id}.xml", "user"); + self.hook("/users/#{user_id}.json", "user"); end # Display public information about the logged in user def user_loggedin - self.hook("/users/loggedin.xml", "user"); + self.hook("/users/loggedin.json", "user"); end # =========== # Campaigns # =========== @@ -46,50 +47,50 @@ 'category_id' => category_id, 'page' => page, 'perpage' => perpage } - self.hook("/campaigns.xml", "campaign", request, 'GET') + self.hook("/campaigns.json", "campaign", request, 'GET') end # Search for campaigns def campaigns_search(search, page = 1, perpage = 6) request = { 'page' => page, 'perpage' => perpage, 'search' => search } - self.hook("/campaigns/search.xml", "campaign", request, 'GET') + self.hook("/campaigns/search.json", "campaign", request, 'GET') end # Display public information about a campaign def campaign_show(campaign_id) - self.hook("/campaigns/#{campaign_id}.xml", "campaign") + self.hook("/campaigns/#{campaign_id}.json", "campaign") end # Affiliates: List campaigns which have the logged in user accepted def campaigns_listforaffiliate - self.hook("/campaigns/listforaffiliate.xml", "campaign") + self.hook("/campaigns/listforaffiliate.json", "campaign") end # Merchants: List all campaigns created by the logged in user def campaigns_listforowner - self.hook("/campaigns/listforowner.xml", "campaign") + self.hook("/campaigns/listforowner.json", "campaign") end # Merchants: Display complete information about a campaign (only available to owner) def campaign_showforowner(campaign_id) - self.hook("/campaigns/#{campaign_id}/showforowner.xml", "campaign") + self.hook("/campaigns/#{campaign_id}/showforowner.json", "campaign") end # Merchants: Update a campaign def campaign_update(campaign_id, campaign) request = { 'campaign' => campaign } - self.hook("/campaigns/#{campaign_id}.xml", "campaign", request, 'PUT') + self.hook("/campaigns/#{campaign_id}.json", "campaign", request, 'PUT') end # Create a Deep Link. This method was created so it wouldn't make a request for every Quick Link. # You may need to get some data before using it. def campaign_quicklink(campaign_id, aff_code, redirect) @@ -109,11 +110,11 @@ def sale_create(campaign_id, sale) request = { 'sale' => sale } - self.hook("/campaigns/#{campaign_id}/sales.xml", "sale", request, "POST") + self.hook("/campaigns/#{campaign_id}/sales.json", "sale", request, "POST") end # ============ # Leads # ============ @@ -121,11 +122,11 @@ def lead_create(campaign_id, lead) request = { 'lead' => lead } - self.hook("/campaigns/#{campaign_id}/leads.xml", "lead", request, "POST") + self.hook("/campaigns/#{campaign_id}/leads.json", "lead", request, "POST") end # ============ # Affiliates # ============ @@ -136,19 +137,19 @@ 'page' => page, 'perpage' => perpage, 'search' => search } - self.hook("/affiliates/search.xml", "user", request, 'GET') + self.hook("/affiliates/search.json", "user", request, 'GET') end # Merchants: List affiliates approved in campaigns def affiliates_listformerchant(campaign_id=nil) request = { 'campaign_id' => campaign_id } - self.hook("/affiliates/listformerchant.xml", "user", request, 'GET') + self.hook("/affiliates/listformerchant.json", "user", request, 'GET') end # ============= # Commissions # ============= @@ -163,46 +164,46 @@ 'perpage' => perpage } request.merge(options) - self.hook("/commissions/search.xml", "commission", request, 'GET') + self.hook("/commissions/search.json", "commission", request, 'GET') end # Merchants: List commissions on campaigns. Month: 01 to 12; Year: 20xx. def commissions_listformerchant(campaign_id, month, year) request = { 'campaign_id' => campaign_id, 'month' => month, 'year' => year } - self.hook("/commissions/listformerchant.xml", "campaign", request, 'GET') + self.hook("/commissions/listformerchant.json", "campaign", request, 'GET') end # Affiliates: List commissions on campaigns. Month: 01 to 12; Year: 20xx. def commissions_listforaffiliate(campaign_id, month, year) request = { 'campaign_id' => campaign_id, 'month' => month, 'year' => year } - self.hook("/commissions/listforaffiliate.xml", "commission", request, 'GET') + self.hook("/commissions/listforaffiliate.json", "commission", request, 'GET') end # Merchant Campaign Owner or Affiliate Commission Owner: Show information about a commission def commission_show(commission_id) - self.hook("/commissions/#{commission_id}.xml", "commission") + self.hook("/commissions/#{commission_id}.json", "commission") end # Merchant: Update a commission def commission_update(commission_id, commission) request = { 'commission' => commission } - self.hook("/commissions/#{commission_id}.xml", "commission", request, 'PUT') + self.hook("/commissions/#{commission_id}.json", "commission", request, 'PUT') end # ======= # Sites # ======= @@ -213,46 +214,46 @@ 'category_id' => category_id, 'page' => page, 'perpage' => perpage } - self.hook("/sites.xml", "site", request) + self.hook("/sites.json", "site", request) end # Display information about a site def site_show(site_id) - self.hook("/sites/#{site_id}.xml", "site") + self.hook("/sites/#{site_id}.json", "site") end # Search for sites def sites_search(search, page=1, perpage=6) request = { 'page' => page, 'perpage' => perpage, 'search' => search } - self.hook("/sites/search.xml", "site", request, 'GET') + self.hook("/sites/search.json", "site", request, 'GET') end # Affiliates: List all sites created by the logged in user def sites_listforowner - self.hook("/sites/listforowner.xml", "site") + self.hook("/sites/listforowner.json", "site") end # Affiliates: Update a site def site_update(site_id, site) request = { 'site' => site } - self.hook("/sites/#{site_id}.xml", "site", request, 'PUT') + self.hook("/sites/#{site_id}.json", "site", request, 'PUT') end # Affiliates: Destroy a site def site_destroy(site_id) - self.hook("/sites/#{site_id}.xml", "site", request, 'DELETE') + self.hook("/sites/#{site_id}.json", "site", request, 'DELETE') end # ============ # Text Links # ============ @@ -262,16 +263,16 @@ request = { 'page' => page, 'perpage' => perpage } - self.hook("/campaigns/#{campaign_id}/txtlinks.xml", "txtlink", request, 'GET') + self.hook("/campaigns/#{campaign_id}/txtlinks.json", "txtlink", request, 'GET') end # Display information about a text link def txtlink_show(campaign_id, txtlink_id) - self.hook("/campaigns/#{campaign_id}/txtlinks/#{txtlink_id}.xml", "txtlink") + self.hook("/campaigns/#{campaign_id}/txtlinks/#{txtlink_id}.json", "txtlink") end # Search for text links in a campaign def txtlinks_search(campaign_id, search, page=1, perpage=6, sort='date') request = { @@ -279,11 +280,11 @@ 'perpage' => perpage, 'search' => search, 'sort' => sort, } - self.hook("/campaigns/#{campaign_id}/txtlinks/search.xml", "txtlink", request, 'GET') + self.hook("/campaigns/#{campaign_id}/txtlinks/search.json", "txtlink", request, 'GET') end # # Merchants: Create Text Link. # @@ -296,24 +297,24 @@ def txtlink_create(campaign_id, txtlink) request = { 'txtlink' => txtlink } - self.hook("/campaigns/#{campaign_id}/txtlinks.xml", "txtlink", request, 'POST') + self.hook("/campaigns/#{campaign_id}/txtlinks.json", "txtlink", request, 'POST') end # Merchants: Update a text link def txtlink_update(campaign_id, txtlink_id, txtlink) request = { 'txtlink' => txtlink } - self.hook("/campaigns/#{campaign_id}/txtlinks/#{txtlink_id}.xml", "txtlink", request, 'PUT') + self.hook("/campaigns/#{campaign_id}/txtlinks/#{txtlink_id}.json", "txtlink", request, 'PUT') end # Merchants: Destroy a text link def txtlink_destroy(campaign_id, txtlink_id) - self.hook("/campaigns/#{campaign_id}/txtlinks/#{txtlink_id}.xml", "txtlink", nil, 'DELETE') + self.hook("/campaigns/#{campaign_id}/txtlinks/#{txtlink_id}.json", "txtlink", nil, 'DELETE') end # ============ # Text Ads # ============ @@ -323,16 +324,16 @@ request = { 'page' => page, 'perpage' => perpage } - self.hook("/campaigns/#{campaign_id}/txtads.xml", "txtad", request, 'GET') + self.hook("/campaigns/#{campaign_id}/txtads.json", "txtad", request, 'GET') end # Display information about a text ad def txtad_show(campaign_id, txtad_id) - self.hook("/campaigns/#{campaign_id}/txtads/#{txtad_id}.xml", "txtad") + self.hook("/campaigns/#{campaign_id}/txtads/#{txtad_id}.json", "txtad") end # Search for text ads in a campaign def txtads_search(campaign_id, search, page=1, perpage=6, sort='date') request = { @@ -340,11 +341,11 @@ 'perpage' => perpage, 'search' => search, 'sort' => sort } - self.hook("/campaigns/#{campaign_id}/txtads/search.xml", "txtad", request, 'GET') + self.hook("/campaigns/#{campaign_id}/txtads/search.json", "txtad", request, 'GET') end # # Merchants: Create Text Ad. # Txtad must be a hash of: @@ -356,26 +357,26 @@ def txtad_create(campaign_id, txtad) request = { 'txtad' => txtad } - self.hook("/campaigns/#{campaign_id}/txtads.xml", "txtad", request, 'POST') + self.hook("/campaigns/#{campaign_id}/txtads.json", "txtad", request, 'POST') end # Merchants: Update a text ad def txtad_update(campaign_id, txtad_id, txtad) request = { 'txtad' => txtad } - self.hook("/campaigns/#{campaign_id}/txtads/#{txtad_id}.xml", "txtad", request, 'PUT') + self.hook("/campaigns/#{campaign_id}/txtads/#{txtad_id}.json", "txtad", request, 'PUT') end # Merchants: Destroy a text ad def txtad_destroy(campaign_id, txtad_id) - self.hook("/campaigns/#{campaign_id}/txtads/#{txtad_id}.xml", "txtad", nil, 'DELETE') + self.hook("/campaigns/#{campaign_id}/txtads/#{txtad_id}.json", "txtad", nil, 'DELETE') end # ========= # Banners # ========= @@ -385,16 +386,16 @@ request = { 'page' => page, 'perpage' => perpage } - self.hook("/campaigns/#{campaign_id}/banners.xml", "banner", request, 'GET') + self.hook("/campaigns/#{campaign_id}/banners.json", "banner", request, 'GET') end # Display information about a banner def banner_show(campaign_id, banner_id) - self.hook("/campaigns/#{campaign_id}/banners/#{banner_id}.xml", "banner") + self.hook("/campaigns/#{campaign_id}/banners/#{banner_id}.json", "banner") end # Search for banners in a campaign def banners_search(campaign_id, search, page=1, perpage=6, sort='date') request = { @@ -402,35 +403,35 @@ 'perpage' => perpage, 'search' => search, 'sort' => sort } - self.hook("/campaigns/#{campaign_id}/banners/search.xml", "banner", request, 'GET') + self.hook("/campaigns/#{campaign_id}/banners/search.json", "banner", request, 'GET') end # Merchants: Create a banner def banner_create(campaign_id, banner, banner_picture) request = { 'banner' => banner, 'banner_picture' => banner_picture } - self.hook("/campaigns/#{campaign_id}/banners.xml", "banner", request, 'POST') + self.hook("/campaigns/#{campaign_id}/banners.json", "banner", request, 'POST') end # Merchants: Update a banner def banner_update(campaign_id, banner_id, banner) request = { 'banner' => banner } - self.hook("/campaigns/#{campaign_id}/banners/#{banner_id}.xml", "banner", request, 'PUT') + self.hook("/campaigns/#{campaign_id}/banners/#{banner_id}.json", "banner", request, 'PUT') end # Merchants: Destroy a banner def banner_destroy(campaign_id, banner_id) - self.hook("/campaigns/#{campaign_id}/banners/#{banner_id}.xml", "banner", nil, 'DELETE') + self.hook("/campaigns/#{campaign_id}/banners/#{banner_id}.json", "banner", nil, 'DELETE') end # =============== # Product Stores # =============== @@ -439,16 +440,16 @@ def product_stores_list(campaign_id) request = { 'campaign_id' => campaign_id } - self.hook("/product_stores.xml", "product-store", request) + self.hook("/product_stores.json", "product-store", request) end # Show a Product Store def product_store_show(product_store_id) - self.hook("/product_stores/#{product_store_id}.xml", "product-store") + self.hook("/product_stores/#{product_store_id}.json", "product-store") end # Show Products from a Product Store def product_store_showitems(product_store_id, category=nil, page=1, perpage=6, uniq_products=nil) request = { @@ -457,20 +458,20 @@ 'perpage' => perpage } request['uniq_products'] = uniq_products if (uniq_products) - self.hook("/product_stores/#{product_store_id}/showitems.xml", "product-store-data", request) + self.hook("/product_stores/#{product_store_id}/showitems.json", "product-store-data", request) end # Show a Product from a Product Store def product_store_showitem(product_store_id, product_id) request = { 'product_id' => product_id } - self.hook("/product_stores/#{product_store_id}/showitem.xml", "product-store-data", request) + self.hook("/product_stores/#{product_store_id}/showitem.json", "product-store-data", request) end # Search for Products in a Product Store def product_store_products_search(campaign_id, search, product_store_id='all', category=nil, page=1, perpage=6, sort='date', uniq_products=false) @@ -485,25 +486,25 @@ request['uniq_products'] = uniq_products if uniq_products product_store_id = 'all' if !product_store_id - self.hook("/product_stores/#{product_store_id}/searchpr.xml", "product-store-data", request, 'GET') + self.hook("/product_stores/#{product_store_id}/searchpr.json", "product-store-data", request, 'GET') end # Merchants: Update a Product Store def product_store_update(product_store_id, product_store) request = { 'product_store' => product_store } - self.hook("/product_stores/#{product_store_id}.xml", "product-store", request, 'PUT') + self.hook("/product_stores/#{product_store_id}.json", "product-store", request, 'PUT') end # Merchants: Destroy a Product Store def product_store_destroy(product_store_id) - self.hook("/product_stores/#{product_store_id}.xml", "product-store", nil, 'DELETE') + self.hook("/product_stores/#{product_store_id}.json", "product-store", nil, 'DELETE') end # # Merchants: Create a Product Store Product. # Product Store Product must be a hash of: @@ -521,44 +522,44 @@ def product_store_createitem(product_store_id, product) request = { 'product' => product } - self.hook("/product_stores/#{product_store_id}/createitem.xml", "product-store-data", request, 'POST') + self.hook("/product_stores/#{product_store_id}/createitem.json", "product-store-data", request, 'POST') end # Merchants: Update a product def product_store_updateitem(product_store_id, product_id, product) request = { 'product' => product, 'product_id' => product_id } - self.hook("/product_stores/#{product_store_id}/updateitem.xml", "product-store-data", request, 'PUT') + self.hook("/product_stores/#{product_store_id}/updateitem.json", "product-store-data", request, 'PUT') end # Merchants: Destroy a product def product_store_destroyitem(product_store_id, product_id) request = { 'pr_id' => product_id } - self.hook("/product_stores/#{product_store_id}/destroyitem.xml", "product-store-data", request, 'DELETE') + self.hook("/product_stores/#{product_store_id}/destroyitem.json", "product-store-data", request, 'DELETE') end # ===================== # Affiliate Ad Groups # ===================== # Affiliates: List Ad Groups def ad_groups_list - self.hook("/ad_groups.xml", "ad_group", nil, "GET") + self.hook("/ad_groups.json", "ad_group", nil, "GET") end # Affiliates: Display information about an Ad Group def ad_group_show(ad_group_id) - self.hook("/ad_groups/#{ad_group_id}.xml", "ad_group", nil, "GET") + self.hook("/ad_groups/#{ad_group_id}.json", "ad_group", nil, "GET") end # Affiliates: Add item to an Ad Group / Crate new Ad Group def ad_group_createitem(group_id, tool_type, tool_id, new_group = nil) request = { @@ -566,59 +567,59 @@ 'new_group' => new_group, 'tool_type' => tool_type, 'tool_id' => tool_id } - self.hook("/ad_groups/createitem.xml", "ad_group", request, 'POST') + self.hook("/ad_groups/createitem.json", "ad_group", request, 'POST') end # Affiliates: Destroy an Ad Group def ad_group_destroy(ad_group_id) - self.hook("/ad_groups/#{ad_group_id}.xml", "ad_group", nil, "DELETE") + self.hook("/ad_groups/#{ad_group_id}.json", "ad_group", nil, "DELETE") end # Affiliates: Delete an Tool from a Group. tooltype is one of 'txtlink', 'txtad' or 'banner'. def ad_group_destroyitem(ad_group_id, tool_type, tool_id) request = { 'tool_type' => tool_type, 'tool_id' => tool_id } - self.hook("/ad_groups/#{ad_group_id}/destroyitem.xml", "ad_group", request, "DELETE") + self.hook("/ad_groups/#{ad_group_id}/destroyitem.json", "ad_group", request, "DELETE") end #================= # Affiliate Feeds #================= # Affiliates: List Feeds def feeds_list() - self.hook("/feeds.xml", "feed", nil, "GET") + self.hook("/feeds.json", "feed", nil, "GET") end # Affiliates: Create a Feed def feed_create(feed) request = { 'feed' => feed } - self.hook("/feeds.xml", "feed", request, 'POST') + self.hook("/feeds.json", "feed", request, 'POST') end # Affiliates: Update a Feed def feed_update(feed_id, feed) request = { 'feed' => feed } - self.hook("/feeds/#{feed_id}.xml", "feed", request, 'PUT') + self.hook("/feeds/#{feed_id}.json", "feed", request, 'PUT') end # Affiliates: Destroy a Feed def feed_destroy(feed_id) - self.hook("/feeds/#{feed_id}.xml", "feed", nil, "DELETE") + self.hook("/feeds/#{feed_id}.json", "feed", nil, "DELETE") end # ========== # Messages # ========== @@ -628,88 +629,71 @@ request = { 'page' => page, 'perpage' => perpage } - self.hook("/messages.xml", "message", nil, "GET") + self.hook("/messages.json", "message", nil, "GET") end # List sent messages. Displays the first 6 entries by default. def sent_messages_list(page=1, perpage=6) request = { 'page' => page, 'perpage' => perpage } - self.hook("/messages/sent.xml", "message", nil, "GET") + self.hook("/messages/sent.json", "message", nil, "GET") end # Display information about a message def message_show(message_id) - self.hook("/messages/#{message_id}.xml", "message") + self.hook("/messages/#{message_id}.json", "message") end # Destroy a message def message_destroy(message_id) - self.hook("/messages/#{message_id}.xml", "message", nil, 'DELETE') + self.hook("/messages/#{message_id}.json", "message", nil, 'DELETE') end #======= # Hooks #======= # List Hooks def hooks_list(oauth_token_key = 'current') - hook("/oauth_clients/#{oauth_token_key}/hooks.xml", "hook", nil, 'GET') + self.hook("/oauth_clients/#{oauth_token_key}/hooks.json", "client_application_hook", nil, 'GET') end # Create a Hook - def hook_create(hook, oauth_token_key = 'current') + def hook_create(name, opts, oauth_token_key = 'current') request = { - 'hook' => hook + 'name' => name, + 'opts' => opts, } - hook("/oauth_clients/#{oauth_token_key}/hooks.xml", "hook", request, 'POST') + self.hook("/oauth_clients/#{oauth_token_key}/hooks.json", "client_application_hook", request, 'POST') end # Destroy a Hook def hook_destroy(hook_id, oauth_token_key = 'current') - hook("/oauth_clients/#{oauth_token_key}/hooks/#{hook_id}.xml", "hook", nil, 'DELETE') + self.hook("/oauth_clients/#{oauth_token_key}/hooks/#{hook_id}.json", "client_application_hook", nil, 'DELETE') end - def hook(path, expected, send = nil, method = 'GET') #:nodoc: - params = normalize_params(send, method) - + def hook(path, expected, send = nil, method = 'GET') #:nodoc: if self.oauth - result = self.oauth.send(method.downcase, "/#{version}#{path}", send, params) + result = self.oauth.send(method.downcase, "/#{version}#{path}", send) else - result = self.class.send(method.downcase, "/#{version}#{path}", :body => params) + result = self.class.send(method.downcase, "/#{version}#{path}", :body => send) + result = result.parsed_response end # scrap the container if result.respond_to? :values result.values.first else result end end - def normalize_params(params, method) - hash_to_xml(:request => params).to_s - end - - def hash_to_xml(var, document = nil) - document = REXML::Document.new if document.nil? - - if var.respond_to? :keys - var.keys.each do |key| - hash_to_xml(var[key], document.add_element(key.to_s)) - end - else - document.add_text(var.to_s) - end - - document - end end