Module: GreenSMS

Defined in:
lib/greensms.rb,
lib/greensms/client.rb,
lib/greensms/version.rb,
lib/greensms/constants.rb,
lib/greensms/http/rest.rb,
lib/greensms/utils/str.rb,
lib/greensms/utils/url.rb,
lib/greensms/api/module.rb,
lib/greensms/api/schema.rb,
lib/greensms/http/error.rb,
lib/greensms/api/modules.rb,
lib/greensms/utils/version.rb,
lib/greensms/utils/validator.rb,
lib/greensms/api/module_loader.rb

Defined Under Namespace

Modules: API, Http Classes: Error, GreenSMSClient, MethodInvoker, RestError

Constant Summary collapse

VERSION =
"0.1.0"
BASE_URL =
"https://api3.greensms.ru"
VERSIONS =
{
  "v1" => "v1",
}
RES_STATUS_ACCEPTED =
"Accepted"
RES_STATUS_DELAYED =
"Delayed"
VALIDATION_SCHEMA =
{
  "account" => {
    "v1" => {
      "token" => {
        "type" => "object",
        "required" => [],
        "properties" => {
          "expire" => {
            "type" => "integer",
            "minimum" => 0,
          },
        },
      },
    },
  },
  "call" => common_schema.clone,
  "hlr" => common_schema.clone,
  "whois" => {
    "v1" => {
      "lookup" => {
        "type" => "object",
        "required" => ["to"],
        "properties" => {
          "to" => to_schema,
        },
      },
    },
  },
  "general" => {},
  "voice" => common_schema.clone.deep_merge(
    {
      "v1" => {
        "send" => {
          "type" => "object",
          "required" => ["txt"],
          "properties" => {
            "txt" => {
              "type" => "string",
              "minlength" => 1,
              "maxlength" => 5,
              "pattern" => "^[0-9]+",
            },
            "lang" => {
              "type" => "string",
            # "allowed" => ["ru", "en"],
            },
          },
        },
      },
    }
  ),
  "pay" => common_schema.clone.deep_merge({
    "v1" => {
      "send" => {
        "type" => "object",
        "required" => ["amount"],
        "properties" => {
          "amount" => {
            "type" => "number",
            "min" => 1,
          },
          "tag" => {
            "type" => "string",
          },
        },
      },
    },
  }),
  "sms" => common_schema.clone.deep_merge(
    {
      "v1" => {
        "send" => {
          "type" => "object",
          "required" => ["txt"],
          "properties" => {
            "txt" => {
              "type" => "string",
              "minlength" => 1,
            },
            "from" => {
              "type" => "string",
            },
            "tag" => {
              "type" => "string",
            },
            "hidden" => {
              "type" => "string",
            },
          },
        },
      },
    }
  ),
  "viber" => common_schema.clone.deep_merge(
    {
      "v1" => {
        "send" => {
          "type" => "object",
          "required" => ["txt"],
          "properties" => {
            "txt" => {
              "type" => "string",
              "minlength" => 1,
            },
            "from" => {
              "type" => "string",
            },
            "cascade" => {
              "type" => "string",
              "allowed" => ["sms", "voice"],
            },
          },
        },
      },
    }
  ),
  "social" => common_schema.clone.deep_merge(
    {
      "v1" => {
        "send" => {
          "type" => "object",
          "required" => ["txt"],
          "properties" => {
            "txt" => {
              "type" => "string",
              "minlength" => 1,
            },
            "from" => {
              "type" => "string",
            },
            "tag" => {
              "type" => "string",
            },
            "hidden" => {
              "type" => "string",
            },
          },
        },
      },
    }
  ),
}
MODULES =
{
  "account" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["account"],
    "versions" => {
      "v1" => {
        "balance" => {
          "args" => nil,
          "method" => "GET",
        },
        "token" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "tariff" => {
          "args" => nil,
          "method" => "GET",
        },
      },
    },
  },
  "general" => {
    "schema" => {},
    "static" => true,
    "versions" => {
      "v1" => {
        "status" => {
          "args" => nil,
          "method" => "GET",
        },
      },
    },
  },
  "call" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["call"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "whois" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["whois"],
    "versions" => {
      "v1" => {
        "lookup" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "voice" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["voice"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "pay" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["pay"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "hlr" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["hlr"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "sms" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["sms"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "viber" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["viber"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
  "social" => {
    "schema" => GreenSMS::VALIDATION_SCHEMA["social"],
    "versions" => {
      "v1" => {
        "send" => {
          "args" => ["params"],
          "method" => "POST",
        },
        "status" => {
          "args" => ["params"],
          "method" => "GET",
        },
      },
    },
  },
}

Class Method Summary collapse

Class Method Details

.base_urlObject



5
6
7
# File 'lib/greensms/utils/url.rb', line 5

def GreenSMS.base_url()
  return GreenSMS::BASE_URL
end

.build_url(base_url, url_parts) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/greensms/utils/url.rb', line 9

def GreenSMS.build_url(base_url, url_parts)
  if GreenSMS.is_null_or_empty(base_url)
    raise StandardError.new "Base URL cannot be empty"
  end

  url = base_url + "/" + url_parts.join("/")
  return url
end

.camelize(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/greensms/utils/str.rb', line 12

def GreenSMS.camelize(value)
  case value
  when Array
    value.map { |v| GreenSMS.camelize(v) }
  when Hash
    Hash[value.map { |k, v| [GreenSMS.camelize(k), GreenSMS.camelize(v)] }]
  when String
    value.split("_").inject { |m, p| m + p.capitalize }
  else
    value
  end
end

.get_version(version) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/greensms/utils/version.rb', line 5

def GreenSMS.get_version(version)
  if GreenSMS::is_null_or_empty(version)
    version = GreenSMS::VERSIONS["v1"]
  end

  version = version.downcase
  if GreenSMS::VERSIONS.has_key? version
    version = GreenSMS::VERSIONS[version]
  else
    raise StandardError.new "Invalid Version"
  end
  return version
end

.is_null_or_empty(input) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/greensms/utils/str.rb', line 2

def GreenSMS.is_null_or_empty(input)
  if input.nil?
    return true
  elsif input.empty?
    return true
  else
    return false
  end
end

.validate(schema, data) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/greensms/utils/validator.rb', line 5

def GreenSMS.validate(schema, data)
  res = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
  if !res.nil? && res.length > 0
    raise RestError.new("Validation Error", 1, res)
  end
  return nil
end