Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed Url.static, Added Url.create #19

Merged
merged 4 commits into from

2 participants

Cody Marshall Johnston Sathya Sekaran
Cody Marshall Johnston

Hey Sathya, sorry the commit message sucks so badly, it's just a vanilla merge message because git commit isn't playing correctly with my command line vim =/

Sathya Sekaran
Owner

Can you please find a way to clean this up before I pull it in? I'll pair with you if you want. it's totally confusing that all of the changes are included within a merge commit.

When in doubt, simply make a topic branch based on my master branch so you have a clean copy to work with.

Cody Marshall Johnston

Thanks, dad.

Sathya Sekaran sfsekaran merged commit 606eb93 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2012
  1. Cody Marshall Johnston

    ignore rvmrc

    codegoblin authored
  2. Cody Marshall Johnston
  3. Cody Marshall Johnston

    update gemfile.lock

    codegoblin authored
  4. Cody Marshall Johnston

    Add Url.create, update Url.static

    codegoblin authored
    Add Url.create
    Refactor/reimplement Url.static
    Update README
    Clarify project spec
This page is out of date. Refresh to see the latest.
1  .gitignore
View
@@ -4,3 +4,4 @@ Gemfile.lock
pkg/*
.DS_Store
tags
+.rvmrc
21 Gemfile.lock
View
@@ -1,29 +1,29 @@
PATH
remote: .
specs:
- awesm (0.1.7)
+ awesm (0.1.8)
hashie
- httparty
+ httparty (= 0.8.1)
json
GEM
remote: http://rubygems.org/
specs:
- addressable (2.2.6)
- columnize (0.3.5)
+ addressable (2.2.8)
+ columnize (0.3.6)
crack (0.3.1)
diff-lcs (1.1.3)
hashie (1.2.0)
httparty (0.8.1)
multi_json
multi_xml
- json (1.6.3)
+ json (1.7.1)
linecache (0.46)
rbx-require-relative (> 0.0.4)
- multi_json (1.0.4)
- multi_xml (0.4.1)
+ multi_json (1.3.4)
+ multi_xml (0.5.1)
rake (0.9.2.2)
- rbx-require-relative (0.0.5)
+ rbx-require-relative (0.0.9)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
@@ -47,9 +47,8 @@ PLATFORMS
DEPENDENCIES
awesm!
hashie
- httparty
json
rake
- rspec
+ rspec (= 2.7.0)
ruby-debug
- webmock
+ webmock (= 1.7.8)
26 README.md
View
@@ -71,6 +71,27 @@ And in your code:
)
# => #<Awesm::Conversion account_conversionid=nil account_id="12" account_userid=nil awesm_url="awe.sm_5WXHo" clicker_id=nil converted_at=1323475432 href=nil id="bfdaddec-2298-43fb-9da0-f12d81febbf6" ip_address=nil language=nil redirection_id="94585739" referrer=nil session_id=nil type="goal_1" user_agent=nil value=1230>
+ # List projects
+ projects = Awesm::Project.list
+ projects.first.class # => Awesm::Project
+
+ # Create an awesm url
+ url = Awesm::Url.create(
+ :channel => 'twitter',
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
+ :tool => 'mKU7uN',
+ :url => 'http://developers.awe.sm/'
+ )
+ url.awesm_url # => "http://demo.awe.sm/ELZ"
+
+ # Create a static url
+ url = Awesm::Url.static(
+ :url => 'http://developers.awe.sm/',
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
+ :tool => 'mKU7uN'
+ )
+ url.awesm_url # => "http://demo.awe.sm/ELZ"
+
# Create a sharing link
Awesm::Url.share(
:url => 'http://developers.awe.sm/',
@@ -84,14 +105,15 @@ And in your code:
# => "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&parent=awe.sm_s5d99&user_id=42"
# Create a static link
- Awesm::Url.static(
+ url = Awesm::Url.static(
:format => 'json',
:url => 'http://developers.awe.sm/',
:key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
:tool => 'mKU7uN'
)
- # => "http://api.awe.sm/url/static.json?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN"
+ url.awesm_url # => "http://demo.awe.sm/ELZ"
+ awesm_url="http://demo.awe.sm/K5s"
# Retrieve stats in return for your hard work!
stats = Awesm::Stats.range(
:key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
11 awesm.gemspec
View
@@ -4,8 +4,8 @@ require "awesm/version"
Gem::Specification.new do |s|
s.name = "awesm"
s.version = Awesm::VERSION
- s.authors = ["Sathya Sekaran", "Michael Durnhofer"]
- s.email = ["sfsekaran@gmail.com", "mdurnhofer@gmail.com"]
+ s.authors = ["Sathya Sekaran", "Michael Durnhofer", "Cody Johnston"]
+ s.email = ["sfsekaran@gmail.com", "mdurnhofer@gmail.com", "cody@codegobl.in"]
s.homepage = "http://github.com/sfsekaran/awesm"
s.summary = %q{Totally awe.sm!}
s.description = %q{The 'awesm' gem is an interface for awe.sm (http://totally.awe.sm), a social link analytics tracking service.}
@@ -19,14 +19,13 @@ Gem::Specification.new do |s|
# specify any dependencies here;
s.add_development_dependency "rake"
- s.add_development_dependency "rspec"
- s.add_development_dependency "webmock"
- s.add_development_dependency "httparty"
+ s.add_development_dependency "rspec", "= 2.7.0"
+ s.add_development_dependency "webmock", "= 1.7.8"
s.add_development_dependency "json"
s.add_development_dependency "hashie"
s.add_development_dependency "ruby-debug"
- s.add_runtime_dependency "httparty"
+ s.add_runtime_dependency "httparty", "= 0.8.1"
s.add_runtime_dependency "json"
s.add_runtime_dependency "hashie"
end
29 lib/awesm/url.rb
View
@@ -1,7 +1,13 @@
module Awesm
- class Url
+ class Url < Hashie::Mash
+ include HTTParty
+ base_uri 'http://api.awe.sm/url'
REQUIRED_SHARE_PARAMS = [:url, :key, :tool, :channel, :destination].freeze
- REQUIRED_STATIC_PARAMS = [:format, :url, :key, :tool].freeze
+
+ # Right now this method only supports the json format
+ def self.create(params = {})
+ call_api('.json', params)
+ end
def self.share(params = {})
if required_params_present?(REQUIRED_SHARE_PARAMS, params)
@@ -14,15 +20,18 @@ def self.share(params = {})
end
end
+ # Right now this method only supports the json format
def self.static(params = {})
- if required_params_present?(REQUIRED_STATIC_PARAMS, params)
- options = params.clone
- options = options.delete_if{|key,value| REQUIRED_STATIC_PARAMS.include?(key) }
- query = options.map{|k,v| "#{k}=#{v}"}.join('&')
- static_url = "http://api.awe.sm/url/static.#{params[:format]}?v=3&url=#{params[:url]}&key=#{params[:key]}&tool=#{params[:tool]}"
- static_url += "&#{query}" if query.length > 0
- static_url
- end
+ call_api('/static.json', params)
+ end
+
+ #########
+ private #
+ #########
+
+ def self.call_api(action, params)
+ response = post(action, :query => { :v => 3 }.merge(params))
+ response.code >= 400 ? nil : new(response)
end
def self.required_params_present?(required_params, params)
2  spec/awesm/project_spec.rb
View
@@ -68,7 +68,7 @@
}.to_json
end
- context '.create' do
+ describe '.create' do
before do
stub_request(:post, "http://api.awe.sm/projects/new?json=%7B%22name%22:%22TotallyAwesomeProject%22%7D&subscription_key=sub-xxxxxx&application_key=app-xxxxxx").
to_return(:status => 200, :body => new_project_response, :headers => { 'Content-Type' => 'application/json;charset=utf-8' })
301 spec/awesm/url_spec.rb
View
@@ -1,6 +1,130 @@
require 'spec_helper'
describe Awesm::Url do
+
+ before do
+ Awesm.subscription_key = 'sub-xxxxxx'
+ Awesm.application_key = 'app-xxxxxx'
+ end
+
+ after do
+ Awesm.subscription_key = nil
+ Awesm.application_key = nil
+ end
+
+ let(:invalid_params) {
+ { :bad => "param" }
+ }
+
+ describe '.create' do
+ let(:api_url) { "http://api.awe.sm/url.json" }
+
+ let(:required_params) {
+ {
+ :url => 'http://developers.awe.sm/',
+ :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
+ :tool => 'mKU7uN',
+ :channel => 'twitter'
+ }
+ }
+
+ let(:create_url_response) {
+ {
+ :application => "test",
+ :awesm_id => "demo.awe.sm_K5s",
+ :awesm_url => "http://demo.awe.sm/K5s",
+ :campaign => nil,
+ :campaign_metadata => {
+ :description => nil,
+ :name => nil
+ },
+ :channel => "facebook-post",
+ :created_at => "2011-10-04T18:00:57Z",
+ :domain => "demo.awe.sm",
+ :notes => nil,
+ :original_url => "http://developers.awe.sm/",
+ :parent => nil,
+ :path => "K5s",
+ :redirect_url => "http://developers.awe.sm/",
+ :service => "facebook",
+ :service_postid => nil,
+ :service_postid_metadata => {
+ :reach => nil,
+ :shared_at => nil
+ },
+ :service_userid => nil,
+ :sharer_id => nil,
+ :tag => nil,
+ :tool => "test-main",
+ :user_id => nil,
+ :user_id_metadata => {
+ :icon_url => nil,
+ :profile_url => nil,
+ :username => nil
+ }
+ }.to_json
+ }
+
+ let(:create_url_error_response) do
+ {
+ "request" => {
+ "action" => "create",
+ "awesm_url" => "awe.sm_5WXHo",
+ "controller" => "url",
+ "key" => "badkeyabcdefghijklmnopqrstuvwxyz1234567890"
+ },
+ "error" => {
+ "code" => 1000,
+ "message" => "Invalid API key"
+ }
+ }.to_json
+ end
+
+ before do
+ expected_params = required_params.merge(:v => 3)
+ stub_request(:post, api_url).
+ with(:query => expected_params).
+ to_return(:status => 200,
+ :body => create_url_response,
+ :headers => {
+ 'Content-Type' => 'application/json;charset=utf-8'
+ })
+ end
+
+ context 'when an error occurs' do
+ before do
+ expected_params = invalid_params.merge(:v => 3)
+ stub_request(:post, api_url).
+ with(:query => expected_params).
+ to_return(:status => 400,
+ :body => create_url_error_response,
+ :headers => {
+ 'Content-Type' => 'application/json;charset=utf-8'
+ })
+ end
+
+ it 'returns nil' do
+ url = Awesm::Url.create(:bad => 'param')
+ url.should == nil
+ end
+ end
+
+ context 'when successful' do
+ it 'returns an Awesm::Url' do
+ url = Awesm::Url.create(required_params)
+ url.should be_an_instance_of(Awesm::Url)
+ end
+ end
+
+ it 'posts to the awe.sm project creation api properly' do
+ expected_query = required_params.merge(:v => 3)
+ Awesm::Url.create(required_params)
+ a_request(:post, "http://api.awe.sm/url.json").
+ with(:query => expected_query).
+ should have_been_made.once
+ end
+ end
+
describe '.share' do
let(:required_parameters) do
{
@@ -18,7 +142,7 @@
:tool => 'mKU7uN',
:channel => 'twitter',
:destination => 'http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm').
- should == 'http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm'
+ should == 'http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm'
end
it 'returns nil if it receives an empty hash' do
@@ -29,83 +153,140 @@
Awesm::Url.share.should == nil
end
- [[:campaign, 'my-campaign-1'],
- [:campaign_description, 'This is a description. Fun.'],
- [:campaign_name, 'My Campaign 1'],
- [:notes, 'This link is going to take down my site.'],
- [:parent, 'awe.sm_s5d99'],
- [:service_userid, 'twitter:13263'],
- [:tag, 'My-Tag'],
- [:user_id, '42']].each do |option, value|
- it "returns the correct awe.sm url when also passed a '#{option.to_s}' parameter" do
- Awesm::Url.share(required_parameters.merge(option => value)).
- should == "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&#{option.to_s}=#{value}"
- end
- end
-
- it "accepts user_id related parameters" do
- url = Awesm::Url.share(required_parameters.merge(
- :user_id => 42,
- :user_id_icon_url => 'http://test.com/users/42/avatar.png',
- :user_id_profile_url => 'http://test.com/users/42/',
- :user_id_username => 'johndoe@test.com'
- ))
- url.should == "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&user_id=42&user_id_icon_url=http://test.com/users/42/avatar.png&user_id_profile_url=http://test.com/users/42/&user_id_username=johndoe@test.com"
- end
+ [
+ [:campaign, 'my-campaign-1'],
+ [:campaign_description, 'This is a description. Fun.'],
+ [:campaign_name, 'My Campaign 1'],
+ [:notes, 'This link is going to take down my site.'],
+ [:parent, 'awe.sm_s5d99'],
+ [:service_userid, 'twitter:13263'],
+ [:tag, 'My-Tag'],
+ [:user_id, '42'],
+ [:user_id, '42'],
+ [:user_id_icon_url, 'http://test.com/users/42/avatar.png'],
+ [:user_id_profile_url, 'http://test.com/users/42/'],
+ [:user_id_username , 'johndoe@test.com']
+ ].each do |option, value|
+ it "returns the correct awe.sm url when also passed a '#{option.to_s}' parameter" do
+ Awesm::Url.share(required_parameters.merge(option => value)).
+ should == "http://api.awe.sm/url/share?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&channel=twitter&destination=http://twitter.com/intent/tweet?text=This+is+the+coolest+API+evar!%26url=AWESM_URL%26via=awesm&#{option.to_s}=#{value}"
+ end
+ end
end
describe '.static' do
- let(:required_parameters) do
+ let(:api_url) { "http://api.awe.sm/url/static.json" }
+
+ let(:valid_params) {
{
- :format => 'json',
:url => 'http://developers.awe.sm/',
:key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
- :tool => 'mKU7uN'
+ :tool => 'mKU7uN',
+ :channel => 'twitter',
+ :user_id => "23291",
+ :user_id_username => "jhstrauss",
+ :user_id_profile_url => "http://plancast.com/user/23291",
+ :user_id_icon_url => "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/41657_627286_9900_n.jpg"
}
- end
+ }
- it 'returns the correct static awe.sm url when passed the required parameters' do
- Awesm::Url.static(:format => 'json',
- :url => 'http://developers.awe.sm/',
- :key => '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9',
- :tool => 'mKU7uN').
- should == 'http://api.awe.sm/url/static.json?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN'
- end
+ let(:static_url_response) {
+ {
+ :awesm_url => "http://demo.awe.sm/ELZ",
+ :awesm_id => "demo.awe.sm_ELZ",
+ :domain => "demo.awe.sm",
+ :path => "ELZ",
+ :created_at => "2011-08-19T23:39:15Z",
+ :redirect_url => "http://plancast.com/p/6xfs/silicon-valley-tweetup-summer-2011?utm_campaign=&utm_medium=demo.awe.sm-copypaste&utm_source=direct-demo.awe.sm&utm_content=test-main",
+ :original_url => "http://plancast.com/p/6xfs/silicon-valley-tweetup-summer-2011",
+ :channel => "copypaste",
+ :service => "copypaste",
+ :tool => "test-main",
+ :application => "test",
+ :parent => nil,
+ :sharer_id => nil,
+ :username =>nil,
+ :service_userid => nil,
+ :service_postid => nil,
+ :service_postid_metadata => {
+ :reach => nil,
+ :shared_at => nil
+ },
+ :campaign => nil,
+ :campaign_metadata => {
+ :description => nil,
+ :name => nil
+ },
+ :user_id => "23291",
+ :user_id_metadata => {
+ :icon_url => "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/41657_627286_9900_n.jpg",
+ :username => "jhstrauss",
+ :profile_url => "http://plancast.com/user/23291"
+ },
+ :tag => nil,
+ :notes => nil,
+ :new => false
+ }.to_json
+ }
- it 'returns nil if it receives an empty hash' do
- Awesm::Url.static({}).should == nil
+ let(:static_url_error_response) do
+ {
+ "request" => {
+ "action" => "static",
+ "awesm_url" => "awe.sm_5WXHo",
+ "controller" => "url",
+ "key" => "badkeyabcdefghijklmnopqrstuvwxyz1234567890"
+ },
+ "error" => {
+ "code" => 1000,
+ "message" => "Invalid API key"
+ }
+ }.to_json
end
- it 'returns nil if it receives nothing' do
- Awesm::Url.static.should == nil
+ before do
+ expected_params = valid_params.merge(:v => 3)
+ stub_request(:post, api_url).
+ with(:query => expected_params).
+ to_return(:status => 200,
+ :body => static_url_response,
+ :headers => {
+ 'Content-Type' => 'application/json;charset=utf-8'
+ })
end
- [[:channel, 'twitter'],
- [:campaign, 'my-campaign-1'],
- [:campaign_description, 'This is a description. Fun.'],
- [:campaign_name, 'My Campaign 1'],
- [:notes, 'This link is going to take down my site.'],
- [:parent, 'awe.sm_s5d99'],
- [:service_userid, 'twitter:13263'],
- [:tag, 'My-Tag'],
- [:user_id, '42']].each do |option, value|
- it "returns the correct static awe.sm url when also passed a '#{option.to_s}' parameter" do
- Awesm::Url.static(required_parameters.merge(option => value)).
- should == "http://api.awe.sm/url/static.json?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&#{option.to_s}=#{value}"
+ context 'when an error occurs' do
+ before do
+ expected_params = invalid_params.merge(:v => 3)
+ stub_request(:post, api_url).
+ with(:query => expected_params).
+ to_return(:status => 400,
+ :body => static_url_error_response,
+ :headers => {
+ 'Content-Type' => 'application/json;charset=utf-8'
+ })
+ end
+
+ it 'returns nil' do
+ url = Awesm::Url.static(:bad => 'param')
+ url.should == nil
end
end
- it "accepts user_id related parameters" do
- url = Awesm::Url.static(required_parameters.merge(
- :user_id => 42,
- :user_id_icon_url => 'http://test.com/users/42/avatar.png',
- :user_id_profile_url => 'http://test.com/users/42/',
- :user_id_username => 'johndoe@test.com'
- ))
- url.should == "http://api.awe.sm/url/static.json?v=3&url=http://developers.awe.sm/&key=5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9&tool=mKU7uN&user_id=42&user_id_icon_url=http://test.com/users/42/avatar.png&user_id_profile_url=http://test.com/users/42/&user_id_username=johndoe@test.com"
+ context 'when successful' do
+ it 'returns an Awesm::Url' do
+ url = Awesm::Url.static(valid_params)
+ url.should be_an_instance_of(Awesm::Url)
+ end
end
+ it 'posts to the awe.sm project creation api properly' do
+ expected_query = valid_params.merge(:v => 3)
+ Awesm::Url.static(valid_params)
+ a_request(:post, "http://api.awe.sm/url/static.json").
+ with(:query => expected_query).
+ should have_been_made.once
+ end
end
end
-
Something went wrong with that request. Please try again.