Project

mozenda

0.0
No commit activity in last 3 years
No release in over 3 years
Mozenda API gem
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 0.9.0
~> 1.6.2.1
~> 2.3.0
>= 0
 Project Readme

mozenda

mozenda is a ruby gem for interacting with mozenda via their api.

Installation

Add this line to your application's Gemfile:

gem 'mozenda'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mozenda

Usage

mozenda requires a WebServiceKey to be configured. The following code configures mozenda (config/initializers is a good place for this).

Mozenda.configuration do |config|
  config.web_service_key = "your-mozenda-web-service-key"
end

To add single item to Collection:

collection_id = 1046
collection = Mozenda::Model::Collection.new(collection_id)
collection.add_item({
  "Field1" => "qq",
  "Field3" => 123
})

To add many items to Collection from XML file:

path_to_xml_file = Pathname.new("./bulk_test.xml").realpath.to_s
collection_id = 1046
collection = Mozenda::Model::Collection.new(collection_id)
collection.add_items_from_file(path_to_xml_file)

To clear Collection:

collection_id = 1046
collection = Mozenda::Model::Collection.new(collection_id)
collection.clear

To simple run Agent:

agent_id = 1050
agent = Mozenda::Model::Agent.new(agent_id)
agent.run

To run Agent with optional params:

agent_id = 1050
agent = Mozenda::Model::Agent.new(agent_id)
agent.run({
  "MyParam1" => 123,
  "my-other-param" => "imba!"
})

To run Agent with Job.StatusUrl:

agent_id = 1050
agent = Mozenda::Model::Agent.new(agent_id)
optional_params = {:foo => 123, :bar => "xyz"}
agent.run(optional_params, {
  :status_url => "http://localhost:3000/some/path",
  :replacement_values => [:job_id, :job_status, :job_ended]
})

To get Agent info:

agent_id = 1050
agent = Mozenda::Model::Agent.new(agent_id)
agent_get_response = agent.get
agent_get_response.to_h
agent_get_response.to_xml

To get Agent Jobs list info:

agent_id = 1050
agent = Mozenda::Model::Agent.new(agent_id)
agent_get_jobs_response = agent.get_jobs
agent_get_jobs_response.to_h
agent_get_jobs_response.to_xml
agent_get_jobs_response.job_list

To simple publish Collection:

collection_id = 1055
collection = Mozenda::Model::Collection.new(collection_id)
collection_publish_response = collection.publish
collection_publish_response.to_h
collection_publish_response.to_xml

To publish Collection with some status URL & replacement values:

collection_id = 1055
collection = Mozenda::Model::Collection.new(collection_id)
params = {
  :status_url => "http://my-app.com/-mozen-on-publish-url?my_param=123&other_param=foo",
  :replacement_values => [:job_id, :job_status, :job_created, :job_ended]
}
collection_publish_response = collection.publish(params)
collection_publish_response.to_h
collection_publish_response.to_xml

Replacement values (defined in Mozenda::REPLACEMENT_VALUES) for Collection.Publish and/or Agent.Run:

Mozenda::REPLACEMENT_VALUES = {
  agent_id: "AgentID",
  agent_name: "Agent.Name",
  agent_description: "Agent.Description",
  agent_domain: "Agent.Domain",
  job_id: "JobID",
  job_status: "Job.Status",
  job_created: "Job.Created",
  job_ended: "Job.Ended",
  job_name: "Job.Name",
  job_description: "Job.Description"
}

To generate XML file for bulk Collection.AddItem:

data = [
  {
    'Name' => "Bob",
    "Age" => 12
  },
  {
    'Name' => "Ed",
    "Age" => 99
  },
  {
    'Name' => "Kate",
    "Age" => 23
  }
]
xml_generator = Mozenda::XmlGenerator.new(data)
xml = xml_generator.generate

result XML:

<?xml version="1.0" encoding="utf-8"?>
<ItemList>
  <Item>
    <Name>Bob</Name>
    <Age>12</Age>
  </Item>
  <Item>
    <Name>Ed</Name>
    <Age>99</Age>
  </Item>
  <Item>
    <Name>Kate</Name>
    <Age>23</Age>
  </Item>
</ItemList>

To get Job details:

job_id = "7E08EB1F-DDA6-4459-BBDA-3F88B4AB7B7C"
job_get_response = Mozenda::Model::Job.get(job_id)
job_get_response.job_id # => "7E08EB1F-DDA6-4459-BBDA-3F88B4AB7B7C"
job_get_response.state # => "Done"
job_get_response.error? # => false

To pause running Job:

job_id = "E656D634-C63F-46D9-AFB5-C9AE1F1F2A9E"
job_pause_response = Mozenda::Model::Job.pause(job_id)
job_pause_response.to_xml
job_resume_response.to_h

To resume Job that is in a Paused or Error state:

job_id = "7E08EB1F-DDA6-4459-BBDA-3F88B4AB7B7C"
job_resume_response = Mozenda::Model::Job.resume(job_id)
job_resume_response.to_xml
job_resume_response.to_h

To cancel Job that is in a Paused or Error state:

job_id = "7E08EB1F-DDA6-4459-BBDA-3F88B4AB7B7C"
job_cancel_response = Mozenda::Model::Job.cancel(job_id)
job_resume_response.to_xml
job_resume_response.to_h

TODO

  • Implement requests-per-minute limit.
  • Implement other Mozenda services

License

This software is relased under the MIT license.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request