Permalink
Browse files

Implemented offer query

  • Loading branch information...
1 parent ed649c8 commit 0cbb17293824ee86fd6f84dd19271fa139c814e6 @kazjote committed Jan 17, 2012
View
@@ -1,7 +1,8 @@
-source "http://rubygems.org"
+source 'http://rubygems.org'
gem 'bundler'
gem 'goliath'
+gem 'rest-client'
group :test do
gem 'em-http-request', :git => 'https://github.com/igrigorik/em-http-request.git'
View
@@ -38,13 +38,16 @@ GEM
rack (>= 1.0.0)
url_mount (~> 0.2.1)
log4r (1.1.10)
+ mime-types (1.17.2)
multi_json (1.0.4)
rack (1.4.0)
rack-accept-media-types (0.9)
rack-contrib (1.1.0)
rack (>= 0.9.1)
rack-respond_to (0.9.8)
rack-accept-media-types (>= 0.6)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
rspec (2.8.0)
rspec-core (~> 2.8.0)
rspec-expectations (~> 2.8.0)
@@ -63,4 +66,5 @@ DEPENDENCIES
bundler
em-http-request!
goliath
+ rest-client
rspec
View
@@ -1,10 +1,11 @@
#!/usr/bin/env ruby
require 'goliath'
-require 'mobile_offer'
+require 'challenge'
class Challenge < Goliath::API
def response(env)
- [200, {}, "ok"]
+ response = OfferQuery.new(1, 2, 3).fetch
+ [200, {}, response]
end
end
View
@@ -1,2 +1,3 @@
require 'challenge/offer_query.rb'
require 'challenge/offer.rb'
+require 'challenge/http_wrapper.rb'
@@ -1,5 +1,9 @@
+require 'em-synchrony'
+require 'em-synchrony/em-http'
+
module HttpWrapper
def self.request(params)
-
+ uri = "http://api.sponsorpay.com/feed/v1/offers.json"
+ EventMachine::HttpRequest.new(uri).get query: params
end
end
@@ -1,3 +1,2 @@
-class Offer
-
+class Offer < Struct.new(:title, :payout, :thumbnail)
end
@@ -1,3 +1,5 @@
+require 'json'
+
class OfferQuery
attr_accessor :uid, :pub0, :page
@@ -17,7 +19,17 @@ def initialize(uid, pub0, page)
@page = page
end
- def fetch
- HttpWrapper.request DEFAULT_PARAMS.merge(uid: uid, pub0: pub0, page: page)
+ def fetch(http_wrapper = HttpWrapper)
+ params = DEFAULT_PARAMS.merge(uid: uid, pub0: pub0, page: page)
+ response = http_wrapper.request params
+ if response && response.response_header.status == 200
+ received_data = JSON.parse response.body
+ if received_data["code"] == "OK"
+ received_data["offers"].map do |offer_data|
+ Offer.new offer_data["title"], offer_data["payout"], offer_data["thumbnail"]
+ end
+ end
+ end
end
end
+
View
@@ -1,10 +0,0 @@
-class MobileOffer
-
- attr_accessor :uid, :pub0, :page
-
- def initialize(uid, pub0, page)
- @uid = uid
- @pub0 = pub0
- @page = page
- end
-end
@@ -0,0 +1 @@
+
@@ -1,27 +1,43 @@
require 'spec_helper'
+require 'json'
+
describe OfferQuery do
- module HttpWrapper
- def self.requests
- @@requests
+ module HttpMock
+ def self.set_response new_response
+ @@response = new_response
+ end
+
+ def self.request_params
+ @@request_params
end
def self.reset
- @@requests = []
+ @@request_params = nil
+ @@response = nil
end
def self.request(params)
- @@requests << params
+ @@request_params = params
+ @@response
+ end
+ end
+
+ class ResponseMock < Struct.new(:code, :body)
+ ResponseHeaderMock = Struct.new(:status)
+
+ def response_header
+ ResponseHeaderMock.new code
end
end
- before(:each) { HttpWrapper.reset }
+ before(:each) { HttpMock.reset }
describe "params passed to SponsorPay during query" do
- before { OfferQuery.new(1, 2, 3).fetch }
+ before { OfferQuery.new(1, 2, 3).fetch HttpMock }
- let(:params) { HttpWrapper.requests.first }
+ let(:params) { HttpMock.request_params }
specify { params[:uid].should == 1 }
specify { params[:pub0].should == 2 }
@@ -32,5 +48,43 @@ def self.request(params)
specify { params[:offer_types].should == "112" }
specify { params[:device_id].should == "2b6f0cc904d137be2 e1730235f5664094b 831186" }
end
+
+ context "when there are some results" do
+ let(:offers_data) do
+ Array.new(3) do |i|
+ { title: "Offer #{i} title",
+ payout: i,
+ thumbnail: {"lowres" => "#{i}.jpg", "hires" => "#{i}.jpg" } }
+ end
+ end
+
+ let(:fetched_offers) { OfferQuery.new(1, 2, 3).fetch HttpMock }
+
+ before do
+ response_body = {offers: offers_data, code: "OK"}.to_json
+ HttpMock.set_response ResponseMock.new 200, response_body
+ end
+
+ it "should return correct number of offers" do
+ fetched_offers.size.should == 3
+ end
+
+ describe "fetched offers" do
+ it "should have correct title" do
+ fetched_offers[0].title.should == offers_data[0][:title]
+ fetched_offers[2].title.should == offers_data[2][:title]
+ end
+
+ it "should have correct payout" do
+ fetched_offers[0].payout.should == offers_data[0][:payout]
+ fetched_offers[2].payout.should == offers_data[2][:payout]
+ end
+
+ it "should have correct thumbnail" do
+ fetched_offers[0].thumbnail.should == offers_data[0][:thumbnail]
+ fetched_offers[2].thumbnail.should == offers_data[2][:thumbnail]
+ end
+ end
+ end
end

0 comments on commit 0cbb172

Please sign in to comment.