Permalink
Browse files

Add update live pic API call; changes to primary API interface class …

…name; tests and docs
  • Loading branch information...
briandoll committed Sep 17, 2011
1 parent 93f1a8a commit 5153f1ee8be802a45241530040843d5b690f1cdf
Showing with 114 additions and 22 deletions.
  1. +18 −3 README.md
  2. +10 −0 TODO.md
  3. +38 −4 lib/movableink-api/api.rb
  4. +21 −1 lib/movableink-api/real-time.rb
  5. +4 −2 lib/movableink-api/wrappers.rb
  6. +23 −12 test/test_real_time.rb
View
@@ -16,17 +16,32 @@ Put movableink-api in your Gemfile:
gem 'movableink-api'
+
+Include the Module:
+
+ include MovableInk
+
+
**Real-Time Content:**
- MovableInk::API.live_pics
+ MovableInkClient.live_pics
# => [#<LivePic collection_id=nil, ...>, ...]
- MovableInk::API.live_pic '123'
+ MovableInkClient.live_pic '123'
# => #<LivePic id=123, ...>
- MovableInk::API.live_pic_stats '123'
+ MovableInkClient.live_pic_stats '123'
# => #<LivePicStats id=123, ...>
+ lp = LivePic.new
+ lp.name = "My Name"
+ ...
+ new_live_pic = MovableInkClient.create_live_pic lp
+ # => #<LivePic id=123, ...>
+
+ MovableInkClient.update_live_pic
+
+
**Creative Optimizers**
View
10 TODO.md
@@ -9,6 +9,7 @@ TODO
- I currently like that all API methods hang together off the API class, as you don't have to
think too hard about which methods are part of which API component (real-time, creative, etc.)
But could be convinced otherwise
+ - The test suite could certainly be better, especially with regard to account limits, etc.
**Real-Time:**
@@ -29,3 +30,12 @@ TODO
**Note:**
- [API documentation](https://movableink.com/api/docs)
+
+
+**MovableInk Feedback:**
+
+ PUT http://movableink.com/api/v1/live_pics/[live_pic_id]
+
+ The API docs say: Modifies a live pic's properties and returns the modified live pic.
+ Currently this is not so. The response body is empty JSON, but does update the live pic.
+
View
@@ -1,5 +1,8 @@
module MovableInk
- class API
+
+ class MovableInkError < StandardError; end
+
+ class MovableInkClient
# This library supports v1 of the MovableInk API
NAMESPACE = "http://movableink.com/api/v1/"
@@ -40,7 +43,7 @@ class << self
def self.get(api_path, params = nil)
url = build_url(api_path, params)
response = Net::HTTP.get_response url
- Crack::JSON.parse response.body
+ process_response response
end
# POST a specific MovableInk API call with parameters
@@ -58,7 +61,27 @@ def self.post(api_path, params = nil)
url = build_url(api_path)
params["token"] = token # POST params overwrite params in URI
response = Net::HTTP.post_form url, params
- Crack::JSON.parse response.body
+ process_response response
+ end
+
+ # PUT a specific MovableInk API call with parameters
+ #
+ # api_path - The API method to invoke
+ # params - A Hash containing any parameters to be sent with this request
+ #
+ # Examples
+ #
+ # put('live_pics', {:name => 'some name', ...})
+ # # => {:live_pic => {:name => 'some name', ...}}
+ #
+ # Returns a Hash representation of the JSON response body
+ def self.put(api_path, params = nil)
+ url = build_url(api_path)
+ request = Net::HTTP::Put.new(url.path)
+ params["token"] = token # PUT params overwrite params in URI
+ request.set_form_data params
+ response = Net::HTTP.new(url.host, url.port).start {|http| http.request(request) }
+ process_response response
end
# Build a URI object reprsenting an API URL based on the parameters provided
@@ -87,10 +110,21 @@ def self.build_url(api_path, params = nil)
# Currently only checks to see that the MovableInk::API.token is set
#
# Returns nothing.
- # Returns ArgumentError if the MovableInk::API.token is empty
+ # Raises ArgumentError if the MovableInk::API.token is empty
def self.validate_request
raise ArgumentError.new "Please set your MovableInk API token before any calls to the API. ex. MovableInk::API.token(yourtoken)" if token.nil?
end
+ # Convert response body to JSON if the response is not an error
+ #
+ # Returns a JSON representation of the response body
+ # Raises MovableInkError if there is an error in the response
+ def self.process_response(response)
+ resp = Crack::JSON.parse response.body
+ err = resp["errors"]
+ raise MovableInkError.new "Error: #{resp["message"]} - #{err["base"]}" if err
+ resp
+ end
+
end
end
@@ -13,7 +13,8 @@ module RealTime
# Returns an array of LivePic instances
# Returns ArgumentError if MovableInk::API.token is empty
def live_pics(offset = nil, limit = nil)
- LivePics.new(get("live_pics", {:offset => offset, :limit => limit}))
+ pics = LivePics.new(get("live_pics", {:offset => offset, :limit => limit}))
+ pics.live_pics
end
@@ -67,4 +68,23 @@ def create_live_pic(live_pic)
LivePic.new response["live_pic"]
end
+ # Public: Update a live pic using specified parameters
+ #
+ # live_pic - A LivePic instance with properties set according to our needs
+ #
+ # Examples
+ #
+ # lp = MovableInk::API.live_pics[0]
+ # lp.name = "Updated Name"
+ # MovableInk::API.update_live_pic(lp)
+ # # => #<LivePic name='Updated Name', id='123' ...>
+ #
+ # Returns a LivePic instance that represents the newly udpated live pic
+ # Returns ArgumentError if MovableInk::API.token is empty
+ def update_live_pic(live_pic)
+ params = live_pic.to_params
+ response = put("live_pics/#{live_pic.id}", params)
+ LivePic.new response["live_pic"]
+ end
+
end
@@ -127,8 +127,10 @@ class LivePics
def initialize(response)
lp = []
- response["live_pics"].each do |live_pic|
- lp << LivePic.new(live_pic)
+ if pics = response["live_pics"]
+ pics.each do |live_pic|
+ lp << LivePic.new(live_pic)
+ end
end
self.live_pics = lp
end
View
@@ -3,35 +3,35 @@
class TestRealTime < Test::Unit::TestCase
+ include MovableInk
+
def test_unathorized_access
- MovableInk::API.token = nil
+ MovableInkClient.token = nil
assert_raise ArgumentError do
- response = MovableInk::API.live_pics
+ response = MovableInkClient.live_pics
end
end
def test_live_pics
- MovableInk::API.token = ::API_KEY
- response = MovableInk::API.live_pics
- assert response.kind_of?(LivePics)
- live_pics = response.live_pics
+ MovableInkClient.token = ::API_KEY
+ live_pics = MovableInkClient.live_pics
assert !live_pics.nil?
assert live_pics.kind_of?(Array)
assert live_pics.first.kind_of?(LivePic)
assert !live_pics.first.url.nil?
end
def test_live_pic
- MovableInk::API.token = ::API_KEY
- response = MovableInk::API.live_pic ::LIVE_PIC_ID
+ MovableInkClient.token = ::API_KEY
+ response = MovableInkClient.live_pic ::LIVE_PIC_ID
assert !response.nil?
assert response.kind_of?(LivePic)
assert !response.url.nil?
end
def test_live_pic_stats
- MovableInk::API.token = ::API_KEY
- response = MovableInk::API.live_pic_stats ::LIVE_PIC_ID
+ MovableInkClient.token = ::API_KEY
+ response = MovableInkClient.live_pic_stats ::LIVE_PIC_ID
assert !response.nil?
assert response.kind_of?(LivePicStats)
assert !response.impressions.nil?
@@ -47,7 +47,7 @@ def test_live_pic_wrapper
end
def test_create_live_pic
- MovableInk::API.token = ::API_KEY
+ MovableInkClient.token = ::API_KEY
lp = LivePic.new
lp.name = "My Name"
lp.height = "10"
@@ -56,11 +56,22 @@ def test_create_live_pic
lp.x_offset = "20"
lp.y_offset = "20"
lp.target_url = 'http://google.com/'
- new_live_pic = MovableInk::API.create_live_pic lp
+ new_live_pic = MovableInkClient.create_live_pic lp
assert new_live_pic.kind_of?(LivePic)
assert new_live_pic.name, "My Name"
assert new_live_pic.height, "10"
end
+ def test_update_live_pic
+ MovableInkClient.token = ::API_KEY
+ lp = MovableInkClient.live_pics[0]
+ updated_pic_id = lp.id
+ name = "Updated Name - #{Time.now.to_i}"
+ lp.name = name
+ MovableInkClient.update_live_pic lp
+ updated_pic = MovableInkClient.live_pic updated_pic_id
+ assert updated_pic.name, name
+ end
+
end

0 comments on commit 5153f1e

Please sign in to comment.