Ver código fonte

Merge pull request #46 from Shopify/article-endpoint-update

Allow acces to articles.json without a blog ID
Lydia Krupp-Hunter 12 anos atrás
pai
commit
359e740372

+ 1 - 1
Rakefile

@@ -25,7 +25,7 @@ end
 
 task :default => :test
 
-require 'rake/rdoctask'
+require 'rdoc/task'
 Rake::RDocTask.new do |rdoc|
   if File.exist?('VERSION.yml')
     config = YAML.load(File.read('VERSION.yml'))

+ 6 - 1
lib/shopify_api/resources/article.rb

@@ -2,9 +2,14 @@ module ShopifyAPI
   class Article < Base
     include Events
     include Metafields
+    include DisablePrefixCheck
 
     self.prefix = "/admin/blogs/:blog_id/"
-    
+     
+    def self.prefix(options={})
+      options[:blog_id].nil? ? "/admin/" : "/admin/blogs/#{options[:blog_id]}/"
+    end
+
     def comments
       Comment.find(:all, :params => { :article_id => id })
     end

+ 28 - 0
test/article_test.rb

@@ -0,0 +1,28 @@
+require 'test_helper'
+
+class ArticleTest < Test::Unit::TestCase
+
+  def test_get_article
+    fake "articles/6242736", :method => :get, :body => load_fixture('article')
+    article = ShopifyAPI::Article.find(6242736)
+    assert_equal "First Post", article.title
+  end
+
+  def test_get_articles
+    fake "articles", :method => :get, :body => load_fixture('articles')
+    articles = ShopifyAPI::Article.all
+    assert_equal 3, articles.length
+  end
+
+  def test_get_articles_namespaced
+    fake "blogs/1008414260/articles", :method => :get, :body => load_fixture('articles')
+    articles = ShopifyAPI::Article.find(:all, :params => {:blog_id => 1008414260})
+    assert_equal 3, articles.length
+  end
+
+  def test_get_article_namespaced
+    fake "blogs/1008414260/articles/6242736", :method => :get, :body => load_fixture('article')
+    article = ShopifyAPI::Article.find(6242736, :params => {:blog_id => 1008414260})
+    assert_equal "First Post", article.title
+  end
+end

+ 2 - 2
test/blog_test.rb

@@ -1,8 +1,8 @@
 require 'test_helper'
 class BlogTest < Test::Unit::TestCase
   test "blog creation" do
-    fake "blogs", :method => :post, :status => 202
-    ShopifyAPI::Blog.create(:title => "Test Blog")
+    fake "blogs", :method => :post, :status => 202, :body => load_fixture('blog')
+    blog = ShopifyAPI::Blog.create(:title => "Test Blog")
     assert_equal '{"blog":{"title":"Test Blog"}}', FakeWeb.last_request.body
   end
 end

+ 15 - 0
test/fixtures/article.json

@@ -0,0 +1,15 @@
+{
+  "article": {
+    "author": "Shopify",
+    "blog_id": 1008414260,
+    "body_html": null,
+    "created_at": "2012-07-06T13:57:28-04:00",
+    "id": 6242736,
+    "published_at": "2012-07-06T13:57:28-04:00",
+    "summary_html": null,
+    "title": "First Post",
+    "updated_at": "2012-07-06T13:57:51-04:00",
+    "user_id": null,
+    "tags": "consequuntur, cupiditate, repellendus"
+  }
+}

+ 39 - 0
test/fixtures/articles.json

@@ -0,0 +1,39 @@
+{
+  "articles": [{
+    "author": "Shopify",
+    "blog_id": 1008414260,
+    "body_html": null,
+    "created_at": "2012-07-06T13:57:28-04:00",
+    "id": 6242736,
+    "published_at": "2012-07-06T13:57:28-04:00",
+    "summary_html": null,
+    "title": "First Post",
+    "updated_at": "2012-07-06T13:57:51-04:00",
+    "user_id": null,
+    "tags": "consequuntur, cupiditate, repellendus"
+  }, {
+    "author": "development shop",
+    "blog_id": 1008414260,
+    "body_html": null,
+    "created_at": "2013-04-21T18:10:35-04:00",
+    "id": 7739673,
+    "published_at": "2013-04-21T18:10:22-04:00",
+    "summary_html": null,
+    "title": "My second blog post",
+    "updated_at": "2013-04-21T18:10:35-04:00",
+    "user_id": 2221540,
+    "tags": ""
+  }, {
+    "author": "development shop",
+    "blog_id": 1008414260,
+    "body_html": null,
+    "created_at": "2013-04-21T18:11:19-04:00",
+    "id": 7739683,
+    "published_at": "2013-04-21T18:10:45-04:00",
+    "summary_html": null,
+    "title": "50% off sale",
+    "updated_at": "2013-04-21T18:11:19-04:00",
+    "user_id": 2221540,
+    "tags": ""
+  }]
+}

+ 13 - 0
test/fixtures/blog.json

@@ -0,0 +1,13 @@
+{
+  "blog": {
+    "handle": "test-blog",
+    "created_at": "2012-01-10T17:45:19-05:00",
+    "title": "Test Blog",
+    "template_suffix": null,
+    "updated_at": "2012-01-10T17:45:19-05:00",
+    "feedburner_location": null,
+    "id": 1008414260,
+    "feedburner": null,
+    "commentable": "no"
+  }
+}

+ 2 - 2
test/fixtures/blogs.json

@@ -1,5 +1,5 @@
 {
-  "blog": {
+  "blogs": [{
     "handle": "test-blog",
     "created_at": "2012-01-10T17:45:19-05:00",
     "title": "Test Blog",
@@ -9,5 +9,5 @@
     "id": 1008414260,
     "feedburner": null,
     "commentable": "no"
-  }
+  }]
 }