Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first round of hacking

  • Loading branch information...
commit 265850c638cea0676a9b641ce098ed09ac3aea03 0 parents
@briandoll authored
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in movableink-api.gemspec
+gemspec
4 History.md
@@ -0,0 +1,4 @@
+Changes
+================
+
+Unreleased
53 README.md
@@ -0,0 +1,53 @@
+movableink-api
+====================
+
+[Movable Ink](http://movableink.com/) provides "Real-time Content for Email". This ruby gem provides a convenient wrapper to access the [Movable Ink API](http://movableink.com/features/api).
+
+Installation
+------------------
+
+ gem install movableink-api
+
+
+Usage
+--------------------
+
+Put movableink-api in your Gemfile:
+
+ gem 'movableink-api'
+
+**Real-Time Content:**
+
+ MovableInk::API.live_pics
+ # => [#<LivePic collection_id=nil, ...>, ...]
+
+ MovableInk::API.live_pic '123'
+ # => #<LivePic id=123, ...>
+
+ MovableInk::API.live_pic_stats '123'
+ # => #<LivePicStats id=123, ...>
+
+
+**Creative Optimizers**
+
+ Not implemented yet
+
+
+**Live Tweets**
+
+ Not implemented yet
+
+
+Notes
+--------------------
+
+See History.md for a list of changes and contributors
+
+See TODO.md for a list of pending features and known issues
+
+_In order to run the tests, add your API token to tests/helper.rb_
+
+Author
+--------------------
+
+Brian Doll, [@briandoll](https://twitter.com/briandoll), [emphaticsolutions.com](http://emphaticsolutions.com)
12 Rakefile
@@ -0,0 +1,12 @@
+require 'bundler'
+include Rake::DSL
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/test_*.rb'
+end
+
+task :default => [:test]
31 TODO.md
@@ -0,0 +1,31 @@
+TODO
+===============
+
+**General:**
+
+ - See note in movableink-api.rb regarding Crack
+ - Sanity-check arguments to API calls.
+ Currently params are passed blindly through where only integers are valid, for example.
+ - 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
+
+**Real-Time:**
+
+ - Support for POST http://movableink.com/api/v1/live_pics
+ - Support for PUT http://movableink.com/api/v1/live_pics/[live_pic_id]
+ - Support for DELETE http://movableink.com/api/v1/live_pics/[live_pic_id]
+
+
+**Creative Optimizers:**
+
+ - Support for all calls
+
+
+**Live Tweets:**
+
+ - Support for all calls
+
+**Note:**
+
+ - [API documentation](https://movableink.com/api/docs)
11 lib/movableink-api.rb
@@ -0,0 +1,11 @@
+require 'net/http'
+require 'crack'
+require 'movableink-api/wrappers'
+require 'movableink-api/real-time'
+require 'movableink-api/creative-optimizers'
+require 'movableink-api/live-tweets'
+require 'movableink-api/api'
+
+# Crack can throw this error, which it does not define.
+# TODO: Patch Crack and send pull request
+class Crack::JSON::ParseError < StandardError; end
78 lib/movableink-api/api.rb
@@ -0,0 +1,78 @@
+module MovableInk
+ class API
+
+ # This library supports v1 of the MovableInk API
+ NAMESPACE = "http://movableink.com/api/v1/"
+
+ class << self
+
+ # Set the MovableInk API token
+ #
+ # token - The string value of the API token
+ #
+ # Returns nothing.
+ attr_writer :token
+
+ # Public: Get the MovableInk API token
+ #
+ # Returns the String value of the API token
+ attr_reader :token
+
+ end
+
+ extend RealTime
+ extend CreativeOptimizers
+ extend LiveTweets
+
+ private
+
+ # GET 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
+ #
+ # get('live_pics', {:offset => 40})
+ # # => {:live_pics => [{:collection_id => nil, ...}, ...]}
+ #
+ # Returns a Hash representation of the JSON response body
+ def self.get(api_path, params = nil)
+ url = build_url(api_path, params)
+ response = Net::HTTP.get_response url
+ Crack::JSON.parse response.body
+ end
+
+ # Build a URI object reprsenting an API URL based on the parameters provided
+ #
+ # api_path - The API method to invoke
+ # params - A Hash containing any parameters to be sent with this request
+ #
+ # Examples
+ #
+ # build_url('live_pics')
+ # # => #<URI::HTTP:0x101afe1a8 URL:http://movableink.com/api/v1/live_pics?token=xxxxx>
+ #
+ # Returns a URI object representing the URL for an API call
+ # Returns ArgumentError if the MovableInk::API.token is empty
+ def self.build_url(api_path, params = nil)
+ validate_request
+ url = URI.parse(NAMESPACE + api_path)
+ query = "token=#{token}"
+ params.each{|k,v| query << "&#{k.to_s}=#{v.to_s}" if v} if params
+ url.query = query
+ url
+ end
+
+ # Validate that all required parameters are set for a given API call
+ #
+ # Currently only checks to see that the MovableInk::API.token is set
+ #
+ # Returns nothing.
+ # Returns 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
+
+ end
+end
2  lib/movableink-api/creative-optimizers.rb
@@ -0,0 +1,2 @@
+module CreativeOptimizers
+end
2  lib/movableink-api/live-tweets.rb
@@ -0,0 +1,2 @@
+module LiveTweets
+end
52 lib/movableink-api/real-time.rb
@@ -0,0 +1,52 @@
+module RealTime
+
+ # Public: Get a list of the live pics
+ #
+ # offset - The number of items to skip
+ # limit - The maximum number of items to return. Capped at 100.
+ #
+ # Examples
+ #
+ # MovableInk::API.live_pics
+ # # => [#<LivePic collection_id=nil, ...>, ...]
+ #
+ # 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}))
+ end
+
+
+ # Public: Get a single live pic by ID
+ #
+ # live_pic_id - The ID of the live pic to get
+ #
+ # Examples
+ #
+ # MovableInk::API.live_pic '123'
+ # # => #<LivePic id=123, ...>
+ #
+ # Returns a LivePic instance
+ # Returns ArgumentError if MovableInk::API.token is empty
+ def live_pic(live_pic_id)
+ response = get("live_pics/#{live_pic_id}")
+ LivePic.new response["live_pic"]
+ end
+
+ # Public: Get stats for a single live pic by ID
+ #
+ # live_pic_id - The ID of the live pic to get stats for
+ #
+ # Examples
+ #
+ # MovableInk::API.live_pic_stats '123'
+ # # => #<LivePicStats id=123, ...>
+ #
+ # Returns a LivePicStats instance
+ # Returns ArgumentError if MovableInk::API.token is empty
+ def live_pic_stats(live_pic_id)
+ response = get("live_pics/#{live_pic_id}/stats")
+ LivePicStats.new response["live_pic"]["stats"]
+ end
+
+end
5 lib/movableink-api/version.rb
@@ -0,0 +1,5 @@
+module Movableink
+ module Api
+ VERSION = "0.0.0"
+ end
+end
25 lib/movableink-api/wrappers.rb
@@ -0,0 +1,25 @@
+require 'ostruct'
+
+# Class representing a single LivePic with member access to all properties
+#
+class LivePic < OpenStruct; end
+
+# Class containing an array of LivePic elements
+#
+class LivePics
+
+ attr_accessor :live_pics
+
+ def initialize(response)
+ lp = []
+ response["live_pics"].each do |live_pic|
+ lp << LivePic.new(live_pic)
+ end
+ self.live_pics = lp
+ end
+
+end
+
+# Class representing the stats for a LivePic instance
+#
+class LivePicStats < OpenStruct; end
20 movableink-api.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "movableink-api/version"
+
+Gem::Specification.new do |s|
+ s.name = "movableink-api"
+ s.version = Movableink::Api::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Brian Doll"]
+ s.email = ["brian@emphaticsolutions.com"]
+ s.homepage = "http://emphaticsolutions.com/"
+ s.summary = %q{Ruby library to provide easy integration with the movableink.com API}
+ s.description = %q{Ruby library to provide easy integration with the movableink.com API}
+ s.add_dependency('crack', '>= 0.1.8')
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
7 test/helper.rb
@@ -0,0 +1,7 @@
+require 'rubygems'
+require 'test/unit'
+require 'movableink-api'
+
+# Set these values in order to run the tests
+::API_KEY = nil
+::LIVE_PIC_ID = nil
8 test/test_creative_optimizers.rb
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'helper'
+
+class TestCreativeOptimizers < Test::Unit::TestCase
+ def test_foo
+ assert true
+ end
+end
8 test/test_live_tweets.rb
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'helper'
+
+class TestLiveTweets < Test::Unit::TestCase
+ def test_foo
+ assert true
+ end
+end
40 test/test_real_time.rb
@@ -0,0 +1,40 @@
+require 'rubygems'
+require 'helper'
+
+class TestRealTime < Test::Unit::TestCase
+
+ def test_unathorized_access
+ MovableInk::API.token = nil
+ assert_raise ArgumentError do
+ response = MovableInk::API.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
+ 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
+ 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
+ assert !response.nil?
+ assert response.kind_of?(LivePicStats)
+ assert !response.impressions.nil?
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.