{"id":18319787,"url":"https://github.com/redding/and-son","last_synced_at":"2025-04-09T14:22:42.365Z","repository":{"id":5506201,"uuid":"6706238","full_name":"redding/and-son","owner":"redding","description":"Sanford client for Ruby","archived":false,"fork":false,"pushed_at":"2021-01-09T05:20:14.000Z","size":97,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-03-15T09:22:32.412Z","etag":null,"topics":["ruby","sanford","tcp","tcp-client"],"latest_commit_sha":null,"homepage":"https://github.com/redding/sanford-protocol","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"gruntjs/grunt-contrib-copy","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redding.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}},"created_at":"2012-11-15T14:49:39.000Z","updated_at":"2021-01-09T05:20:11.000Z","dependencies_parsed_at":"2022-07-08T03:02:56.930Z","dependency_job_id":null,"html_url":"https://github.com/redding/and-son","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fand-son","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fand-son/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fand-son/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fand-son/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redding","download_url":"https://codeload.github.com/redding/and-son/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054200,"owners_count":21039952,"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":["ruby","sanford","tcp","tcp-client"],"created_at":"2024-11-05T18:14:15.701Z","updated_at":"2025-04-09T14:22:42.345Z","avatar_url":"https://github.com/redding.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AndSon\n\nAndSon is a simple Sanford client for Ruby.  It provides an API for calling services and handling responses.  It uses [Sanford::Protocol](https://github.com/redding/sanford-protocol) to communicate with Sanford servers.\n\n## Usage\n\n```ruby\n# create a client\nclient = AndSon.new('127.0.0.1', 8000)\n\n# call a service and get its response data:\nuser_data = client.call('get_user_v1', {:user_name =\u003e 'joetest'})\n```\n\n## Calling Services\n\nTo call a service, you first need a client to make the calls.  You define clients by specifying the host's ip address and port.\n\nOnce you have your client defined, make service calls using the `call` method.  It will return any response data and raise an exception if anything goes wrong.\n\n### Timeouts\n\nBy default, all requests timeout after 60s.  You can override this globally using the `ANDSON_TIMEOUT` env var. You can override this global timeout on a per-call basis by chaining in the `timeout` method.\n\n```ruby\n# timeout this request after 10 seconds\nclient.timeout(10).call('get_user', {:user_name =\u003e 'joetest'})\n```\n\nWhen a request times out, a `Sanford::Protocol::TimeoutError` is raised:\n\n```ruby\nbegin\n  client.timeout(10).call('get_user', {:user_name =\u003e 'joetest'})\nrescue Sanford::Protocol::TimeoutError =\u003e err\n  puts \"timeout - so sad :(\"\nend\n```\n\n### Default Params\n\nSimilarly to timeouts, all requests default their params to an empty `Hash` (`{}`). This can be overriden using the `params` method.\n\n```ruby\n# add an API key to all requests made by this client, to authorize our client\nclient.params({ 'api_key' =\u003e 12345 }).call('get_user', {:user_name =\u003e 'joetest'})\n```\n\nOne thing to be aware of, AndSon has limited ability to 'merge' or 'append' params. For example:\n\n```ruby\n# raises an exception, can't merge a string on to a hash\nclient.params({ 'api_key' =\u003e 12345 }).call('get_user', 'joetest')\n```\n\nBe aware of this when setting default params and passing additional params with the `call` method. In general, it's recommended to use ruby's `Hash` for the best results.\n\n### Exception Handling\n\nAndSon raises exceptions when a call responds with a `4xx` or `5xx` response code (see [Sanford Status Codes](https://github.com/redding/sanford-protocol#status-codes) for more on response codes):\n\n* `400`: `BadRequestError \u003c ClientError`\n* `404`: `NotFoundError \u003c ClientError`\n* `4xx`: `ClientError \u003c RequestError`\n* `5xx`: `ServerError \u003c RequestError`\n\n```ruby\nclient.call('some_unknown_service')   #=\u003e NotFoundError...\n```\n\nEach exception knows about the response that raised it:\n\n```ruby\nbegin\n  client.call('some_unknown_service')\nrescue AndSon::NotFoundError =\u003e err\n  err.response       #=\u003e AndSon::Response ...\n  err.response.code  #=\u003e 404\nend\n```\n\n### Response Handling\n\nIf you call a service and pass it a block, no exceptions will be raised and the call will yield its response to the block.  The call will return the return value of the block.\n\n```ruby\nuser = client.call('get_user', { :user_name =\u003e 'joetest' }) do |response|\n  if response.code == 200\n    User.new(response.data)\n  else\n    NullUser.new\n  end\nend\n```\n\nFor more details about the response object, see [sanford-protocol](https://github.com/redding/sanford-protocol#response).\n\n## Contributing\n\n1. Fork it\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 new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fand-son","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredding%2Fand-son","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fand-son/lists"}