Module: ButterCMS

Defined in:
lib/buttercms/tag.rb,
lib/buttercms-ruby.rb,
lib/buttercms/feed.rb,
lib/buttercms/page.rb,
lib/buttercms/post.rb,
lib/buttercms/author.rb,
lib/buttercms/errors.rb,
lib/buttercms/content.rb,
lib/buttercms/version.rb,
lib/buttercms/category.rb,
lib/buttercms/hash_to_object.rb,
lib/buttercms/butter_resource.rb,
lib/buttercms/butter_collection.rb,
lib/buttercms/data_store_adapters/yaml.rb,
lib/buttercms/data_store_adapters/redis.rb,
lib/buttercms/data_store_adapters/redis_ssl.rb

Defined Under Namespace

Modules: DataStoreAdapters Classes: Author, BadRequest, ButterCollection, ButterResource, Category, Content, Error, Feed, HashToObject, NotFound, Page, Post, Tag, Unauthorized

Constant Summary collapse

VERSION =
'2.4'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_tokenObject

Returns the value of attribute api_token.



30
31
32
# File 'lib/buttercms-ruby.rb', line 30

def api_token
  @api_token
end

.data_storeObject

Returns the value of attribute data_store.



35
36
37
# File 'lib/buttercms-ruby.rb', line 35

def data_store
  @data_store
end

.loggerObject



39
40
41
42
43
# File 'lib/buttercms-ruby.rb', line 39

def self.logger
  @logger ||= Logger.new($stdout).tap do |log|
    log.progname = "ButterCMS"
  end
end

.open_timeoutObject

Returns the value of attribute open_timeout.



34
35
36
# File 'lib/buttercms-ruby.rb', line 34

def open_timeout
  @open_timeout
end

.read_timeoutObject

Returns the value of attribute read_timeout.



33
34
35
# File 'lib/buttercms-ruby.rb', line 33

def read_timeout
  @read_timeout
end

.test_modeObject

Returns the value of attribute test_mode.



32
33
34
# File 'lib/buttercms-ruby.rb', line 32

def test_mode
  @test_mode
end

.write_api_tokenObject

Returns the value of attribute write_api_token.



31
32
33
# File 'lib/buttercms-ruby.rb', line 31

def write_api_token
  @write_api_token
end

Class Method Details

.api_request(path, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/buttercms-ruby.rb', line 70

def self.api_request(path, options = {})
  query = options.dup
  query[:auth_token] ||= api_token

  if test_mode
    query[:test] = 1
  end

  # If the user has passed in a "/" leading path, don't interpret that
  # as wanting to get rid of the API prefix
  if path.start_with?("/")
    path = path[1..-1]
  end

  path = Pathname.new(@api_url.path).join(path).to_s + "?#{URI.encode_www_form(query)}"

  response =
    Net::HTTP.start(@api_url.host, @api_url.port, http_options) do |http|
      request = Net::HTTP::Get.new(path)
      request["User-Agent"] = "ButterCMS/Ruby #{ButterCMS::VERSION}"
      request["Accept"]     = "application/json"

      http.request(request)
    end

  case response
  when Net::HTTPNotFound
    raise ::ButterCMS::NotFound, JSON.parse(response.body)["detail"]
  when Net::HTTPUnauthorized
    raise ::ButterCMS::Unauthorized, JSON.parse(response.body)['detail']
  end

  response.body
end

.request(path, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/buttercms-ruby.rb', line 105

def self.request(path, options = {})
  raise ArgumentError.new "Please set your API token" unless api_token

  key = "buttercms:#{path}:#{options}"

  begin
    result = api_request(path, options)

    if data_store
      data_store.set(key, result)
      logger.info "Set key #{key}"
    end

  # TODO - more selective exception handling (SocketError)
  rescue Exception => e

    if data_store
      if result = data_store.get(key)
        logger.info "Fetched key #{key}"

        # Log request error
        logger.error e
      else
        logger.info "No data for key #{key}"
      end
    end

    # Raise request exception if there's no data store or value returned
    raise e unless data_store && result
  end

  return JSON.parse(result)
end

.write_api_request(path, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/buttercms-ruby.rb', line 146

def self.write_api_request(path, options = {})
  query = options.dup
  token_for_request = query.delete(:auth_token) || write_api_token

  path = "#{@api_url.path}#{URI.encode(path)}"

  response =
    Net::HTTP.start(@api_url.host, @api_url.port, http_options) do |http|
      write_type = query.delete(:method) || "Post"
      request_type = "Net::HTTP::#{write_type}".constantize
      request = request_type.new(path)
      request["User-Agent"] = "ButterCMS/Ruby #{ButterCMS::VERSION}"
      request["Accept"]     = "application/json"
      request["Content-Type"] = "application/json"
      request["Authorization"] = "Token #{token_for_request}"
      request.body = query.except(:auth_token).to_json

      http.request(request)
    end

  case response
  when Net::HTTPNotFound
    raise ::ButterCMS::NotFound, JSON.parse(response.body)["detail"]
  when Net::HTTPBadRequest
    parsed_body = JSON.parse(response.body)
    errors = if parsed_body.is_a?(Array)
      parsed_body.join(' ')
    else
      parsed_body.map do |k, v|
        "#{k}: #{v}"
      end.join(" ")
    end
    raise ::ButterCMS::BadRequest, errors
  end

  response.body
end

.write_request(path, options = {}) ⇒ Object



139
140
141
142
143
144
# File 'lib/buttercms-ruby.rb', line 139

def self.write_request(path, options = {})
  raise ArgumentError.new "Please set your write API token" unless write_api_token
  result = write_api_request(path, options)

  return JSON.parse(result)
end