Class: MicroCMS::Client

Inherits:
Object
  • Object
show all
Includes:
HttpUtil
Defined in:
lib/microcms.rb

Overview

Client

Instance Method Summary collapse

Methods included from HttpUtil

#send_http_request

Constructor Details

#initialize(service_domain, api_key) ⇒ Client

Returns a new instance of Client.



84
85
86
87
# File 'lib/microcms.rb', line 84

def initialize(service_domain, api_key)
  @service_domain = service_domain
  @api_key = api_key
end

Instance Method Details

#create(endpoint, content, option = {}) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/microcms.rb', line 111

def create(endpoint, content, option = {})
  if content[:id]
    put(endpoint, content, option)
  else
    post(endpoint, content, option)
  end
end

#delete(endpoint, id) ⇒ Object



124
125
126
# File 'lib/microcms.rb', line 124

def delete(endpoint, id)
  send_http_request('DELETE', endpoint, id)
end

#get(endpoint, id = '', option = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/microcms.rb', line 98

def get(endpoint, id = '', option = {})
  send_http_request(
    'GET',
    endpoint,
    id,
    {
      draftKey: option[:draft_key],
      fields: option[:fields] ? option[:fields].join(',') : nil,
      depth: option[:depth]
    }.select { |_key, value| value }
  )
end

#list(endpoint, option = {}) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/microcms.rb', line 89

def list(endpoint, option = {})
  list = send_http_request('GET', endpoint, nil, build_query(option))
  if list[:totalCount]
    list[:total_count] = list[:totalCount]
    list.delete_field(:totalCount)
  end
  list
end

#update(endpoint, content) ⇒ Object



119
120
121
122
# File 'lib/microcms.rb', line 119

def update(endpoint, content)
  body = content.reject { |key, _value| key == :id }
  send_http_request('PATCH', endpoint, content[:id], nil, body)
end