Permalink
Browse files

Add Url.create, update Url.static

Add Url.create
Refactor/reimplement Url.static
Update README
Clarify project spec
  • Loading branch information...
1 parent 68f68cd commit 8c73e20e2aeed5f0227f5a83e210dff682e99851 @codegoblin committed May 11, 2012
Showing with 285 additions and 73 deletions.
  1. +24 −2 README.md
  2. +19 −10 lib/awesm/url.rb
  3. +1 −1 spec/awesm/project_spec.rb
  4. +241 −60 spec/awesm/url_spec.rb
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',
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)
@@ -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' })
Oops, something went wrong.

0 comments on commit 8c73e20

Please sign in to comment.