pixallent-fetchapi-ruby 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ v.0.2.2 Order support
2
+
3
+ v.0.2.1 Prelim rework
4
+
5
+ v.0.2 Functionality for Downloads and Account
6
+
7
+ v.0.1 Functionality for Orders and Items
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) <year> <copyright holders>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,18 @@
1
+ CHANGELOG
2
+ fetchapi-ruby.gemspec
3
+ init.rb
4
+ lib/fetchapi/account.rb
5
+ lib/fetchapi/base.rb
6
+ lib/fetchapi/download.rb
7
+ lib/fetchapi/item.rb
8
+ lib/fetchapi/order.rb
9
+ lib/fetchapi-ruby.rb
10
+ LICENSE
11
+ Manifest
12
+ Rakefile
13
+ README
14
+ spec/account_spec.rb
15
+ spec/base_spec.rb
16
+ spec/download_spec.rb
17
+ spec/item_spec.rb
18
+ spec/order_spec.rb
data/README ADDED
@@ -0,0 +1,9 @@
1
+ == Installation
2
+
3
+ gem install pixallent-fetchapi-ruby
4
+
5
+ == Usage
6
+
7
+ FetchAPI::Base.basic_auth('youraccount.fetchapp.com', 'demokey', 'demotoken')
8
+ FetchAPI::Item.find("ABC0001")
9
+ FetchAPI::Order.create(:id => "1015", :title => "Test Order", :first_name => "Donald", :last_name => "Duck", :email => "donald@duck.com", :order_items => [{:sku => 'ABC0001'}, {:sku => 'ABC0002}])
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('fetchapi-ruby', '0.2.2') do |p|
6
+ p.description = "Integrate your site with http://fetchapp.com for seamless digital delivery."
7
+ p.url = "http://github.com/pixallent/fetchapi-ruby"
8
+ p.author = ["Thomas Reynolds, Michael Larkin"]
9
+ p.email = "mikelarkin@pixallent.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ p.runtime_dependencies = ["httparty"]
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{fetchapi-ruby}
5
+ s.version = "0.2.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Thomas Reynolds, Michael Larkin"]
9
+ s.date = %q{2009-05-01}
10
+ s.description = %q{Integrate your site with http://fetchapp.com for seamless digital delivery.}
11
+ s.email = %q{mikelarkin@pixallent.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/fetchapi/account.rb", "lib/fetchapi/base.rb", "lib/fetchapi/download.rb", "lib/fetchapi/item.rb", "lib/fetchapi/order.rb", "lib/fetchapi-ruby.rb", "LICENSE", "README"]
13
+ s.files = ["CHANGELOG", "fetchapi-ruby.gemspec", "init.rb", "lib/fetchapi/account.rb", "lib/fetchapi/base.rb", "lib/fetchapi/download.rb", "lib/fetchapi/item.rb", "lib/fetchapi/order.rb", "lib/fetchapi-ruby.rb", "LICENSE", "Manifest", "Rakefile", "README", "spec/account_spec.rb", "spec/base_spec.rb", "spec/download_spec.rb", "spec/item_spec.rb", "spec/order_spec.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/pixallent/fetchapi-ruby}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Fetchapi-ruby", "--main", "README"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{fetchapi-ruby}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Integrate your site with http://fetchapp.com for seamless digital delivery.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<httparty>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<httparty>, [">= 0"])
33
+ end
34
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'fetchapi-ruby'
@@ -0,0 +1,20 @@
1
+ module FetchAPI
2
+ class Account < FetchAPI::Base
3
+ #--
4
+ ################ Class Methods ###############
5
+ #--
6
+
7
+ # Retrieves information about the Account
8
+ def self.details
9
+ execute(:get, "/account")
10
+ end
11
+
12
+ # Generates a new API token. Subsequent API calls using the
13
+ # existing token will be refused.
14
+ def self.new_token
15
+ execute(:get, "/new_token")
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,78 @@
1
+ module FetchAPI
2
+ class Base
3
+
4
+ def initialize(id_or_attributes)
5
+ case id_or_attributes
6
+ when Integer, String
7
+ @attributes = get("/#{self.class.pluralized_class_name}/#{id_or_attributes.to_s}")
8
+ @attributes = @attributes[self.class.singularized_class_name]
9
+ @id = @attributes['id']
10
+ when Hash
11
+ @attributes = id_or_attributes
12
+ @id = id_or_attributes['id']
13
+ end
14
+ end
15
+
16
+ def self.find(selector, params={})
17
+ case selector
18
+ when :all
19
+ execute(:get, "/#{pluralized_class_name}")[pluralized_class_name].map { |data| new(data) }
20
+ when Integer, String
21
+ new(selector)
22
+ end
23
+ end
24
+
25
+ class Connector
26
+ include HTTParty
27
+ format :xml
28
+ end
29
+
30
+ # Initializes the connection
31
+ def self.basic_auth(url, key, token)
32
+ Connector.base_uri(url)
33
+ Connector.basic_auth(key, token)
34
+ end
35
+
36
+ protected
37
+ def self.singularized_class_name
38
+ ancestors.first.to_s.split('::').last.downcase
39
+ end
40
+ def self.pluralized_class_name
41
+ ancestors.first.to_s.split('::').last.downcase << 's'
42
+ end
43
+
44
+ def post(*args); self.class.execute(:post, *args); end
45
+ def get(*args); self.class.execute(:get, *args); end
46
+ def delete(*args); self.class.execute(:delete, *args); end
47
+ def put(*args); self.class.execute(:put, *args); end
48
+
49
+ # Do HTTP request, handle errors
50
+ def self.execute(action, path, options = {})
51
+ handle_response(Connector.send(action, path, :query => options))
52
+ #Connector.send(action, path, options)
53
+ end
54
+
55
+ def self.handle_response(response)
56
+ case response.code
57
+ when 100..199 then response
58
+ when 200..299 then response
59
+ when 300.399 then
60
+ raise(response.messsage)
61
+ when 400..499 then
62
+ raise(response.message)
63
+ when 500..599 then
64
+ raise(response.message)
65
+ else
66
+ raise("Unknown Response")
67
+ end
68
+
69
+ end
70
+
71
+
72
+ # Access attributes as class methods of the Item object
73
+ def method_missing(method)
74
+ return super unless attributes.has_key?(method.to_s)
75
+ attributes[method.to_s]
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ module FetchAPI
2
+ class Download < FetchAPI::Base
3
+ attr_accessor :id, :attributes
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ module FetchAPI
2
+ class Item < FetchAPI::Base
3
+ attr_accessor :attributes
4
+
5
+ def initialize(id_or_attributes)
6
+ case id_or_attributes
7
+ when Integer, String
8
+ @attributes = get("/#{self.class.pluralized_class_name}/#{id_or_attributes.to_s}")
9
+ @attributes = @attributes[self.class.singularized_class_name]
10
+ when Hash
11
+ @attributes = id_or_attributes
12
+ end
13
+ end
14
+
15
+ #--
16
+ ################ Class Methods ###############
17
+ #--
18
+ def self.create(options)
19
+ execute(:post, "/items/create", :item => options)
20
+ end
21
+
22
+ #--
23
+ ################# Instance Methods ###############
24
+ #--
25
+
26
+ def destroy
27
+ delete("/items/#{sku}/delete")
28
+ end
29
+
30
+ def update(options)
31
+ put("/items/#{sku}", :item => options)
32
+ end
33
+
34
+
35
+ end
36
+ end
@@ -0,0 +1,60 @@
1
+ module FetchAPI
2
+
3
+ class Order < FetchAPI::Base
4
+ attr_accessor :id, :attributes
5
+
6
+ #--
7
+ ################ Class Methods ###############
8
+ #--
9
+
10
+ def self.find(selector, params={})
11
+ case selector
12
+ when :current
13
+ params.merge!(:filter => "current")
14
+ execute(:get, "/#{pluralized_class_name}?#{params.to_params}")[pluralized_class_name].map { |data| new(data) }
15
+ when :manual
16
+ params.merge!(:filter => "manual")
17
+ execute(:get, "/#{pluralized_class_name}?#{params.to_params}")[pluralized_class_name].map { |data| new(data) }
18
+ when :expired
19
+ params.merge!(:filter => "expired")
20
+ execute(:get, "/#{pluralized_class_name}?#{params.to_params}")[pluralized_class_name].map { |data| new(data) }
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ # Creates a new order
27
+ def self.create(options={})
28
+ execute(:post, "/orders/create", :order => options)
29
+ end
30
+
31
+ #--
32
+ ################# Instance Methods ###############
33
+ #--
34
+
35
+ # Permanently deletes the order
36
+ def destroy
37
+ delete("/orders/#{id}/delete")
38
+ end
39
+
40
+ # Sets the expiration date to Time.now, stopping future downloads
41
+ def expire
42
+ post("/orders/#{id}/expire")
43
+ end
44
+
45
+ # Delivers the email containing the download instructions.
46
+ # Optional params:
47
+ # reset_expiration (true/false): Reset the order's expiration. Defaults to true.
48
+ # expiration_date (2009-04-30T15:03:46+00:00): Manually sets the order's expiration date
49
+ def send_email(options={})
50
+ post("/orders/#{id}/send_email", options)
51
+ end
52
+
53
+ # Immediately updates the order with the new parameters
54
+ def update(options={})
55
+ put("/orders/#{id}", :order => options)
56
+ end
57
+
58
+
59
+ end
60
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'net/http'
4
+
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'fetchapi/base'
7
+ require 'fetchapi/account'
8
+ require 'fetchapi/download'
9
+ require 'fetchapi/item'
10
+ require 'fetchapi/order'
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "fetchapi-ruby")
2
+ include FetchAPI
3
+
4
+ describe Account do
5
+ before do
6
+ Base.basic_auth('http://pixallent.myhost.dev:3000/api', 'pixallent', 'pixallent')
7
+ end
8
+
9
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "fetchapi-ruby")
2
+ include FetchAPI
3
+
4
+ describe Base do
5
+
6
+ before do
7
+ Base.basic_auth('http://pixallent.myhost.dev:3000/api', 'pixallent', 'pixallent')
8
+ end
9
+
10
+ it "should catch invalid login" do
11
+ Base.basic_auth('http://pixallent.myhost.dev:3000/api', 'bad', 'login')
12
+ lambda {Account.details}.should raise_error("Unauthorized")
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "fetchapi-ruby")
2
+ include FetchAPI
3
+
4
+
5
+ describe Download do
6
+ before do
7
+ Base.basic_auth('http://pixallent.myhost.dev:3000/api', 'pixallent', 'pixallent')
8
+ end
9
+
10
+
11
+ it "should find by id" do
12
+ Download.find(37).id.to_s.should == "37"
13
+ end
14
+
15
+
16
+ it "should find all" do
17
+ d = Download.find(:all)
18
+ d.class.should == Array
19
+ end
20
+ end
data/spec/item_spec.rb ADDED
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "fetchapi-ruby")
2
+ include FetchAPI
3
+
4
+ describe Item do
5
+ before do
6
+ Base.basic_auth('http://pixallent.myhost.dev:3000/api', 'pixallent', 'pixallent')
7
+ end
8
+
9
+
10
+ it "should find by id" do
11
+ i = Item.find('CATALOG').sku.should == "CATALOG"
12
+ end
13
+
14
+ it "should find all" do
15
+ Item.find(:all).length.should > 0
16
+ end
17
+ end
@@ -0,0 +1,50 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "fetchapi-ruby")
2
+ include FetchAPI
3
+
4
+ describe Order do
5
+ before do
6
+ Base.basic_auth('http://pixallent.myhost.dev:3000/api', 'pixallent', 'pixallent')
7
+ end
8
+
9
+ it "should update title" do
10
+ order = Order.find('1001')
11
+ order.update(:first_name => "Daffy")
12
+ order = Order.find('1001')
13
+ order.first_name.should == "Daffy"
14
+ end
15
+
16
+ it "should create an order" do
17
+ begin
18
+ Order.find('1015').destroy
19
+ rescue
20
+ end
21
+ Order.create(:id => "1015", :title => "Test Order", :first_name => "Donald", :last_name => "Duck", :email => "donald@duck.com", :order_items => [{:sku => 'BIZCARD'}, {:sku => "CATALOG"}])
22
+ order = Order.find('1015')
23
+ order.id.to_s.should == "1015"
24
+ end
25
+
26
+ it "should delete an order" do
27
+ Order.create(:id => "1016", :title => "Test Order", :first_name => "Donald", :last_name => "Duck", :email => "donald@duck.com", :download_limit => "4")
28
+ order = Order.find('1016')
29
+ order.destroy
30
+ lambda {Order.find('1016')}.should raise_error("Not Found")
31
+ end
32
+
33
+ it "should find all" do
34
+ Order.find(:all).length.should > 0
35
+ end
36
+
37
+ it "should find current" do
38
+ Order.find(:current).length.should > 0
39
+ end
40
+
41
+ it "should find manual" do
42
+ Order.find(:manual).length.should > 0
43
+ end
44
+
45
+ it "should find expired" do
46
+ Order.find(:expired).length.should > 0
47
+ end
48
+
49
+
50
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pixallent-fetchapi-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Reynolds, Michael Larkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Integrate your site with http://fetchapp.com for seamless digital delivery.
26
+ email: mikelarkin@pixallent.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - CHANGELOG
33
+ - lib/fetchapi/account.rb
34
+ - lib/fetchapi/base.rb
35
+ - lib/fetchapi/download.rb
36
+ - lib/fetchapi/item.rb
37
+ - lib/fetchapi/order.rb
38
+ - lib/fetchapi-ruby.rb
39
+ - LICENSE
40
+ - README
41
+ files:
42
+ - CHANGELOG
43
+ - fetchapi-ruby.gemspec
44
+ - init.rb
45
+ - lib/fetchapi/account.rb
46
+ - lib/fetchapi/base.rb
47
+ - lib/fetchapi/download.rb
48
+ - lib/fetchapi/item.rb
49
+ - lib/fetchapi/order.rb
50
+ - lib/fetchapi-ruby.rb
51
+ - LICENSE
52
+ - Manifest
53
+ - Rakefile
54
+ - README
55
+ - spec/account_spec.rb
56
+ - spec/base_spec.rb
57
+ - spec/download_spec.rb
58
+ - spec/item_spec.rb
59
+ - spec/order_spec.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/pixallent/fetchapi-ruby
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --line-numbers
65
+ - --inline-source
66
+ - --title
67
+ - Fetchapi-ruby
68
+ - --main
69
+ - README
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "1.2"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project: fetchapi-ruby
87
+ rubygems_version: 1.2.0
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: Integrate your site with http://fetchapp.com for seamless digital delivery.
91
+ test_files: []
92
+