Sha256: db207653faf10c210f3a47c92707e3f893b60bc987a56d09890d8d0aac3fad0e

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module Mention
  class Account
    def initialize(credentials)
      @account_id = credentials.fetch(:account_id)
      @access_token = credentials.fetch(:access_token)
    end

    def alerts
      @alerts ||= begin
        raw_data = JSON.parse(resource['alerts'].get)
        raw_data['alerts'].map do |hash|
          Alert.new(hash)
        end
      end
    end

    def name
      account_info['account']['name']
    end

    def id
      account_info['account']['id']
    end

    def email
      account_info['account']['email']
    end

    def created_at
      Time.parse(account_info['account']['created_at'])
    end

    def add(alert)
      creator = AlertCreator.new(resource, alert)
      @alerts = nil if creator.valid?
      creator.created_alert
    end

    def remove_alert(alert, share)
      resource["/alerts/#{alert.id}/shares/#{share.id}"].delete
    end

    def fetch_mentions(alert, params = {})
      raw_data = JSON.parse(resource["/alerts/#{alert.id}/mentions"].get params: params)
      MentionList.new(raw_data)
    end

    private
    attr_reader :account_id, :access_token

    def resource
      @resource ||= RestClient::Resource.new("https://api.mention.net/api/accounts/#{account_id}",
        headers: {'Authorization' => "Bearer #{access_token}", "Accept" => "application/json"})
    end

    def account_info
      @account_info ||= JSON.parse(resource.get)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mention-api-0.0.3 lib/mention/account.rb
mention-api-0.0.2 lib/mention/account.rb