Class: Multilocale::Projects
- Inherits:
-
Object
- Object
- Multilocale::Projects
- Defined in:
- lib/multilocale/projects.rb
Overview
/api/projects. Reads need the projects:read scope, writes projects:write.
Instance Method Summary collapse
-
#create(attributes) ⇒ Object
create(name: "website", default_locale: "en", locales: %w[en es fr]).
-
#find(id_or_name) ⇒ Object
Accepts an id (24 hex characters) or a name.
-
#find_by(id_or_name) ⇒ Object
Returns nil instead of raising when there is no such project.
-
#initialize(client) ⇒ Projects
constructor
A new instance of Projects.
-
#list ⇒ Object
Every project the credential can see.
-
#update(id, attributes) ⇒ Object
locales:is a REPLACEMENT, not a merge.
Constructor Details
#initialize(client) ⇒ Projects
Returns a new instance of Projects.
6 7 8 |
# File 'lib/multilocale/projects.rb', line 6 def initialize(client) @client = client end |
Instance Method Details
#create(attributes) ⇒ Object
create(name: "website", default_locale: "en", locales: %w[en es fr])
32 33 34 |
# File 'lib/multilocale/projects.rb', line 32 def create(attributes) Project.new(@client.post("projects", body: Project.to_api(attributes))) end |
#find(id_or_name) ⇒ Object
Accepts an id (24 hex characters) or a name. Names are unique per organization, not globally, and the credential supplies the organization.
18 19 20 21 22 |
# File 'lib/multilocale/projects.rb', line 18 def find(id_or_name) raise ConfigurationError, "Project id or name required" if id_or_name.nil? || id_or_name.to_s.empty? Project.new(@client.get("projects/#{Encoding.percent_encode(id_or_name)}")) end |
#find_by(id_or_name) ⇒ Object
Returns nil instead of raising when there is no such project.
25 26 27 28 29 |
# File 'lib/multilocale/projects.rb', line 25 def find_by(id_or_name) find(id_or_name) rescue NotFoundError nil end |
#list ⇒ Object
Every project the credential can see. A project-scoped API key sees exactly one — the server filters the list, it does not 403.
12 13 14 |
# File 'lib/multilocale/projects.rb', line 12 def list Array(@client.get("projects")).map { |attributes| Project.new(attributes) } end |
#update(id, attributes) ⇒ Object
locales: is a REPLACEMENT, not a merge. Read the project first and send
the full list, or you will silently drop the locales you left out.
38 39 40 |
# File 'lib/multilocale/projects.rb', line 38 def update(id, attributes) Project.new(@client.put("projects/#{Encoding.percent_encode(id)}", body: Project.to_api(attributes))) end |