james-ruminant 0.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/moo.rb +28 -4
- data/spec/moo_spec.rb +41 -0
- data/spec/spec_helper.rb +0 -1
- metadata +3 -12
- data/spec/all.rb +0 -4
data/lib/moo.rb
CHANGED
@@ -3,6 +3,10 @@ require 'rubygems'
|
|
3
3
|
require 'builder'
|
4
4
|
require 'design'
|
5
5
|
|
6
|
+
require 'net/http'
|
7
|
+
require 'uri'
|
8
|
+
require 'hpricot'
|
9
|
+
|
6
10
|
module Moo
|
7
11
|
class Order < OptionsStruct.create(:api_key, :designs)
|
8
12
|
def api_version
|
@@ -20,14 +24,34 @@ module Moo
|
|
20
24
|
@designs.first.product_type if @designs.first
|
21
25
|
end
|
22
26
|
|
23
|
-
def to_xml(xml=
|
27
|
+
def to_xml(xml=nil)
|
28
|
+
xml = Builder::XmlMarkup.new(:target => (@xml_string ||= "")) unless xml
|
24
29
|
eval File.open(File.expand_path(File.dirname(__FILE__) + "/template.xml.builder")).read
|
25
|
-
xml
|
30
|
+
xml.target!
|
26
31
|
end
|
27
32
|
|
33
|
+
attr_accessor :raw_response, :error_string, :error_code, :session_id, :start_url
|
28
34
|
def submit
|
29
|
-
|
30
|
-
|
35
|
+
res = Net::HTTP.post_form(URI.parse('http://www.moo.com/api/api.php'),
|
36
|
+
{'xml'=>self.to_xml, 'method'=>'direct'})
|
37
|
+
@raw_response = res.body
|
38
|
+
process_response
|
39
|
+
!error?
|
40
|
+
end
|
41
|
+
|
42
|
+
def process_response
|
43
|
+
@response = Hpricot(raw_response)
|
44
|
+
if error?
|
45
|
+
@error_string = @response.search('error_string').inner_html
|
46
|
+
@error_code = @response.search('error_code').inner_html
|
47
|
+
else
|
48
|
+
@session_id = @response.search('session_id').inner_html
|
49
|
+
@start_url = @response.search('start_url').inner_html
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def error?
|
54
|
+
@error ||= (@response.search('error').inner_html == "true")
|
31
55
|
end
|
32
56
|
end
|
33
57
|
|
data/spec/moo_spec.rb
CHANGED
@@ -41,4 +41,45 @@ describe "a moo order with a minicard" do
|
|
41
41
|
it "should include the xml for the minicard" do
|
42
42
|
@moo.to_xml.should include(@minicard.to_xml)
|
43
43
|
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "processing an XML response for failure" do
|
47
|
+
before :each do
|
48
|
+
@moo = Moo::Order.new :api_key => "TESTAPIKEY"
|
49
|
+
@moo.stub!(:raw_response).and_return('<?xml version="1.0" encoding="UTF-8"?> <moo> <response> <call>build</call> <error>true</error> </response> <payload> <error_code>1</error_code> <error_string>Error importing XML document</error_string> </payload> </moo>')
|
50
|
+
@moo.process_response
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should set error? as true" do
|
54
|
+
@moo.error?.should be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should set error_string correctly" do
|
58
|
+
@moo.error_string.should == "Error importing XML document"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should set error_code correctly" do
|
62
|
+
@moo.error_code.should == "1"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
describe "processing an XML response for success" do
|
68
|
+
before :each do
|
69
|
+
@moo = Moo::Order.new :api_key => "TESTAPIKEY"
|
70
|
+
@moo.stub!(:raw_response).and_return('<?xml version="1.0" encoding="UTF-8"?> <moo> <response> <call>build</call> <api_key>3ddbe3c0-17ac-c0a802d2-47bafd19-2473</api_key> </response> <payload> <session_id>d4c7a6d179fca910d67221a57a1bf966</session_id> <start_url>http://www.moo.com/make/designback.php?bid=1&pid=115&SID=d4c7a6d179fca910d67221a57a1bf966</start_url> </payload> </moo>')
|
71
|
+
@moo.process_response
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should set error? as false" do
|
75
|
+
@moo.error?.should be_false
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should set start_url correctly" do
|
79
|
+
@moo.start_url.should == "http://www.moo.com/make/designback.php?bid=1&pid=115&SID=d4c7a6d179fca910d67221a57a1bf966"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should set session_id correctly" do
|
83
|
+
@moo.session_id.should == "d4c7a6d179fca910d67221a57a1bf966"
|
84
|
+
end
|
44
85
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: james-ruminant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Darling
|
@@ -12,22 +12,13 @@ date: 2008-07-24 00:00:00 -07:00
|
|
12
12
|
default_executable:
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: hpricot builder
|
16
16
|
version_requirement:
|
17
17
|
version_requirements: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
22
|
-
version:
|
23
|
-
- !ruby/object:Gem::Dependency
|
24
|
-
name: hoe
|
25
|
-
version_requirement:
|
26
|
-
version_requirements: !ruby/object:Gem::Requirement
|
27
|
-
requirements:
|
28
|
-
- - ">="
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: 1.1.7
|
21
|
+
version: "0"
|
31
22
|
version:
|
32
23
|
description: A ruby library to regurgitate MOO products
|
33
24
|
email: james@abscond.org
|
data/spec/all.rb
DELETED