Class: Chatmeter::API

Inherits:
Object
  • Object
show all
Defined in:
lib/chatmeter/api.rb,
lib/chatmeter/api/mock.rb,
lib/chatmeter/api/group.rb,
lib/chatmeter/api/login.rb,
lib/chatmeter/api/errors.rb,
lib/chatmeter/api/review.rb,
lib/chatmeter/api/account.rb,
lib/chatmeter/api/campaign.rb,
lib/chatmeter/api/listings.rb,
lib/chatmeter/api/location.rb,
lib/chatmeter/api/mock/group.rb,
lib/chatmeter/api/mock/login.rb,
lib/chatmeter/api/competitors.rb,
lib/chatmeter/api/mock/review.rb,
lib/chatmeter/api/mock/account.rb,
lib/chatmeter/api/mock/campaign.rb,
lib/chatmeter/api/mock/listings.rb,
lib/chatmeter/api/mock/location.rb,
lib/chatmeter/api/single_signon.rb,
lib/chatmeter/api/user_management.rb,
lib/chatmeter/api/mock/competitors.rb,
lib/chatmeter/api/user_group_access.rb,
lib/chatmeter/api/mock/single_signon.rb,
lib/chatmeter/api/mock/user_management.rb,
lib/chatmeter/api/user_location_access.rb,
lib/chatmeter/api/mock/user_group_access.rb,
lib/chatmeter/api/mock/user_location_access.rb

Defined Under Namespace

Modules: Errors, Mock

Constant Summary collapse

