class LineNotifyClient::HttpClient

## LineNotify::Client A class for LINE Notify API client.

Constants

ACCEPT
HEADERS
HOST
USER_AGENT

Public Class Methods

new(access_token) click to toggle source
# File lib/line_notify_client/http_client.rb, line 13
def initialize(access_token)
  @access_token = access_token
end

Public Instance Methods

notify(message, options = {}) click to toggle source

### LineNotify::Client#notify Sends notifications to users or groups that are related to an access token.

POST notify-api.line.me/api/notify

# File lib/line_notify_client/http_client.rb, line 21
def notify(message, options = {})
  params = { message: message }.merge(options)
  post('/api/notify', params, options)
end
revoke() click to toggle source

### LineNotify::Client#revoke Disable an access token.

POST notify-api.line.me/api/revoke

# File lib/line_notify_client/http_client.rb, line 38
def revoke
  post('/api/revoke')
end
status() click to toggle source

### LineNotify::Client#status Check the validity of an access token.

GET notify-api.line.me/api/status

# File lib/line_notify_client/http_client.rb, line 30
def status
  get('/api/status')
end

Private Instance Methods

connection() click to toggle source
# File lib/line_notify_client/http_client.rb, line 61
def connection
  @connection ||= Faraday.new(faraday_client_options) do |builder|
    builder.options.timeout = 5 # 5 seconds
    builder.request :url_encoded
    builder.request :json
    builder.response :json
    builder.adapter Faraday.default_adapter
  end
end
faraday_client_options() click to toggle source
# File lib/line_notify_client/http_client.rb, line 71
def faraday_client_options
  {
    url: "https://#{HOST}",
    ssl: { verify: true },
    headers: faraday_headers
  }
end
faraday_headers() click to toggle source
# File lib/line_notify_client/http_client.rb, line 79
def faraday_headers
  HEADERS.merge('Authorization' => "Bearer #{@access_token}")
end
get(path, params = nil, headers = nil) click to toggle source
# File lib/line_notify_client/http_client.rb, line 44
def get(path, params = nil, headers = nil)
  request(:get, path, params, headers)
end
post(path, params = nil, headers = nil) click to toggle source
# File lib/line_notify_client/http_client.rb, line 48
def post(path, params = nil, headers = nil)
  request(:post, path, params, headers)
end
request(http_method, path, params, headers) click to toggle source
# File lib/line_notify_client/http_client.rb, line 52
def request(http_method, path, params, headers)
  faraday_response = connection.send(http_method, path, params, headers)
  LineNotify::Response.new(faraday_response)
rescue Faraday::TimeoutError => e
  raise LineNotify::TimeoutError, e.message
rescue Faraday::Error => e
  raise LineNotify::Error, e.message
end