Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Jan 8, 2025
1 parent a382f99 commit 98bbc46
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 67 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
"friendly_image_tag": "latest",
"image_tag": "prod-${{ github.sha }}",
"ecs_cluster": "scfm",
"docs_path": "docs/scfm"
"docs_path": "docs/open-ews"
}
]
EOF
Expand Down Expand Up @@ -141,9 +141,6 @@ jobs:
cp -R api_docs/api/* source
bundle exec middleman build --build-dir=build/api
cp -R api_docs/open_ews_api/* source
bundle exec middleman build --build-dir=build/open_ews_api
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
Expand All @@ -157,7 +154,6 @@ jobs:
- name: Deploy API Documentation
run: |
aws s3 sync --delete --acl public-read build/api s3://www.somleng.org/${{ matrix.docs_path }}
aws s3 sync --delete --acl public-read build/open_ews_api s3://www.somleng.org/${{ matrix.docs_path }}/open_ews_api
- name: Invalidate Cache
run: aws cloudfront create-invalidation --distribution-id E3962XCJFZ0KB1 --paths /${{ matrix.docs_path }}/\*
Expand Down
16 changes: 8 additions & 8 deletions app/request_schemas/v1/beneficiary_request_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BeneficiaryRequestSchema < JSONAPIRequestSchema
required(:data).value(:hash).schema do
required(:type).filled(:str?, eql?: "beneficiary")
required(:attributes).value(:hash).schema do
required(:msisdn).filled(:string)
required(:phone_number).filled(:string)
required(:iso_country_code).filled(Types::UpcaseString, included_in?: Contact.iso_country_code.values)
optional(:language_code).maybe(:string)
optional(:date_of_birth).maybe(:date)
Expand All @@ -25,32 +25,32 @@ class BeneficiaryRequestSchema < JSONAPIRequestSchema
end
end

attribute_rule(:msisdn).validate(:phone_number_format)
attribute_rule(:phone_number).validate(:phone_number_format)
rule(data: :attributes) do
Rules.new(self).validate
end

def output
result = super
result[:msisdn] = PhonyRails.normalize_number(result.fetch(:msisdn)) if result.key?(:msisdn)
result[:msisdn] = PhonyRails.normalize_number(result.delete(:phone_number))
result
end

class Rules < SchemaRules::JSONAPISchemaRules
def validate
if resource.blank?
return key(:msisdn).failure(text: "can't be blank") if values[:msisdn].blank?
return key(:phone_number).failure(text: "can't be blank") if values[:phone_number].blank?

key(:msisdn).failure(text: "must be unique") if contact_exists?
elsif values[:msisdn].present?
key(:msisdn).failure(text: "must be unique") if contact_exists?
key(:phone_number).failure(text: "must be unique") if contact_exists?
elsif values[:phone_number].present?
key(:phone_number).failure(text: "must be unique") if contact_exists?
end
end

private

def contact_exists?
relation = account.contacts.where_msisdn(values.fetch(:msisdn))
relation = account.contacts.where_msisdn(values.fetch(:phone_number))
relation = relation.where.not(id: resource.id) if resource.present?
relation.exists?
end
Expand Down
8 changes: 4 additions & 4 deletions app/request_schemas/v1/update_beneficiary_request_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ class UpdateBeneficiaryRequestSchema < JSONAPIRequestSchema
required(:id).filled(:integer)
required(:type).filled(:str?, eql?: "beneficiary")
required(:attributes).value(:hash).schema do
optional(:msisdn).filled(:string)
optional(:iso_country_code).filled(Types::UpcaseString, included_in?: Contact.iso_country_code.values)
optional(:phone_number).filled(:string)
optional(:language_code).maybe(:string)
optional(:date_of_birth).maybe(:date)
optional(:gender).maybe(:string, included_in?: Contact.gender.values)
optional(:iso_country_code).maybe(:string, included_in?: Contact.iso_country_code.values)
optional(:metadata).maybe(:hash?)
end
end
end

attribute_rule(:msisdn).validate(:phone_number_format)
attribute_rule(:phone_number).validate(:phone_number_format)
rule do
BeneficiaryRequestSchema::Rules.new(self).validate
end

def output
result = super
result[:msisdn] = PhonyRails.normalize_number(result.fetch(:msisdn)) if result.key?(:msisdn)
result[:msisdn] = PhonyRails.normalize_number(result.delete(:phone_number)) if result.key?(:phone_number)
result
end
end
Expand Down
6 changes: 5 additions & 1 deletion app/serailizers/beneficiary_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class BeneficiarySerializer < ResourceSerializer
attributes :msisdn, :language_code, :date_of_birth, :iso_country_code, :metadata
attributes :phone_number, :language_code, :date_of_birth, :iso_country_code, :metadata
has_many :addresses, serializer: BeneficiaryAddressSerializer

attribute :gender do |object|
object.gender_value
end

attribute :phone_number do |object|
object.msisdn
end
end
24 changes: 12 additions & 12 deletions spec/request_schemas/v1/beneficiary_request_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

module V1
RSpec.describe BeneficiaryRequestSchema, type: :request_schema do
it "validates the msisdn" do
it "validates the phone_number" do
contact = create(:contact)

expect(
validate_schema(input_params: { data: { attributes: { msisdn: nil } } })
).not_to have_valid_field(:data, :attributes, :msisdn)
validate_schema(input_params: { data: { attributes: { phone_number: nil } } })
).not_to have_valid_field(:data, :attributes, :phone_number)

expect(
validate_schema(input_params: { data: { attributes: { msisdn: "+855 97 2345 6789" } } })
).not_to have_valid_field(:data, :attributes, :msisdn)
validate_schema(input_params: { data: { attributes: { phone_number: "+855 97 2345 6789" } } })
).not_to have_valid_field(:data, :attributes, :phone_number)

expect(
validate_schema(input_params: { data: { attributes: { msisdn: "+855 97 2345 678" } } })
).to have_valid_field(:data, :attributes, :msisdn)
validate_schema(input_params: { data: { attributes: { phone_number: "+855 97 2345 678" } } })
).to have_valid_field(:data, :attributes, :phone_number)

expect(
validate_schema(
input_params: { data: { attributes: { msisdn: contact.msisdn, iso_country_code: "KH" } } },
input_params: { data: { attributes: { phone_number: contact.msisdn, iso_country_code: "KH" } } },
options: { account: contact.account }
)
).not_to have_valid_field(:data, :attributes, :msisdn)
).not_to have_valid_field(:data, :attributes, :phone_number)

expect(
validate_schema(
input_params: { data: { attributes: { msisdn: "+855 97 2345 678", iso_country_code: "KH" } } },
input_params: { data: { attributes: { phone_number: "+855 97 2345 678", iso_country_code: "KH" } } },
options: { account: contact.account }
)
).to have_valid_field(:data, :attributes, :msisdn)
).to have_valid_field(:data, :attributes, :phone_number)
end

it "validates the iso_country_code" do
Expand Down Expand Up @@ -131,7 +131,7 @@ module V1
input_params: {
data: {
attributes: {
msisdn: "(855) 97 2345 678",
phone_number: "(855) 97 2345 678",
iso_country_code: "kh"
}
}
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/open_ews_api/v1/beneficiaries/addresses_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "rails_helper"

RSpec.resource "Beneficiary's Addresses" do
RSpec.resource "Addresses" do
get "/v1/beneficiaries/:beneficiary_id/addresses" do
example "List all a beneficiary's addresses" do
example "List all addresses for a beneficiary" do
account = create(:account)
beneficiary = create(:beneficiary, account:)
address1 = create(:beneficiary_address, beneficiary:)
Expand Down Expand Up @@ -51,7 +51,7 @@
end

get "/v1/beneficiaries/:beneficiary_id/addresses/:id" do
example "Get an address for a beneficiary" do
example "Fetch an address for a beneficiary" do
account = create(:account)
beneficiary = create(:beneficiary, account:)
address = create(:beneficiary_address, beneficiary:)
Expand Down
18 changes: 9 additions & 9 deletions spec/requests/open_ews_api/v1/beneficiaries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
post "/v1/beneficiaries" do
with_options scope: %i[data attributes] do
parameter(
:msisdn, "Phone number in E.164 format or shortcode.",
:phone_number, "Phone number in E.164 format or shortcode.",
required: true
)
parameter(
Expand All @@ -60,7 +60,7 @@
required: false
)
parameter(
:iso_country_code, "The ISO 3166-1 alpha-2 country code of the phone number. It must be matched with the country of `msisdn` parameter.",
:iso_country_code, "The ISO 3166-1 alpha-2 country code of the phone number. It must be matched with the country of `phone_number` parameter.",
required: false
)
parameter(
Expand Down Expand Up @@ -107,7 +107,7 @@
data: {
type: :beneficiary,
attributes: {
msisdn: "+85510999999",
phone_number: "+85510999999",
language_code: "km",
gender: "M",
date_of_birth: "1990-01-01",
Expand All @@ -125,7 +125,7 @@
expect(response_status).to eq(201)
expect(response_body).to match_jsonapi_resource_schema("beneficiary")
expect(jsonapi_response_attributes).to include(
"msisdn" => "+85510999999",
"phone_number" => "+85510999999",
"language_code" => "km",
"gender" => "M",
"date_of_birth" => "1990-01-01",
Expand All @@ -149,7 +149,7 @@
data: {
type: :beneficiary,
attributes: {
msisdn: "+85510999999",
phone_number: "+85510999999",
iso_country_code: "kh"
}
}
Expand All @@ -159,13 +159,13 @@
expect(response_body).to match_api_response_schema("jsonapi_error")
expect(json_response.dig("errors", 0)).to include(
"title" => "must be unique",
"source" => { "pointer" => "/data/attributes/msisdn" }
"source" => { "pointer" => "/data/attributes/phone_number" }
)
end
end

get "/v1/beneficiaries/:id" do
example "Get a beneficiary" do
example "Fetch a beneficiary" do
beneficiary = create(:beneficiary)

set_authorization_header_for(beneficiary.account)
Expand Down Expand Up @@ -195,7 +195,7 @@
id: beneficiary.id,
type: :beneficiary,
attributes: {
msisdn: "+85510999002",
phone_number: "+85510999002",
gender: "F",
status: "disabled",
language_code: "en",
Expand All @@ -210,7 +210,7 @@
expect(response_status).to eq(200)
expect(response_body).to match_jsonapi_resource_schema("beneficiary")
expect(jsonapi_response_attributes).to include(
"msisdn" => "+85510999002",
"phone_number" => "+85510999002",
"language_code" => "en",
"gender" => "F",
"date_of_birth" => "1990-01-01",
Expand Down
2 changes: 1 addition & 1 deletion spec/support/api_response_schemas/beneficiary_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module APIResponseSchema
required(:type).filled(eql?: "beneficiary")

required(:attributes).schema do
required(:msisdn).filled(:str?)
required(:phone_number).filled(:str?)
required(:language_code).maybe(:str?)
required(:gender).maybe(:str?)
required(:date_of_birth).maybe(:str?)
Expand Down
4 changes: 2 additions & 2 deletions spec/support/request_spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def jsonapi_response_attributes
config.include(RequestSpecHelpers, type: :request)

config.define_derived_metadata(file_path: %r{spec/requests/scfm_api/}) do |metadata|
metadata[:document] = :scfm_api
metadata[:document] = false
end

config.define_derived_metadata(file_path: %r{spec/requests/open_ews_api/}) do |metadata|
metadata[:document] = :open_ews_api
metadata[:jsonapi] = true
end
end
16 changes: 3 additions & 13 deletions spec/support/rspec_api_documentation.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "rspec_api_documentation/dsl"

RspecApiDocumentation.configure do |config|
config.api_name = "Somleng SCFM API Documentation"
config.api_name = "OpenEWS API Documentation"
config.api_explanation = <<~HEREDOC
This is the API Documentation for Somleng Simple Call Flow Manager (Somleng SCFM).
This is the API Documentation for OpenEWS.
HEREDOC
config.format = :slate
config.curl_headers_to_filter = [ "Host", "Cookie", "Content-Type" ]
Expand All @@ -13,7 +13,7 @@
config.request_body_formatter = proc do |params|
JSON.pretty_generate(params) if params.present?
end
config.keep_source_order = false
config.keep_source_order = true
config.disable_dsl_status!

# https://github.com/zipmark/rspec_api_documentation/pull/458
Expand All @@ -24,14 +24,4 @@
response_body
end
end

config.define_group :scfm_api do |conf|
conf.filter = :scfm_api
conf.docs_dir = Rails.root.join("doc/api")
end

config.define_group :open_ews_api do |conf|
conf.filter = :open_ews_api
conf.docs_dir = Rails.root.join("doc/open_ews_api")
end
end
11 changes: 2 additions & 9 deletions spec/support/rspec_api_documentation_client.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
class APIDocumentationClient < RspecApiDocumentation::RackTestClient
DOC_HOSTS = {
scfm_api: "https://scfm.somleng.org",
open_ews_api: "https://api.open-ews.org"
}


private

def process(method, path, params = {}, headers = {})
if path.start_with?("http")
full_path = path
else
doc_host = DOC_HOSTS.fetch(metadata[:document]) { DOC_HOSTS[:scfm_api] }
full_path = URI.join(doc_host, path)
full_path = URI.join("https://api.open-ews.org", path)
end

do_request(method, full_path, params, headers)
Expand All @@ -30,7 +23,7 @@ def client
RSpec.configure do |config|
config.prepend(APIDocumentationHelpers, api_doc_dsl: :resource)

config.before(:each, document: :open_ews_api) do
config.before(:each, jsonapi: true) do
header("Content-Type", "application/vnd.api+json")
end
end

0 comments on commit 98bbc46

Please sign in to comment.