pixallent-fetchapi-ruby 0.9 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +49 -5
- data/Rakefile +1 -1
- data/fetchapi-ruby.gemspec +1 -1
- data/lib/fetchapi/account.rb +9 -3
- data/lib/fetchapi/base.rb +15 -8
- data/lib/fetchapi/item.rb +11 -3
- data/lib/fetchapi/order.rb +26 -5
- metadata +1 -1
data/README
CHANGED
@@ -1,9 +1,53 @@
|
|
1
1
|
== Installation
|
2
|
-
|
2
|
+
gem sources -a http://gems.github.com (you only have to do this once)
|
3
3
|
gem install pixallent-fetchapi-ruby
|
4
|
+
|
5
|
+
= Setup
|
6
|
+
FetchAPI::Base.basic_auth('youraccount.fetchapp.com', 'demokey', 'demotoken')
|
7
|
+
|
8
|
+
= Account
|
4
9
|
|
5
|
-
|
10
|
+
FetchAPI::Account.details
|
11
|
+
|
12
|
+
token = FetchAPI::Account.new_token *Subsequent calls will use the new token automatically
|
6
13
|
|
7
|
-
|
8
|
-
|
9
|
-
FetchAPI::
|
14
|
+
= Downloads
|
15
|
+
|
16
|
+
downloads = FetchAPI::Download.find(:all, :per_page => 50, :page => 2)
|
17
|
+
|
18
|
+
download = FetchAPI::Download.find(1)
|
19
|
+
|
20
|
+
|
21
|
+
= Items
|
22
|
+
|
23
|
+
items = FetchAPI::Item.find(:all, :per_page => 10, :page => 3)
|
24
|
+
|
25
|
+
item = FetchAPI::Item.find("CJ0001")
|
26
|
+
|
27
|
+
item = FetchAPI::Item.create(:sku => "CJ0001", :name => "Carrot Juice")
|
28
|
+
|
29
|
+
item.update(:name => "Tomato Juice")
|
30
|
+
|
31
|
+
item.destroy
|
32
|
+
|
33
|
+
downloads = item.downloads *Returns an array of FetchAPI::Downloads for this item
|
34
|
+
|
35
|
+
= Orders
|
36
|
+
|
37
|
+
all_orders = FetchAPI::Order.find(:all)
|
38
|
+
|
39
|
+
current_orders = FetchAPI::Order.find(:current, :page => 3)
|
40
|
+
|
41
|
+
manual_orders = FetchAPI::Order.find(:manual, :per_page => 10)
|
42
|
+
|
43
|
+
expired_orders = FetchAPI::Order.find(:expired, :per_page => 10, :page => 3)
|
44
|
+
|
45
|
+
order = FetchAPI::Order.find("1001")
|
46
|
+
|
47
|
+
order = FetchAPI::Order.create(:id => "1015", :title => "Test Order", :first_name => "Donald", :last_name => "Duck", :email => "donald@duck.com", :order_items => [{:sku => 'ABC0001'}, {:sku => 'ABC0002}])
|
48
|
+
|
49
|
+
order.update(:first_name => "Daffy")
|
50
|
+
|
51
|
+
order.destroy
|
52
|
+
|
53
|
+
downloads = order.downloads *Returns an array of FetchAPI::Downloads for this order
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('fetchapi-ruby', '0.9') do |p|
|
5
|
+
Echoe.new('fetchapi-ruby', '0.9.1') do |p|
|
6
6
|
p.description = "Integrate your site with http://fetchapp.com for seamless digital delivery."
|
7
7
|
p.url = "http://github.com/pixallent/fetchapi-ruby"
|
8
8
|
p.author = ["Thomas Reynolds, Michael Larkin"]
|
data/fetchapi-ruby.gemspec
CHANGED
data/lib/fetchapi/account.rb
CHANGED
@@ -2,17 +2,23 @@ module FetchAPI
|
|
2
2
|
class Account < FetchAPI::Base
|
3
3
|
#--
|
4
4
|
################ Class Methods ###############
|
5
|
-
#--
|
5
|
+
#--
|
6
6
|
|
7
7
|
# Retrieves information about the Account
|
8
8
|
def self.details
|
9
|
-
execute(:get, "/account")
|
9
|
+
new(execute(:get, "/account"))
|
10
10
|
end
|
11
11
|
|
12
12
|
# Generates a new API token. Subsequent API calls using the
|
13
13
|
# existing token will be refused.
|
14
14
|
def self.new_token
|
15
|
-
execute(:get, "/new_token")
|
15
|
+
token = execute(:get, "/new_token")
|
16
|
+
unless token["message"].blank?
|
17
|
+
# Reauthorize
|
18
|
+
puts "XXXXXXXX #{token["message"]}"
|
19
|
+
Connector.basic_auth(@key, token["message"])
|
20
|
+
token
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
end
|
data/lib/fetchapi/base.rb
CHANGED
@@ -1,22 +1,28 @@
|
|
1
1
|
module FetchAPI
|
2
2
|
class Base
|
3
|
-
|
4
|
-
|
3
|
+
|
4
|
+
def initialize(id_or_attributes)
|
5
5
|
case id_or_attributes
|
6
6
|
when Integer, String
|
7
7
|
@attributes = get("/#{self.class.pluralized_class_name}/#{id_or_attributes.to_s}")
|
8
|
-
|
9
|
-
|
8
|
+
@attributes = @attributes[self.class.singularized_class_name]
|
9
|
+
@id = @attributes['id']
|
10
10
|
when Hash
|
11
11
|
@attributes = id_or_attributes
|
12
|
-
|
12
|
+
@id = id_or_attributes['id']
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.find(selector, params={})
|
17
17
|
case selector
|
18
18
|
when :all
|
19
|
-
execute(:get, "/#{pluralized_class_name}")
|
19
|
+
objects = execute(:get, "/#{pluralized_class_name}")
|
20
|
+
|
21
|
+
if objects[pluralized_class_name].blank?
|
22
|
+
return []
|
23
|
+
else
|
24
|
+
objects[pluralized_class_name].map { |data| new(data) }
|
25
|
+
end
|
20
26
|
when Integer, String
|
21
27
|
new(selector)
|
22
28
|
end
|
@@ -29,6 +35,7 @@ module FetchAPI
|
|
29
35
|
|
30
36
|
# Initializes the connection
|
31
37
|
def self.basic_auth(url, key, token)
|
38
|
+
@key = key # Save this in case they generate a new token
|
32
39
|
Connector.base_uri(url)
|
33
40
|
Connector.basic_auth(key, token)
|
34
41
|
end
|
@@ -60,8 +67,8 @@ module FetchAPI
|
|
60
67
|
raise(response.messsage)
|
61
68
|
when 400..499 then
|
62
69
|
raise(response.message)
|
63
|
-
when 500..599 then
|
64
|
-
|
70
|
+
when 500..599 then
|
71
|
+
raise(response.message)
|
65
72
|
else
|
66
73
|
raise("Unknown Response")
|
67
74
|
end
|
data/lib/fetchapi/item.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module FetchAPI
|
2
2
|
class Item < FetchAPI::Base
|
3
|
-
attr_accessor :attributes
|
3
|
+
attr_accessor :id, :attributes
|
4
4
|
|
5
5
|
def initialize(id_or_attributes)
|
6
6
|
case id_or_attributes
|
@@ -16,7 +16,7 @@ module FetchAPI
|
|
16
16
|
################ Class Methods ###############
|
17
17
|
#--
|
18
18
|
def self.create(options)
|
19
|
-
execute(:post, "/items/create", :item => options)
|
19
|
+
return FetchAPI::Item.new(execute(:post, "/items/create", :item => options)["item"])
|
20
20
|
end
|
21
21
|
|
22
22
|
#--
|
@@ -28,9 +28,17 @@ module FetchAPI
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def update(options)
|
31
|
-
put("/items/#{sku}", :item => options)
|
31
|
+
self.attributes = put("/items/#{sku}", :item => options)["item"]
|
32
|
+
self.id = self.sku
|
32
33
|
end
|
33
34
|
|
35
|
+
def downloads
|
36
|
+
downloads = get("/items/#{sku}/downloads")
|
37
|
+
if downloads
|
38
|
+
downloads["downloads"].map { |data| FetchAPI::Download.new(data) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
34
42
|
|
35
43
|
end
|
36
44
|
end
|
data/lib/fetchapi/order.rb
CHANGED
@@ -11,13 +11,28 @@ module FetchAPI
|
|
11
11
|
case selector
|
12
12
|
when :current
|
13
13
|
params.merge!(:filter => "current")
|
14
|
-
execute(:get, "
|
14
|
+
orders = execute(:get, "/orders?#{params.to_params}")
|
15
|
+
if orders["orders"].blank?
|
16
|
+
return []
|
17
|
+
else
|
18
|
+
orders["orders"].map { |data| new(data) }
|
19
|
+
end
|
15
20
|
when :manual
|
16
21
|
params.merge!(:filter => "manual")
|
17
|
-
execute(:get, "
|
22
|
+
orders = execute(:get, "/orders?#{params.to_params}")
|
23
|
+
if orders["orders"].blank?
|
24
|
+
return []
|
25
|
+
else
|
26
|
+
orders["orders"].map { |data| new(data) }
|
27
|
+
end
|
18
28
|
when :expired
|
19
29
|
params.merge!(:filter => "expired")
|
20
|
-
execute(:get, "
|
30
|
+
orders = execute(:get, "/orders?#{params.to_params}")
|
31
|
+
if orders["orders"].blank?
|
32
|
+
return []
|
33
|
+
else
|
34
|
+
orders["orders"].map { |data| new(data) }
|
35
|
+
end
|
21
36
|
else
|
22
37
|
super
|
23
38
|
end
|
@@ -25,7 +40,7 @@ module FetchAPI
|
|
25
40
|
|
26
41
|
# Creates a new order
|
27
42
|
def self.create(options={})
|
28
|
-
execute(:post, "/orders/create", :order => options)
|
43
|
+
return FetchAPI::Order.new(execute(:post, "/orders/create", :order => options)["order"])
|
29
44
|
end
|
30
45
|
|
31
46
|
#--
|
@@ -52,9 +67,15 @@ module FetchAPI
|
|
52
67
|
|
53
68
|
# Immediately updates the order with the new parameters
|
54
69
|
def update(options={})
|
55
|
-
put("/orders/#{id}", :order => options)
|
70
|
+
self.attributes = put("/orders/#{id}", :order => options)["order"]
|
56
71
|
end
|
57
72
|
|
73
|
+
def downloads(params={})
|
74
|
+
downloads = get("/orders/#{id}/downloads")
|
75
|
+
if downloads
|
76
|
+
downloads["downloads"].map { |data| FetchAPI::Download.new(data) }
|
77
|
+
end
|
78
|
+
end
|
58
79
|
|
59
80
|
end
|
60
81
|
end
|