class Pixela::Client

Constants

API_ENDPOINT
TOP_ENDPOINT

Attributes

token[R]

@!attribute [r] token @return [String]

username[R]

@!attribute [r] username @return [String]

Public Class Methods

new(username:, token:) click to toggle source

@param username [String] @param token [String] secret token

# File lib/pixela/client.rb, line 24
def initialize(username:, token:)
  @username = username
  @token    = token
end

Public Instance Methods

graph(graph_id) click to toggle source

@param graph_id [String]

@return [Pixela::Graph]

# File lib/pixela/client.rb, line 38
def graph(graph_id)
  Graph.new(client: self, graph_id: graph_id)
end
inspect() click to toggle source

@return [String]

# File lib/pixela/client.rb, line 30
def inspect
  # NOTE: hide @token
  %Q(#<Pixela::Client:0x#{"%016X" % object_id} @username="#{username}">)
end
webhook(webhook_hash) click to toggle source

@param webhook_hash [String]

@return [Pixela::Webhook]

# File lib/pixela/client.rb, line 45
def webhook(webhook_hash)
  Webhook.new(client: self, webhook_hash: webhook_hash)
end

Private Instance Methods

connection(request_headers: user_token_headers, endpoint: API_ENDPOINT) click to toggle source

@param request_headers [Hash]

@return [Faraday::Connection]

# File lib/pixela/client.rb, line 58
def connection(request_headers: user_token_headers, endpoint: API_ENDPOINT)
  Faraday.new(url: endpoint, headers: request_headers) do |conn|
    conn.request :json
    conn.response :mashify, mash_class: Pixela::Response
    conn.response :json
    conn.response :raise_error

    if Pixela.config.debug_logger
      conn.request :curl, Pixela.config.debug_logger, :debug
      conn.response :logger, Pixela.config.debug_logger
    end

    conn.adapter Faraday.default_adapter
  end
end
default_headers() click to toggle source
# File lib/pixela/client.rb, line 85
def default_headers
  {
    "User-Agent" => "Pixela v#{Pixela::VERSION} (https://github.com/sue445/pixela)",
    "Content-Type" => "application/json",
  }
end
to_boolean_string(flag) click to toggle source
# File lib/pixela/client.rb, line 96
def to_boolean_string(flag)
  flag ? "yes" : "no"
end
to_ymd(date) click to toggle source
# File lib/pixela/client.rb, line 100
def to_ymd(date)
  return nil unless date

  date.strftime("%Y%m%d")
end
user_token_headers() click to toggle source
# File lib/pixela/client.rb, line 92
def user_token_headers
  { "X-USER-TOKEN" => token }.merge(default_headers)
end
with_error_handling() { || ... } click to toggle source
# File lib/pixela/client.rb, line 74
def with_error_handling
  yield
rescue Faraday::ClientError, Faraday::ServerError => error
  begin
    body = JSON.parse(error.response[:body])
    raise PixelaError, body["message"]
  rescue JSON::ParserError
    raise error
  end
end