Sha256: e75426179205d880e1878fe09cf4ba2711279d99021c180e26a9a5c96d1a3ae2

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

module Hungry
  class User < Resource

    self.endpoint = '/users'

    ### ASSOCIATIONS:

    has_many :reviews, 'Hungry::Review'

    ### FINDERS:

    def self.with_ip(ip_address)
      collection.all(ip: ip_address)
    end

    ### CLASS METHODS:

    def self.authenticate_with_persistence_token(persistence_token)
      result = authenticate_via_api 'Cookie' => "persistence_token=#{persistence_token}"
      return false unless result.present?

      from_json(result)
    end

    def self.authenticate_with_perishable_token(perishable_token)
      result = authenticate_via_api query: "perishable_token=#{perishable_token}"
      return false unless result.present?

      from_json(result)
    end

    def self.from_json(json)
      new Util.parse_json(json)
    end

    def self.authenticate_via_api(options = {})
      options = {
        path:  '/account.json',
        query: nil
      }.merge(options)

      url = URI.parse(Hungry.api_url)
      url.path  = options.delete(:path)
      url.query = options.delete(:query)

      http = Net::HTTP.new(url.host, url.port)
      if url.scheme == 'https'
        http.use_ssl     = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end

      request  = Net::HTTP::Get.new(url.request_uri, options)
      response = http.request request

      if response.code.to_i == 200
        response.body
      else
        false
      end
    end

    ### ATTRIBUTES:

                  ### Preview:
    attr_accessor :id, :name, :avatar, :slug, :profile_url, :score, :bio,
                  :location, :email,

                  ### FULL:
                  :gender, :birth_date, :website_url, :persistence_token,
                  :single_access_token, :maintainer_of, :connections,
                  :device_tokens, :admin,

                  ### Utility:
                  :resources, :counters, :created_at, :updated_at, :activated_at

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hungry-0.1.5 lib/hungry/user.rb
hungry-0.1.4 lib/hungry/user.rb
hungry-0.1.3 lib/hungry/user.rb
hungry-0.1.2 lib/hungry/user.rb