lib/keikokuc/client.rb in keikokuc-0.7 vs lib/keikokuc/client.rb in keikokuc-0.8

- old
+ new

@@ -9,25 +9,30 @@ include HandlesTimeout InvalidNotification = Class.new Unauthorized = Class.new - attr_accessor :producer_api_key, :user, :password + attr_accessor :username, :api_key + # Internal: Initialize a Client + # + # opts = a hash containing two possible attributes: + # api_key - the user's or producer's API key (required) + # username - the producer's username (only required for publishers) def initialize(opts = {}) - @producer_api_key = opts[:producer_api_key] - @user = opts[:user] - @password = opts[:password] + @api_key = opts.fetch(:api_key) + @username = opts[:username] end # Internal: posts a new notification to keikoku # # attributes - a hash containing notification attributes # # Examples # - # client = Keikokuc::Client.new(producer_api_key: 'abcd') + # client = Keikokuc::Client.new(username: 'heroku-postgres', + # api_key: 'abcd') # response, error = client.post_notification(message: 'hello') # # Returns # # two objects: @@ -40,11 +45,11 @@ # * `Client::InvalidNotification` if the response indicates # invalid notification attributes # * `Client::Unauthorized` if API key auth fails def post_notification(attributes) begin - response = notifications_api.post(encode_json(attributes), {'X-KEIKOKU-AUTH' => producer_api_key}) + response = notifications_api.post(encode_json(attributes)) [parse_json(response), nil] rescue RestClient::UnprocessableEntity => e [parse_json(e.response), InvalidNotification] rescue RestClient::Unauthorized [{}, Unauthorized] @@ -54,11 +59,11 @@ # Internal: gets all active notifications for a user # # Examples # - # client = Keikokuc::Client.new(user: 'user@example.com', password: 'pass') + # client = Keikokuc::Client.new(api_key: 'api-key') # response, error = client.get_notifications # # Returns # # two objects: @@ -81,10 +86,12 @@ # Public: Marks a notification as read # # remote_id - the keikoku id for the notification to mark as read # + # Returns + # # two objects: # The response as a hash # The error if any (nil if no error) # # Possible errors include: @@ -93,23 +100,23 @@ # * `Client::Unauthorized` if HTTP Basic auth fails def read_notification(remote_id) begin response = notifications_api["/#{remote_id}/read"].post '' parsed_response = parse_json(response) - parsed_response[:read_at] = DateTime.parse(parsed_response[:read_at]) [parsed_response, nil] rescue RestClient::Unauthorized [{}, Unauthorized] end end handle_timeout :read_notification - private def notifications_api # :nodoc: - @notifications_api ||= RestClient::Resource.new(api_url, - :user => user, - :password => password) + @notifications_api ||= RestClient::Resource.new( + api_url, + :user => username || '', + :password => api_key + ) end def api_url # :nodoc: "https://keikoku.herokuapp.com/api/v1/notifications" end