0

I'm extracting a subset of fields from a full JSON dataset having a JSON fixture. The better way I could think of is the following :

require "spec_helper"

  # API ref.: GET /repos/:owner/:repo
  # http://developer.github.com/v3/repos/

  describe Elasticrepo::RepoSubset do

    context "extract a subset of repository fields" do
      let(:parsed) { Yajl::Parser.parse(fixture("repository.json").read) }
      subject { Elasticrepo::RepoSubset.new(parsed) }

      context "#id" do
        its(:id) { should eq(2126244) }
      end
      context "#owner" do
        its(:owner) { should eq("twitter") }
      end
      context "#name" do
        its(:name) { should eq("bootstrap") }
      end
      context "#url" do
        its(:url) { should eq("https://api.github.com/repos/twitter/bootstrap") }
      end
      context "#description" do
        its(:description) { should eq("Sleek, intuitive, and powerful front-end framework for faster and easier web development.") }
      end
      context "#created_at" do
        its(:created_at) { should eq("2011-07-29T21:19:00Z") }
      end
      context "#pushed_at" do 
        its(:pushed_at) { should eq("2013-04-13T03:56:36Z") }
      end
      context "#organization" do
        its(:organization) { should eq("Organization") }
      end
      context "#full_name" do
        its(:full_name) { should eq("twitter/bootstrap") }
      end
      context "#language" do
        its(:language) { should eq("JavaScript") }
      end
      context "#updated_at" do
        its(:updated_at) { should eq("2013-04-13T19:12:09Z") }
      end
    end
  end

but I wonder if is there a better, smarter, cleaner or just more elegant way of doing that. The class I TDD out is this :

module Elasticrepo
  class RepoSubset
    attr_reader :id, :owner, :name, :url, :description, :created_at, :pushed_at,
                :organization, :full_name, :language, :updated_at
    def initialize(attributes)
      @id = attributes["id"]
      @owner = attributes["owner"]["login"]
      @name = attributes["name"]
      @url = attributes["url"]
      @description = attributes["description"]
      @created_at = attributes["created_at"]
      @pushed_at = attributes["pushed_at"]
      @organization = attributes["owner"]["type"]
      @full_name = attributes["full_name"]
      @language = attributes["language"]
      @updated_at = attributes["updated_at"]
    end
  end
end
4

2 回答 2

2

我会删除各个上下文块。他们不添加任何额外的信息。

于 2013-04-14T11:36:31.447 回答
1

我会使用键/值映射并进行迭代,或者创建一个具有正确值的对象并比较整个对象。

于 2013-04-14T11:39:47.740 回答