{"id":13807712,"url":"https://github.com/crystal-china/hashr","last_synced_at":"2026-05-15T13:34:25.522Z","repository":{"id":147696192,"uuid":"525289625","full_name":"crystal-china/hashr","owner":"crystal-china","description":"Hashr is a tiny library makes test on JSON response easier,  and can also be used as a models object.","archived":false,"fork":false,"pushed_at":"2023-08-02T11:45:47.000Z","size":44,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-18T19:32:28.104Z","etag":null,"topics":["crystal","rspec","spec","testing"],"latest_commit_sha":null,"homepage":"","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crystal-china.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-08-16T08:18:23.000Z","updated_at":"2023-07-28T15:51:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"ec7aa0e1-46a9-4589-b316-ab6a897734cd","html_url":"https://github.com/crystal-china/hashr","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crystal-china%2Fhashr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crystal-china%2Fhashr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crystal-china%2Fhashr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crystal-china%2Fhashr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crystal-china","download_url":"https://codeload.github.com/crystal-china/hashr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246009720,"owners_count":20709007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crystal","rspec","spec","testing"],"created_at":"2024-08-04T01:01:29.296Z","updated_at":"2026-05-15T13:34:20.489Z","avatar_url":"https://github.com/crystal-china.png","language":"Crystal","funding_links":[],"categories":["Testing"],"sub_categories":[],"readme":"# hashr\n\nHashr is a tiny class makes test on JSON response easier.\n\nThe name of `hashr` come from the awesome ruby gem [hashr](https://github.com/svenfuchs/hashr), though, AFAIK, use original code is not possible because Crystal very different with Ruby in some aspect.\n\nThis shard should only be used in spec.\n\n## Installation\n\n1. Add the dependency to your `shard.yml`:\n\n   ```yaml\n   dependencies:\n     hashr:\n       github: crystal-china/hashr\n   ```\n\n2. Run `shards install`\n\n## Usage\n\nFollowing is a example:\n\n```crystal\nrequire \"./src/hashr\"\nrequire \"spec\"\n\ndescribe \"daily reports\" do\n  it \"query daily report\" do\n    json_string = \"{\\\"data\\\":{\\\"reportQuery\\\":{\\\"target\\\":{\\\"targetTotalCount\\\":47,\\\"processedTotalCount\\\":44,\\\"qualifiedTotalCount\\\":40}}}}\"\n\n    response_hash = Hash(String, JSON::Any).from_json(json_string)\n    # =\u003e Get a hash like this:\n    # {\"data\" =\u003e\n    #  {\"reportQuery\" =\u003e\n    #   {\"target\" =\u003e\n    #    {\"targetTotalCount\" =\u003e 47,\n    #     \"processedTotalCount\" =\u003e 44,\n    #     \"qualifiedTotalCount\" =\u003e 40}}}} (Hash(String, JSON::Any))\n\n    # Instead, verify on the entire response result, we can verify on specified field only.\n    parsed_response = Hashr.new(response_hash)\n\n    # Use nice dot method call nonation.\n    target = parsed_response.data.reportQuery.target\n\n    target.processedTotalCount.should eq 44\n    target.qualifiedTotalCount.should eq 40\n  end\nend\n```\n\nYou can use Hashr as model object, like this:\n\n```crystal\nit \"use hashr as model object\" do\n    contacts = [\n      {\n        id:    1,\n        first: \"billy\",\n        last:  \"zheng\",\n        phone: \"18612385678\",\n        email: \"billy@gmail.com\",\n      },\n      {\n        id:    2,\n        first: \"xuan\",\n        last:  \"zheng\",\n        phone: \"18512345678\",\n        email: \"retired@qq.com\",\n      },\n    ]\n\n    models = contacts.map { |e| Hashr.new(e) }\n\n    models.each do |contact|\n      puts \"#{contact.first}.#{contact.last}: phone: #{contact.phone}, email: #{contact.email}\"\n    end\n\t\n\tbilly = models.first\n\tbilly.phone = \"13012345678\"\n\tp billy.name # =\u003e 13012345678\n```\n\n## Limit\n\nFor verify a value is nil, you have to use `eq`, `be_nil` not work because Crystal don't allow us redefine #nil? method on any object.\n\n```crystal\nh = {\"nilValue\" =\u003e nil}.to_json\nvalue = Hashr.new({\"foo\" =\u003e JSON.parse(h)})\n\nvalue.foo.nilValue.should eq nil # =\u003e true\nvalue.foo.nilValue.should be_nil    # =\u003e false\n```\n\n## Development\n\nTODO: Write development instructions here\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/crystal-china/hashr/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Billy.Zheng](https://github.com/zw963) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrystal-china%2Fhashr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrystal-china%2Fhashr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrystal-china%2Fhashr/lists"}