HEADERS =
{
  'Accept-Encoding': 'gzip',
  'Accept':          'application/json',
  'Content-Type':    'application/json'
}
OPTIONS =
{
  headers:     {},
  host:        ENV['CHATMETER_API_ROOT'] || "live.chatmeter.com",
  username:    ENV['CHATMETER_ADMIN_USER'],
  password:    ENV['CHATMETER_ADMIN_PASSWORD'],
  scheme:      'https',
  api_version: 'v5'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chatmeter/api.rb', line 44

def initialize(options={})
  options = OPTIONS.merge(options)
  options = options.merge(headers: HEADERS)

  if !@api_key && options.has_key?(:username) && options.has_key?(:password)
    username = options.delete(:username)
    password = options.delete(:password)
    @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
    @api_key = self.(username, password)[:token]
  end

  headers = HEADERS.merge({ Authorization: @api_key })
  @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", headers: headers, mock: options[:mock])
end

Instance Method Details

#accounts(query = {}) ⇒ Object

GET /accounts



14
15
16
17
18
19
20
21
# File 'lib/chatmeter/api/account.rb', line 14

def accounts(query={})
  request(
    expects: 200,
    method:  :get,
    path:    '/accounts',
    query:    query
  )[:accounts]
end

#add_group_to_user(user_id, params) ⇒ Object

POST /users/user_id/groups



14
15
16
17
18
19
20
21
# File 'lib/chatmeter/api/user_group_access.rb', line 14

def add_group_to_user(user_id, params)
  request(
    expects:  200,
    method:   :post,
    path:     "/users/#{user_id}/groups",
    body:    params.to_json
  )
end

#add_location_to_user(user_id, location_ids) ⇒ Object

POST /users/user_id/locations



14
15
16
17
18
19
20
21
22
23
# File 'lib/chatmeter/api/user_location_access.rb', line 14

def add_location_to_user(user_id, location_ids)
  request(
    expects: 200,
    method: :post,
    path: "/users/#{user_id}/locations",
    body: {
      "locationIds": location_ids
    }.to_json
  )
end

#add_new_account(params) ⇒ Object

POST /accounts



24
25
26
27
28
29
30
31
# File 'lib/chatmeter/api/account.rb', line 24

def (params)
  request(
    expects:  200,
    method:   :post,
    path:     "/accounts",
    body:     params.to_json
  )
end

#add_new_location(fields) ⇒ Object

POST /locations



24
25
26
27
28
29
30
31
# File 'lib/chatmeter/api/location.rb', line 24

def add_new_location(fields)
  request(
    expects: 200,
    method:  :post,
    path:    '/locations',
    body:    fields.to_json
  )
end

#add_reviews(params) ⇒ Object

POST /v5/reviews



54
55
56
57
58
59
60
61
# File 'lib/chatmeter/api/review.rb', line 54

def add_reviews(params)
  request(
    expects:  [200, 201],
    method:   :post,
    path:     "/reviews",
    body:     params.to_json
  )
end

#add_sub_account(user_id, params) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/chatmeter/api/user_management.rb', line 72

def (user_id, params)
  request(
    expects: 200,
    method: :put,
    path: "/users/#{user_id}/accounts",
    body: params.to_json
  )
end

#aggregate_reports(review_id, params = {}) ⇒ Object

GET /reviews/reports/ReportId



44
45
46
47
48
49
50
51
# File 'lib/chatmeter/api/review.rb', line 44

def aggregate_reports(review_id, params={})
  request(
    expects: 200,
    method:  :get,
    path:    "/reviews/reports/{report_id}",
    query:    params
  )
end

#competitors(params) ⇒ Object

GET /competitors



5
6
7
8
9
10
11
12
# File 'lib/chatmeter/api/competitors.rb', line 5

def competitors(params)
  request(
    expects: 200,
    method:  :post,
    path:    "/competitors",
    body:    params.to_json
  )
end

#create_new_campaign(params) ⇒ Object

POST /reviewBuilder/campaign/create



14
15
16
17
18
19
20
21
# File 'lib/chatmeter/api/campaign.rb', line 14

def create_new_campaign(params)
  request(
    expects:  201,
    method:   :post,
    path:     "/reviewBuilder/campaign/create",
    body:     params.to_json
  )
end

#create_new_group(params) ⇒ Object

POST /groups



14
15
16
17
18
19
20
21
# File 'lib/chatmeter/api/group.rb', line 14

def create_new_group(params)
  request(
    expects:  200,
    method:   :post,
    path:     "/groups",
    body:    params.to_json
  )
end

#create_new_user(params) ⇒ Object

POST /users



24
25
26
27
28
29
30
31
# File 'lib/chatmeter/api/user_management.rb', line 24

def create_new_user(params)
  request(
    expects:  200,
    method:   :post,
    path:     "/users",
    body:     params.to_json
  )
end

#delete_campaign(campaign_id) ⇒ Object

DELETE /reviewBuilder/campaign/delete/campaignId



52
53
54
55
56
57
58
# File 'lib/chatmeter/api/campaign.rb', line 52

def delete_campaign(campaign_id)
  request(
    expects:  200,
    method:   :delete,
    path:     "/reviewBuilder/campaign/delete/#{campaign_id}"
  )
end

#delete_group(group_id) ⇒ Object

DELETE /groups/group_id



34
35
36
37
38
39
40
# File 'lib/chatmeter/api/group.rb', line 34

def delete_group(group_id)
  request(
    expects:  200,
    method:   :delete,
    path:     "/groups/#{group_id}"
  )
end

#delete_group_access(user_id, group_ids) ⇒ Object

DELETE /users/user_id/groups?groupIds=group_ids



24
25
26
27
28
29
30
# File 'lib/chatmeter/api/user_group_access.rb', line 24

def delete_group_access(user_id, group_ids)
  request(
    expects:  200,
    method:   :delete,
    path:     "/users/#{user_id}/groups?groupIds=#{group_ids.join(',')}"
  )
end

#delete_group_locations(group_id, locationsIds) ⇒ Object

DELETE /groups/group_id/locations



53
54
55
56
57
58
59
# File 'lib/chatmeter/api/group.rb', line 53

def delete_group_locations(group_id,locationsIds)
  request(
    expects:  200,
    method:   :delete,
    path:     "/groups/#{group_id}/locations?locationIds=#{locationsIds.join(',')}"
  )
end

#delete_location(location_id) ⇒ Object

DELETE /locations/locationId



44
45
46
47
48
49
50
# File 'lib/chatmeter/api/location.rb', line 44

def delete_location(location_id)
  request(
    expects:  200,
    method:   :delete,
    path:     "/locations/#{location_id}"
  )
end

#delete_review(review_id, params) ⇒ Object

DELETE /reviews/review_id



74
75
76
77
78
79
80
81
# File 'lib/chatmeter/api/review.rb', line 74

def delete_review(review_id, params)
  request(
    expects:  200,
    method:   :delete,
    path:     "/reviews/#{review_id}",
    query:    params
  )
end

#delete_user(user_id) ⇒ Object

DELETE /users/user_id



54
55
56
57
58
59
60
# File 'lib/chatmeter/api/user_management.rb', line 54

def delete_user(user_id)
  request(
    expects:  200,
    method:   :delete,
    path:     "/users/#{user_id}"
  )
end

#enable_user(user_id, params) ⇒ Object

POST /users



63
64
65
66
67
68
69
70
# File 'lib/chatmeter/api/user_management.rb', line 63

def enable_user(user_id, params)
  request(
    expects:  200,
    method:   :post,
    path:     "/users/#{user_id}/status",
    body:    params.to_json
  )
end

#get_account_groups(account_id) ⇒ Object

GET account’s groups



34
35
36
37
38
39
40
41
42
43
# File 'lib/chatmeter/api/account.rb', line 34

def ()
  request(
    expects: 200,
    method:  :get,
    path:    "/groups",
    query: {
      accountId: 
    }
  )[:groups]
end

#get_accounts_for_user(user_id) ⇒ Object

GET /accounts for user



5
6
7
8
9
10
11
# File 'lib/chatmeter/api/account.rb', line 5

def get_accounts_for_user(user_id)
  request(
    expects: 200,
    method:  :get,
    path:    "/accounts/forUser/#{user_id}"
  )[:accountIds]
end

#get_all_locations(query = {}) ⇒ Object

GET /locations



5
6
7
8
9
10
11
12
# File 'lib/chatmeter/api/location.rb', line 5

def get_all_locations(query={})
  request(
    expects: 200,
    method:  :get,
    path:    '/locations',
    query:  query
  )
end

#get_all_reviews(params = {}) ⇒ Object

GET /reviews



15
16
17
18
19
20
21
22
# File 'lib/chatmeter/api/review.rb', line 15

def get_all_reviews(params={})
  request(
    expects: 200,
    method:  :get,
    path:    "/reviews",
    query:    params
  )[:reviews]
end

#get_campaign_by_id(campaign_id) ⇒ Object

GET /reviewBuilder/campaign/get/campaignId



24
25
26
27
28
29
30
# File 'lib/chatmeter/api/campaign.rb', line 24

def get_campaign_by_id(campaign_id)
  request(
    expects:  200,
    method:   :get,
    path:     "/reviewBuilder/campaign/get/#{campaign_id}"
  )
end

#get_listings(params = {}) ⇒ Object

GET /listings



7
8
9
10
11
12
13
14
# File 'lib/chatmeter/api/listings.rb', line 7

def get_listings(params={})
  request(
    expects: 200,
    method:  :get,
    path:    "/listings",
    query:   params
  )
end

#get_locations_by_reseller_location_id(reseller_location_id) ⇒ Object

GET /locations/externalId/resellerLocationId



15
16
17
18
19
20
21
# File 'lib/chatmeter/api/location.rb', line 15

def get_locations_by_reseller_location_id(reseller_location_id)
  request(
    expects: 200,
    method:  :get,
    path:    "/locations/externalId/#{reseller_location_id}"
  )
end

#get_review_metrics(params = {}) ⇒ Object

GET /dashboard/reviewReport



5
6
7
8
9
10
11
12
# File 'lib/chatmeter/api/review.rb', line 5

def get_review_metrics(params={})
  request(
    expects: 200,
    method:  :get,
    path:    "/dashboard/reviewReport",
    query:    params
  )
end

#get_single_review(review_id) ⇒ Object

GET /reviews/reviewId



25
26
27
28
29
30
31
# File 'lib/chatmeter/api/review.rb', line 25

def get_single_review(review_id)
  request(
    expects: 200,
    method:  :get,
    path:    "/reviews/#{review_id}"
  )
end

#get_user(user_id) ⇒ Object

GET /user/user_id



15
16
17
18
19
20
21
# File 'lib/chatmeter/api/user_management.rb', line 15

def get_user(user_id)
  request(
    expects: 200,
    method:  :get,
    path:    "/user/#{user_id}"
  )
end

#get_user_locations(user_id) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/chatmeter/api/user_management.rb', line 81

def get_user_locations(user_id)
  request(
    expects: 200,
    method: :get,
    path: "/users/#{user_id}/locations"
  )
end

#groups_for_user(user_id) ⇒ Object

GET /users/user_id/groups



5
6
7
8
9
10
11
# File 'lib/chatmeter/api/user_group_access.rb', line 5

def groups_for_user(user_id)
  request(
    expects: 200,
    method:  :get,
    path:    "/users/#{user_id}/groups"
  )
end

#list_all_campaignsObject

GET /reviewBuilder/campaign/get



5
6
7
8
9
10
11
# File 'lib/chatmeter/api/campaign.rb', line 5

def list_all_campaigns
  request(
    expects: 200,
    method:  :get,
    path:    "/reviewBuilder/campaign/get"
  )
end

#list_all_groupsObject

GET /groups



5
6
7
8
9
10
11
# File 'lib/chatmeter/api/group.rb', line 5

def list_all_groups
  request(
    expects: 200,
    method:  :get,
    path:    "/groups"
  )[:groups]
end

#list_all_users(params = {}) ⇒ Object

GET /users



5
6
7
8
9
10
11
12
# File 'lib/chatmeter/api/user_management.rb', line 5

def list_all_users(params={})
  request(
    expects: 200,
    method:  :get,
    path:    "/users",
    query:    params
  )[:users]
end

#locations_for_user(user_id) ⇒ Object

GET /users/user_id/locations



5
6
7
8
9
10
11
# File 'lib/chatmeter/api/user_location_access.rb', line 5

def locations_for_user(user_id)
  request(
    expects: 200,
    method:  :get,
    path:    "/users/#{user_id}/locations"
  )
end

#post_login(username, password) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/chatmeter/api/login.rb', line 4

def (username, password)
  request(
    expects: 200,
    method:  :post,
    path:    '/login',
    body:    { username: username, password: password }.to_json
  )
end

#remove_location_access(user_id, params) ⇒ Object

DELETE /users/user_id/locations?locationIds=location_ids



26
27
28
29
30
31
32
33
# File 'lib/chatmeter/api/user_location_access.rb', line 26

def remove_location_access(user_id, params)
  request(
    expects:  200,
    method:   :post,
    path:     "/users/#{user_id}/locations/delete",
    body: params.to_json
  )
end

#request(params, &block) ⇒ Object



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
# File 'lib/chatmeter/api.rb', line 59

def request(params, &block)
  params[:path] = "/#{OPTIONS[:api_version]}#{params[:path]}"

  begin
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status
    when 400 then Chatmeter::API::Errors::BadRequest
    when 401 then Chatmeter::API::Errors::Unauthorized
    when 403 then Chatmeter::API::Errors::Forbidden
    when 404 then Chatmeter::API::Errors::NotFound
    when /50./ then Chatmeter::API::Errors::RequestFailed
    else Chatmeter::API::Errors::ErrorWithResponse
    end

    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    raise(reerror)
  end

  if response.body && !response.body.empty?
    begin
      response.body = MultiJson.load(response.body, symbolize_keys: true)
    rescue
      if response.headers['Content-Type'] === 'application/json'
        raise
      end
      # leave non-JSON body as is
    end
  end

  @connection.reset
  response.body || ""
end

#respond_to_review(review_id, params) ⇒ Object

POST /reviews/ReviewId/responses



34
35
36
37
38
39
40
41
# File 'lib/chatmeter/api/review.rb', line 34

def respond_to_review(review_id, params)
  request(
    expects:  200,
    method:   :post,
    path:     "/reviews/#{review_id}/responses",
    body:     params.to_json
  )
end

#restore_location(location_ids) ⇒ Object

PUT /locations/restore (array)



53
54
55
56
57
58
59
60
# File 'lib/chatmeter/api/location.rb', line 53

def restore_location(location_ids)
  request(
    expects: 200,
    method:  :put,
    path:    '/locations/restore',
    body:    location_ids.to_json
  )
end

#review_not_mine(review_id, params) ⇒ Object

POST /reviews/reviewId/notMine



64
65
66
67
68
69
70
71
# File 'lib/chatmeter/api/review.rb', line 64

def review_not_mine(review_id, params)
  request(
    expects:  200,
    method:   :post,
    path:     "/reviews/#{review_id}/notMine",
    body:     params.to_json
  )
end

#search_campaign(params = {}) ⇒ Object

GET /reviewBuilder/campaign



43
44
45
46
47
48
49
# File 'lib/chatmeter/api/campaign.rb', line 43

def search_campaign(params={})
  request(
    expects:  200,
    method:   :get,
    path:     "/reviewBuilder/campaign?#{params.to_query}"
  )
end

#sso_token_for(user_name) ⇒ Object

GET /singlesignon/generateLoginToken?username=username



5
6
7
8
9
10
11
# File 'lib/chatmeter/api/single_signon.rb', line 5

def sso_token_for(user_name)
  request(
    expects: 200,
    method:  :get,
    path:    "/singlesignon/generateLoginToken?username=#{user_name}"
  )[:ssoToken]
end

#update_campaign(campaign_id, params) ⇒ Object

PUT /reviewBuilder/campaign/campaignId



33
34
35
36
37
38
39
40
# File 'lib/chatmeter/api/campaign.rb', line 33

def update_campaign(campaign_id, params)
  request(
    expects:  200,
    method:   :put,
    path:     "/reviewBuilder/campaign/#{campaign_id}",
    body:     params.to_json
  )
end

#update_group(group_id, params) ⇒ Object

PUT /groups/group_id



24
25
26
27
28
29
30
31
# File 'lib/chatmeter/api/group.rb', line 24

def update_group(group_id, params)
  request(
    expects:  200,
    method:   :put,
    path:     "/groups/#{group_id}",
    body:     params.to_json
  )
end

#update_group_locations(group_id, params) ⇒ Object

POST /groups/group_id/locations



43
44
45
46
47
48
49
50
# File 'lib/chatmeter/api/group.rb', line 43

def update_group_locations(group_id, params)
  request(
    expects:  200,
    method:   :post,
    path:     "/groups/#{group_id}/locations",
    body:     params.to_json
  )
end

#update_location(location_id, fields) ⇒ Object

PUT /locations/locationId



34
35
36
37
38
39
40
41
# File 'lib/chatmeter/api/location.rb', line 34

def update_location(location_id, fields)
  request(
    expects: 200,
    method:  :put,
    path:    "/locations/#{location_id}",
    body:    fields.to_json
  )
end

#update_user(user_id, params) ⇒ Object

PUT /users/user_id



34
35
36
37
38
39
40
41
# File 'lib/chatmeter/api/user_management.rb', line 34

def update_user(user_id, params)
  request(
    expects:  200,
    method:   :put,
    path:     "/users/#{user_id}",
    body:     params.to_json
  )
end

#update_user_password(user_id, params) ⇒ Object

PUT /users/user_id/password



44
45
46
47
48
49
50
51
# File 'lib/chatmeter/api/user_management.rb', line 44

def update_user_password(user_id, params)
  request(
    expects:  200,
    method:   :put,
    path:     "/users/#{user_id}/password",
    body:    params.to_json
  )
end