Class: Breacan::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Api, Auth, Channels, Chat, Emoji, Files, Groups, Im, OAuth, Rtm, Search, Star, Usergroups, Users, Configurable
Defined in:
lib/breacan/client.rb,
lib/breacan/client/im.rb,
lib/breacan/client/api.rb,
lib/breacan/client/rtm.rb,
lib/breacan/client/auth.rb,
lib/breacan/client/chat.rb,
lib/breacan/client/star.rb,
lib/breacan/client/emoji.rb,
lib/breacan/client/files.rb,
lib/breacan/client/oauth.rb,
lib/breacan/client/users.rb,
lib/breacan/client/groups.rb,
lib/breacan/client/search.rb,
lib/breacan/client/channels.rb,
lib/breacan/client/usergroups.rb

Defined Under Namespace

Modules: Api, Auth, Channels, Chat, Emoji, Files, Groups, Im, OAuth, Rtm, Search, Star, Usergroups, Users

Constant Summary collapse

CONVENIENCE_HEADERS =
Set.new(i(accept content_type))

Constants included from Configurable

Breacan::Configurable::OPTIONS_KEYS

Instance Method Summary collapse

Methods included from Usergroups

#usergroups_create, #usergroups_disable, #usergroups_enable, #usergroups_list, #usergroups_update, #usergroups_users_list, #usergroups_users_update

Methods included from Users

#user_by_name, #users_get_presence, #users_info, #users_list, #users_set_active, #users_set_presence

Methods included from Star

#star_list

Methods included from Search

#search_all, #search_files, #search_messages

Methods included from Rtm

#rtm_start

Methods included from OAuth

#oauth_access

Methods included from Im

#im_close, #im_history, #im_list, #im_mark, #im_open

Methods included from Groups

#groups_archive, #groups_create, #groups_history, #groups_info, #groups_info_by_name, #groups_invite, #groups_join, #groups_kick, #groups_leave, #groups_list, #groups_mark, #groups_rename, #groups_set_purpose, #groups_set_topic, #groups_unarchive

Methods included from Files

#files_delete, #files_info, #files_list, #files_upload, #reset_files_upload_builder, #set_files_upload_builder

Methods included from Emoji

#emoji_list

Methods included from Chat

#chat_delete, #chat_post_message, #chat_update

Methods included from Channels

#channel_by_name, #channels_archive, #channels_create, #channels_history, #channels_info, #channels_invite, #channels_join, #channels_kick, #channels_leave, #channels_list, #channels_mark, #channels_rename, #channels_set_purpose, #channels_set_topic, #channels_unarchive

Methods included from Auth

#auth_test

Methods included from Api

#api_test

Methods included from Configurable

#api_endpoint, #configure, #default_builder, #default_serializer, keys, #options, #reset!, #web_endpoint

Methods included from Authentication

#token_authenticated?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
# File 'lib/breacan/client.rb', line 42

def initialize(options = {})
  Breacan::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Breacan.instance_variable_get(:"@#{key}"))
  end
end

Instance Method Details

#agentObject



115
116
117
118
119
120
# File 'lib/breacan/client.rb', line 115

def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:accept] = media_type
    http.headers[:user_agent] = user_agent
  end
end

#delete(url, options = {}) ⇒ Object



107
108
109
# File 'lib/breacan/client.rb', line 107

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Object



52
53
54
55
# File 'lib/breacan/client.rb', line 52

def get(url, options = {})
  return get_with_auto_paginate(url, options) if auto_paginate
  request :get, url, parse_query_and_convenience_headers(options)
end

#get_with_auto_paginate(url, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/breacan/client.rb', line 57

def get_with_auto_paginate(url, options = {})
  res = nil
  if url.end_with?('.list')
    nextc = nil
    loop do
      r = request :get, url, parse_query_and_convenience_headers(options.merge({cursor: nextc}))
      if r.is_a?(Sawyer::Resource)
        res ||= {}
        res = res.deep_merge(r) do |key, this_val, other_val|
          if this_val.is_a?(Array) && other_val.is_a?(Array)
            this_val + other_val
          else
            other_val
          end
        end
      elsif r.is_a?(Array)
        res ||= []
        res.concat(r)
      else
        raise "unknown response type"
      end

      if r.respond_to?(:response_metadata)
        if r..next_cursor == ""
          res = Sawyer::Resource.new(Sawyer::Agent.new(api_endpoint), res)
          break
        end
        nextc = r..next_cursor
      else
        break
      end
    end
  else
    res = request :get, url, parse_query_and_convenience_headers(options)
  end
  res
end

#head(url, options = {}) ⇒ Object



111
112
113
# File 'lib/breacan/client.rb', line 111

def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end

#patch(url, options = {}) ⇒ Object



103
104
105
# File 'lib/breacan/client.rb', line 103

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Object



95
96
97
# File 'lib/breacan/client.rb', line 95

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Object



99
100
101
# File 'lib/breacan/client.rb', line 99

def put(url, options = {})
  request :put, url, options
end

#same_options?(opts) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/breacan/client.rb', line 48

def same_options?(opts)
  opts.hash == options.hash
end